Documentation
¶
Overview ¶
Package builder defines a Builder type that may be used to build a [jsonschema.Schema] step by step.
It is usually more convenient to use the Builder defined by the specific JSON schema draft that you are using.
The Infer and InferType functions may be used with a Builder to build a schema from a Go type.
Index ¶
- func Infer[T any, Builder inferBuilder[Builder]](builder Builder, opts *InferOpts) (Builder, error)
- func InferType[Builder inferBuilder[Builder]](builder Builder, typ reflect.Type, opts *InferOpts) (Builder, error)
- type Builder
- func (b *Builder) AddAny(keyword *schema.Keyword, v any) *Builder
- func (b *Builder) AddBool(keyword *schema.Keyword, v bool) *Builder
- func (b *Builder) AddFloat(keyword *schema.Keyword, f float64) *Builder
- func (b *Builder) AddInt(keyword *schema.Keyword, i int64) *Builder
- func (b *Builder) AddMapArrayOrSchema(keyword *schema.Keyword, pv schema.PartMapArrayOrSchema) *Builder
- func (b *Builder) AddMapSchema(keyword *schema.Keyword, m map[string]*schema.Schema) *Builder
- func (b *Builder) AddSchema(keyword *schema.Keyword, s *schema.Schema) *Builder
- func (b *Builder) AddSchemaOrSchemas(keyword *schema.Keyword, pv schema.PartSchemaOrSchemas) *Builder
- func (b *Builder) AddSchemaParts(parts []schema.Part) *Builder
- func (b *Builder) AddSchemas(keyword *schema.Keyword, schemas []*schema.Schema) *Builder
- func (b *Builder) AddString(keyword *schema.Keyword, s string) *Builder
- func (b *Builder) AddStrings(keyword *schema.Keyword, s []string) *Builder
- func (b *Builder) Build() *schema.Schema
- func (b *Builder) NewBuilder() *Builder
- type InferOpts
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Infer ¶
Infer adds schema elements to b designed to validate JSON values that unmarshal into values of the given type.
The default translation is:
- Strings become "type":"string".
- Bools become "type":"bool".
- Integer types become "type":"integer".
- Floating point types become "type":"number".
- Slice and array types become "type":"array", with an "items" entry mapped to a schema inferred from the element type. An array type will have "minItems" and "maxItems" set to the length of the array.
- Maps with a string key become "type":"object", with an "additionalProperties" entry mapped to a schema inferred from the value type.
- Structs have "type":"object", and include "properties" for each exported field using the JSON name of the field. Fields ignored by the JSON marshaler are ignored here. Fields whose JSON attributes include neither "omitempty" nor "omitzero" are added to a "required" list.
- Interface types are accepted but add nothing to the schema.
- Some standard library types with custom JSOM marshaling are translated to predefined schemas. This may be overridden using the InferOpts.Types option.
For other Go types Infer will return an error. Other types may be handled specially using the InferOpts.Types option.
Infer will look at jsonschema struct field tags. The tag may start with keyword=value pairs separated by commas, where a keyword does not contain space or tab characters. If the tag, or the trailing part of the tag, does not contain =, that will set the "description" property. Recognized tag keywords are:
enum=A,enum=B,... sets the "enum" property to the listed values
As this function takes and returns a Builder, the caller may add additional schema checks before calling the Build method to get a schema.
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder is a JSON schema builder. Builder provides a list of methods that may be used to add new elements to the schema. This should be used by programs that need to create a JSON schema from scratch, rather than unmarshaling it from a JSON representation.
When using Builder there is no support for references to other schemas via $ref or $dynamicRef. Similarly there is no way to define anchors via $anchor, $dynamicAnchor, or $defs.
func New ¶
func New(v *schema.Vocabulary) *Builder
New returns a new Builder to build a [*types.Schema] described by the [*types.Vocabulary] v.
func (*Builder) AddBool ¶
AddBool adds a keyword whose argument is a bool. This panics if the keyword does not expect a bool.
func (*Builder) AddMapArrayOrSchema ¶
func (b *Builder) AddMapArrayOrSchema(keyword *schema.Keyword, pv schema.PartMapArrayOrSchema) *Builder
AddMapArrayOrSchema adds a keyword whose argument is a map from strings to either arrays or schemas. This is like the draft7 "dependencies" keyword. This probably should not be used for anything else.
func (*Builder) AddMapSchema ¶
AddMapSchema adds a keyword whose argument is a mapping from strings to schemas.
func (*Builder) AddSchema ¶
AddSchema adds a keyword whose argument is a schema. This panics if the schema is nil.
func (*Builder) AddSchemaOrSchemas ¶
func (b *Builder) AddSchemaOrSchemas(keyword *schema.Keyword, pv schema.PartSchemaOrSchemas) *Builder
AddSchemaOrSchemas adds a keyword whose argument is either a single schema or an array of schemas.
func (*Builder) AddSchemaParts ¶
AddSchemaParts adds a list of parts.
func (*Builder) AddSchemas ¶
AddSchemas adds a keyword whose argument is a list of schemas. This panics if the list of schemas is empty or any is nil. This may be used to implement a custom schema keyword.
func (*Builder) AddStrings ¶
AddStrings adds a keyword whose argument is an array of strings.
func (*Builder) NewBuilder ¶
NewBuilder returns a new Builder with the same vocabulary.
type InferOpts ¶
type InferOpts struct {
// Types maps types to the schema to infer for values of those types.
// The key is a type,
// the value is the schema to use for values of that type.
// This overrides any default inferences;
// mapping to nil uses the default behavior for that type.
Types map[reflect.Type]*schema.Schema
// If IgnoreInvalidTypes is true, fields that can't be represented
// in a JSON schema are ignored. For example, fields of
// function type. The caller can add describe these fields using
// the returned Builder.
IgnoreInvalidTypes bool
}
InferOpts contains options to pass when inferring a JSON schema.