builder

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 10, 2026 License: MIT, BSD-3-Clause Imports: 11 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Infer

func Infer[T any, Builder inferBuilder[Builder]](builder Builder, opts *InferOpts) (Builder, error)

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.

func InferType

func InferType[Builder inferBuilder[Builder]](builder Builder, typ reflect.Type, opts *InferOpts) (Builder, error)

InferType is like Infer but takes a reflect.Type rather than a type argument.

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) AddAny

func (b *Builder) AddAny(keyword *schema.Keyword, v any) *Builder

AddAny adds a keyword whose argument has any type.

func (*Builder) AddBool

func (b *Builder) AddBool(keyword *schema.Keyword, v bool) *Builder

AddBool adds a keyword whose argument is a bool. This panics if the keyword does not expect a bool.

func (*Builder) AddFloat

func (b *Builder) AddFloat(keyword *schema.Keyword, f float64) *Builder

AddFloat adds a keyword whose argument is an float.

func (*Builder) AddInt

func (b *Builder) AddInt(keyword *schema.Keyword, i int64) *Builder

AddInt adds a keyword whose argument is an int.

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

func (b *Builder) AddMapSchema(keyword *schema.Keyword, m map[string]*schema.Schema) *Builder

AddMapSchema adds a keyword whose argument is a mapping from strings to schemas.

func (*Builder) AddSchema

func (b *Builder) AddSchema(keyword *schema.Keyword, s *schema.Schema) *Builder

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

func (b *Builder) AddSchemaParts(parts []schema.Part) *Builder

AddSchemaParts adds a list of parts.

func (*Builder) AddSchemas

func (b *Builder) AddSchemas(keyword *schema.Keyword, schemas []*schema.Schema) *Builder

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) AddString

func (b *Builder) AddString(keyword *schema.Keyword, s string) *Builder

AddString adds a keyword whose argument is a string.

func (*Builder) AddStrings

func (b *Builder) AddStrings(keyword *schema.Keyword, s []string) *Builder

AddStrings adds a keyword whose argument is an array of strings.

func (*Builder) Build

func (b *Builder) Build() *schema.Schema

Build builds and returns the [*jsonschema.Schema].

func (*Builder) NewBuilder

func (b *Builder) NewBuilder() *Builder

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL