You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
3.8 KiB

  1. # Go JSON Schema Reflection
  2. [![Build Status](https://travis-ci.org/alecthomas/jsonschema.png)](https://travis-ci.org/alecthomas/jsonschema)
  3. [![Gitter chat](https://badges.gitter.im/alecthomas.png)](https://gitter.im/alecthomas/Lobby)
  4. [![Go Report Card](https://goreportcard.com/badge/github.com/alecthomas/jsonschema)](https://goreportcard.com/report/github.com/alecthomas/jsonschema)
  5. [![GoDoc](https://godoc.org/github.com/alecthomas/jsonschema?status.svg)](https://godoc.org/github.com/alecthomas/jsonschema)
  6. This package can be used to generate [JSON Schemas](http://json-schema.org/latest/json-schema-validation.html) from Go types through reflection.
  7. It supports arbitrarily complex types, including `interface{}`, maps, slices, etc.
  8. And it also supports json-schema features such as minLength, maxLength, pattern, format and etc.
  9. ## Example
  10. The following Go type:
  11. ```go
  12. type TestUser struct {
  13. ID int `json:"id"`
  14. Name string `json:"name"`
  15. Friends []int `json:"friends,omitempty"`
  16. Tags map[string]interface{} `json:"tags,omitempty"`
  17. BirthDate time.Time `json:"birth_date,omitempty"`
  18. }
  19. ```
  20. Results in following JSON Schema:
  21. ```go
  22. jsonschema.Reflect(&TestUser{})
  23. ```
  24. ```json
  25. {
  26. "$schema": "http://json-schema.org/draft-04/schema#",
  27. "$ref": "#/definitions/TestUser",
  28. "definitions": {
  29. "TestUser": {
  30. "type": "object",
  31. "properties": {
  32. "birth_date": {
  33. "type": "string",
  34. "format": "date-time"
  35. },
  36. "friends": {
  37. "type": "array",
  38. "items": {
  39. "type": "integer"
  40. }
  41. },
  42. "id": {
  43. "type": "integer"
  44. },
  45. "name": {
  46. "type": "string"
  47. },
  48. "tags": {
  49. "type": "object",
  50. "patternProperties": {
  51. ".*": {
  52. "type": "object",
  53. "additionalProperties": true
  54. }
  55. }
  56. }
  57. },
  58. "additionalProperties": false,
  59. "required": ["id", "name"]
  60. }
  61. }
  62. }
  63. ```
  64. ## Configurable behaviour
  65. The behaviour of the schema generator can be altered with parameters when a `jsonschema.Reflector`
  66. instance is created.
  67. ### ExpandedStruct
  68. If set to ```true```, makes the top level struct not to reference itself in the definitions. But type passed should be a struct type.
  69. eg.
  70. ```go
  71. type GrandfatherType struct {
  72. FamilyName string `json:"family_name" jsonschema:"required"`
  73. }
  74. type SomeBaseType struct {
  75. SomeBaseProperty int `json:"some_base_property"`
  76. // The jsonschema required tag is nonsensical for private and ignored properties.
  77. // Their presence here tests that the fields *will not* be required in the output
  78. // schema, even if they are tagged required.
  79. somePrivateBaseProperty string `json:"i_am_private" jsonschema:"required"`
  80. SomeIgnoredBaseProperty string `json:"-" jsonschema:"required"`
  81. SomeSchemaIgnoredProperty string `jsonschema:"-,required"`
  82. SomeUntaggedBaseProperty bool `jsonschema:"required"`
  83. someUnexportedUntaggedBaseProperty bool
  84. Grandfather GrandfatherType `json:"grand"`
  85. }
  86. ```
  87. will output:
  88. ```json
  89. {
  90. "$schema": "http://json-schema.org/draft-04/schema#",
  91. "required": [
  92. "some_base_property",
  93. "grand",
  94. "SomeUntaggedBaseProperty"
  95. ],
  96. "properties": {
  97. "SomeUntaggedBaseProperty": {
  98. "type": "boolean"
  99. },
  100. "grand": {
  101. "$schema": "http://json-schema.org/draft-04/schema#",
  102. "$ref": "#/definitions/GrandfatherType"
  103. },
  104. "some_base_property": {
  105. "type": "integer"
  106. }
  107. },
  108. "type": "object",
  109. "definitions": {
  110. "GrandfatherType": {
  111. "required": [
  112. "family_name"
  113. ],
  114. "properties": {
  115. "family_name": {
  116. "type": "string"
  117. }
  118. },
  119. "additionalProperties": false,
  120. "type": "object"
  121. }
  122. }
  123. }
  124. ```