package jsonschema
Import Path
github.com/invopop/jsonschema (on go.dev)
Dependency Relation
imports 19 packages, and imported by one package
Involved Source Files
comment_extractor.go
id.go
Package jsonschema uses reflection to generate JSON Schemas from Go types [1].
If json tags are present on struct fields, they will be used to infer
property names and if a property is required (omitempty is present).
[1] http://json-schema.org/latest/json-schema-validation.html
schema.go
utils.go
Package-Level Type Names (total 4)
Definitions hold schema definitions.
http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.26
RFC draft-wright-json-schema-validation-00, section 5.26
ID represents a Schema ID type which should always be a URI.
See draft-bhutton-json-schema-00 section 8.2.1
Add appends the provided path to the id, and removes any
anchor data that might be there.
Anchor sets the anchor part of the schema URI.
Base removes any anchor information from the schema
Def adds or replaces a definition identifier.
String provides string version of ID
Validate is used to check if the ID looks like a proper schema.
This is done by parsing the ID as a URL and checking it has all the
relevant parts.
ID : expvar.Var
ID : fmt.Stringer
func ID.Add(path string) ID
func ID.Anchor(name string) ID
func ID.Base() ID
func ID.Def(name string) ID
const EmptyID
A Reflector reflects values into a Schema.
AdditionalFields allows adding structfields for a given type
AllowAdditionalProperties will cause the Reflector to generate a schema
without additionalProperties set to 'false' for all struct types. This means
the presence of additional keys in JSON objects will not cause validation
to fail. Note said additional keys will simply be dropped when the
validated JSON is unmarshaled.
Anonymous when true will hide the auto-generated Schema ID and provide what is
known as an "anonymous schema". As a rule, this is not recommended.
AssignAnchor when true will use the original struct's name as an anchor inside
every definition, including the root schema. These can be useful for having a
reference to the original struct's name in CamelCase instead of the snake-case used
by default for URI compatibility.
Anchors do not appear to be widely used out in the wild, so at this time the
anchors themselves will not be used inside generated schema.
BaseSchemaID defines the URI that will be used as a base to determine Schema
IDs for models. For example, a base Schema ID of `https://invopop.com/schemas`
when defined with a struct called `User{}`, will result in a schema with an
ID set to `https://invopop.com/schemas/user`.
If no `BaseSchemaID` is provided, we'll take the type's complete package path
and use that as a base instead. Set `Anonymous` to try if you do not want to
include a schema ID.
CommentMap is a dictionary of fully qualified go types and fields to comment
strings that will be used if a description has not already been provided in
the tags. Types and fields are added to the package path using "." as a
separator.
Type descriptions should be defined like:
map[string]string{"github.com/invopop/jsonschema.Reflector": "A Reflector reflects values into a Schema."}
And Fields defined as:
map[string]string{"github.com/invopop/jsonschema.Reflector.DoNotReference": "Do not reference definitions."}
See also: AddGoComments
Do not reference definitions. This will remove the top-level $defs map and
instead cause the entire structure of types to be output in one tree. The
list of type definitions (`$defs`) will not be included.
ExpandedStruct when true will include the reflected type's definition in the
root as opposed to a definition with a reference.
FieldNameTag will change the tag used to get field names. json tags are used by default.
IgnoredTypes defines a slice of types that should be ignored in the schema,
switching to just allowing additional properties instead.
KeyNamer allows customizing of key names.
The default is to use the key's name as is, or the json tag if present.
If a json tag is present, KeyNamer will receive the tag's name as an argument, not the original key name.
Lookup allows a function to be defined that will provide a custom mapping of
types to Schema IDs. This allows existing schema documents to be referenced
by their ID instead of being embedded into the current schema definitions.
Reflected types will never be pointers, only underlying elements.
Mapper is a function that can be used to map custom Go types to jsonschema schemas.
Namer allows customizing of type names. The default is to use the type's name
provided by the reflect package.
RequiredFromJSONSchemaTags will cause the Reflector to generate a schema
that requires any key tagged with `jsonschema:required`, overriding the
default of requiring any key *not* tagged with `json:,omitempty`.
AddGoComments will update the reflectors comment map with all the comments
found in the provided source directories. See the #ExtractGoComments method
for more details.
Reflect reflects to Schema from a value.
ReflectFromType generates root schema
SetBaseSchemaID is a helper use to be able to set the reflectors base
schema ID from a string as opposed to then ID instance.
Schema represents a JSON Schema object type.
RFC draft-bhutton-json-schema-00 section 4.3
// section 10.3.2.3
RFC draft-bhutton-json-schema-00 section 10.2.1 (Sub-schemas with logic)
// section 10.2.1.1
// section 8.2.2
// section 10.2.1.2
// section 8.3
// section 6.1.3
// section 10.3.1.3
RFC draft-bhutton-json-schema-validation-00, section 8
// section 8.3
// section 8.4
// section 8.5
// section 9.2
// section 8.2.4
// section 6.5.4
// section 10.2.2.4
// section 9.3
// section 9.1
// section 8.2.3.2
// section 10.2.2.3
// section 6.1.2
// section 9.5
// section 6.2.3
// section 6.2.5
Extras map[string]any
RFC draft-bhutton-json-schema-validation-00, section 7
// section 8.2.1
RFC draft-bhutton-json-schema-00 section 10.2.2 (Apply sub-schemas conditionally)
// section 10.2.2.1
// section 10.3.1.2 (replaces additionalItems)
// section 6.4.4
// section 6.4.1
// section 6.3.1
// section 6.5.1
// section 6.2.2
// section 6.4.5
// section 6.4.2
// section 6.3.2
// section 6.5.2
// section 6.2.4
// section 6.2.1
// section 10.2.1.4
// section 10.2.1.3
// section 6.3.3
// section 10.3.2.2
RFC draft-bhutton-json-schema-00 section 10.3.1 (arrays)
// section 10.3.1.1
RFC draft-bhutton-json-schema-00 section 10.3.2 (sub-schemas)
// section 10.3.2.1
// section 10.3.2.4
// section 9.4
// section 8.2.3.1
// section 6.5.3
// section 10.2.2.2
RFC draft-bhutton-json-schema-validation-00, section 9
// section 9.1
RFC draft-bhutton-json-schema-validation-00, section 6
// section 6.1.1
// section 6.4.3
RFC draft-bhutton-json-schema-00
// section 8.1.1
// section 9.4
MarshalJSON is used to serialize a schema object or boolean.
UnmarshalJSON is used to parse a schema object or boolean.
*Schema : github.com/goccy/go-json.Marshaler
*Schema : github.com/goccy/go-json.Unmarshaler
*Schema : encoding/json.Marshaler
*Schema : encoding/json.Unmarshaler
func Reflect(v any) *Schema
func ReflectFromType(t reflect.Type) *Schema
func (*Reflector).Reflect(v any) *Schema
func (*Reflector).ReflectFromType(t reflect.Type) *Schema
var FalseSchema *Schema
var TrueSchema *Schema
Package-Level Functions (total 5)
ExtractGoComments will read all the go files contained in the provided path,
including sub-directories, in order to generate a dictionary of comments
associated with Types and Fields. The results will be added to the `commentsMap`
provided in the parameters and expected to be used for Schema "description" fields.
The `go/parser` library is used to extract all the comments and unfortunately doesn't
have a built-in way to determine the fully qualified name of a package. The `base` paremeter,
the URL used to import that package, is thus required to be able to match reflected types.
When parsing type comments, we use the `go/doc`'s Synopsis method to extract the first phrase
only. Field comments, which tend to be much shorter, will include everything.
NewProperties is a helper method to instantiate a new properties ordered
map.
Reflect reflects to Schema from a value using the default Reflector
ReflectFromType generates root schema using the default Reflector
ToSnakeCase converts the provided string into snake case using dashes.
This is useful for Schema IDs and definitions to be coherent with
common JSON Schema examples.
Package-Level Variables (total 3)
FalseSchema defines a schema with a false value
TrueSchema defines a schema with a true value
Version is the JSON Schema version.
Package-Level Constants (only one)
EmptyID is used to explicitly define an ID with no value.
![]() |
The pages are generated with Golds v0.8.2. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |