jsonschema

package module
v0.0.0-...-8a85022 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2023 License: MIT Imports: 6 Imported by: 0

README

Go

jsonschema

Code generation from json schemas with basic support for 2019-09:

Installation

  • go install github.com/RossMerr/jsonschema/cmd/jsonschema
Notes for usage
$id
  • Top level $id must follow the absolute-URI convention of being a canonical URL. The old 'id' field is not supported.
$ref
  • Can resolve to any point in the document or another local file using a JSON Pointer.

"#/definitions/address"

"http://www.sample.com/definitions.json#/address"

$def
  • The old 'definitions' field is still supported but merged into the new $def field.
allOf
  • Will generate a struct with all of the subschemas embedded.
anyOf
  • Will generate an array of a interface with all subschemas implementing its method.
oneOf
  • Will generate a interface with all subschemas implementing its method.
Validation

All generated go types will be a value or reference type depending on the 'required' field. All fields and struct's also get generated validation tags mostly conforming to the Well-known struct tags for validate. Additional values for the validate tag include:

  • allof
  • anyof
  • oneof
  • regex

Which you can then use for any custom validators.

Not or partial support

String
Regular Expressions

You can add support with a custom validate field tag. But a regex validator won't be added because commas and = signs can be part of a regex which conflict with the validation definitions.

Numeric
Object
Array
Generic
Combining schemas
External JSON Pointer

All schemas must be local

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains(a []string, b string) bool

Contains checks a slice to see if it contains the matching string

func DataTypeValues

func DataTypeValues() map[DataType]string

func Filter

func Filter(a []string, delegate func(string) bool) []string

Filter removes from a slice any true conditions from the delegate

func MetaSchemaVersions

func MetaSchemaVersions() []string

MetaSchemaVersions returns a list of all supported schema versions

func ResolveIDs

func ResolveIDs(b json.RawMessage) map[ID]*Schema

ResolveIDs looks over the raw json and traverses over the schema looking for $id fields,

Types

type DataType

type DataType string

DataType specifies the data type for a schema

const (
	// String type is used for strings of text. It may contain Unicode characters
	String DataType = "string"
	// Object are the mapping type in JSON. They map “keys” to “values”. In JSON, the “keys” must always be strings. Each of these pairs is conventionally referred to as a “property”
	Object DataType = "object"
	// Array are used for ordered elements. In JSON, each element in an array may be of a different type
	Array DataType = "array"
	// Integer type is used for integral numbers
	Integer DataType = "integer"
	// Number type is used for any numeric type, either integers or floating point numbers
	Number DataType = "number"
	// Boolean type matches only two special values: true and false. Note that values that evaluate to true or false, such as 1 and 0, are not accepted by the schema
	Boolean DataType = "boolean"
	// Null type is generally used to represent a missing value. When a schema specifies a type of null, it has only one acceptable value: null
	Null DataType = "null"
)

JSON Schema defines the following basic types

func (DataType) String

func (s DataType) String() string

String returns the string representation of the DataType

type ID

type ID string

ID ($id) property is a URI-reference that serves two purposes:

It declares a unique identifier for the schema.
It declares a base URI against which $ref URI-references are resolved.

func NewID

func NewID(s string) (ID, error)

NewID returns a new ID

func (ID) Fragment

func (s ID) Fragment() string

Fragment returns the fragment identifier from the ID, everything after the hash mark '#'

func (ID) String

func (s ID) String() string

String returns the underlying string representation

func (*ID) UnmarshalJSON

func (s *ID) UnmarshalJSON(b []byte) error

type Path

type Path []string

func (Path) ToKey

func (s Path) ToKey() string

type Reference

type Reference string

Reference ($ref) is used to reference other schemas, used with allOf, anyOf and oneOf

func NewReference

func NewReference(s string) (Reference, error)

NewReference returns a Reference

func (Reference) ID

func (s Reference) ID() (ID, error)

ID return's the ID of the Reference

func (Reference) IsNotEmpty

func (s Reference) IsNotEmpty() bool

func (Reference) Path

func (s Reference) Path() Path

Path returns all segments of the relative path of the referenced schema

func (Reference) String

func (s Reference) String() string

func (Reference) ToKey

func (s Reference) ToKey() string

func (*Reference) UnmarshalJSON

func (s *Reference) UnmarshalJSON(b []byte) error

type Schema

type Schema struct {
	// Annotations
	Description string `json:"description,omitempty"`
	Title       string `json:"title,omitempty"`
	Default     string `json:"default,omitempty"`
	Examples    string `json:"examples,omitempty"`

	ID     ID            `json:"$id,omitempty"`
	Schema SchemaVersion `json:"$schema,omitempty"`
	Ref    Reference     `json:"$ref,omitempty"`

	Defs map[string]*Schema `json:"$defs,omitempty"`
	// Deprecated use Defs
	Definitions map[string]*Schema `json:"definitions,omitempty"`

	Anchor string `json:"$anchor,omitempty"`

	Type DataType `json:"type,omitempty"`

	// Required Properties
	Required []string `json:"required,omitempty"`

	// Properties
	Properties           map[string]*Schema `json:"properties,omitempty"`
	AdditionalProperties *bool              `json:"additionalproperties,omitempty"`

	Items *Schema   `json:"items,omitempty"`
	OneOf []*Schema `json:"oneof,omitempty"`
	AnyOf []*Schema `json:"anyof,omitempty"`
	AllOf []*Schema `json:"allof,omitempty"`
	Enum  []string  `json:"enum,omitempty"`

	// Size
	MaxProperties *uint32 `json:"maxproperties,omitempty"`
	MinProperties *uint32 `json:"minproperties,omitempty"`

	// Validation
	MaxLength        *uint32 `json:"maxlength,omitempty"`
	MinLength        *uint32 `json:"minlength,omitempty"`
	MaxContains      *uint32 `json:"maxcontains,omitempty"`
	MinContains      *uint32 `json:"mincontains,omitempty"`
	MaxItems         *uint32 `json:"maxitems,omitempty"`
	MinItems         *uint32 `json:"minitems,omitempty"`
	Maximum          *int32  `json:"maximum,omitempty"`
	ExclusiveMaximum *int32  `json:"exclusivemaximum,omitempty"`
	Minimum          *int32  `json:"minimum,omitempty"`
	ExclusiveMinimum *int32  `json:"exclusiveminimum,omitempty"`
	Pattern          string  `json:"pattern,omitempty"`

	Parent *Schema `json:"-"`
	Key    string  `json:"-"`
}

Schema the base JSON Schema

func (*Schema) AllDefinitions

func (s *Schema) AllDefinitions() map[string]*Schema

AllDefinitions returns a merged map of the definitions and $def fields, $def takes precedences

func (*Schema) Base

func (s *Schema) Base() *Schema

Base finds the root/base schema from any point in the schema hierarchy

func (*Schema) SetParent

func (s *Schema) SetParent(key string, parent *Schema)

SetParent recursively traverse the schema setting any parents and keys

func (*Schema) Stat

func (s *Schema) Stat() (DataType, Reference, []*Schema, []*Schema, []*Schema, []string)

Stat returns the main keywords of the schema to workout how to process it

func (*Schema) UnmarshalJSON

func (s *Schema) UnmarshalJSON(b []byte) error

type SchemaVersion

type SchemaVersion string

SchemaVersion declares which version of the JSON Schema standard that the schema was written against

const (
	// Draft07 is not really supported
	Draft07 SchemaVersion = "http://json-schema.org/draft-07/schema"
	// Draft08 old name not really supported
	Draft08 SchemaVersion = "http://json-schema.org/draft-08/schema"
	// IETF_2019_19 formerly known as Draft 8
	IETF_2019_19 SchemaVersion = "https://json-schema.org/2019-09/schema"
)

func (*SchemaVersion) UnmarshalJSON

func (s *SchemaVersion) UnmarshalJSON(b []byte) error

Directories

Path Synopsis
sample_schemas
allOf
Code generated by jsonschema.
Code generated by jsonschema.
allOfObject
Code generated by jsonschema.
Code generated by jsonschema.
anyOf
Code generated by jsonschema.
Code generated by jsonschema.
basic
Code generated by jsonschema.
Code generated by jsonschema.
enum
Code generated by jsonschema.
Code generated by jsonschema.
nesting
Code generated by jsonschema.
Code generated by jsonschema.
oneOf
Code generated by jsonschema.
Code generated by jsonschema.
reference
Code generated by jsonschema.
Code generated by jsonschema.
referenceOutside
Code generated by jsonschema.
Code generated by jsonschema.
spec

Jump to

Keyboard shortcuts

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