jsonschema

package module
v0.0.0-...-e0d73b5 Latest Latest
Warning

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

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

README

go-jsonschema

A JSON schema code generator for Go.

JSON schema draft 2020-12 is supported.

Usage

jsonschemagen -s <schema> -o <output>

One Go type per definition will be generated.

  • int64 is used for "type": "integer".
  • json.Number is used for "type": "number".
  • Go structs are generated for objects with "additionalProperties": false.
  • json.RawMessage is used when a value can have multiple types. Helpers are generated for allOf, anyOf, oneOf, then, else and dependantSchemas which are references.

Contributing

Report bugs and send patches to the mailing list. Discuss in #emersion on Libera Chat.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Schema

type Schema struct {
	// Core
	Schema     string            `json:"$schema"`
	Vocabulary map[string]bool   `json:"$vocabulary"`
	ID         string            `json:"$id"`
	Ref        string            `json:"$ref"`
	DynamicRef string            `json:"$dynamicRef"`
	Defs       map[string]Schema `json:"$defs"`
	Comment    string            `json:"$comment"`

	// Applying subschemas with logic
	AllOf []Schema `json:"allOf"`
	AnyOf []Schema `json:"anyOf"`
	OneOf []Schema `json:"oneOf"`
	Not   []Schema `json:"not"`

	// Applying subschemas conditionally
	If               *Schema           `json:"if"`
	Then             *Schema           `json:"then"`
	Else             *Schema           `json:"else"`
	DependentSchemas map[string]Schema `json:"dependentSchemas"`

	// Applying subschemas to arrays
	PrefixItems []Schema `json:"prefixItems"`
	Items       *Schema  `json:"items"`
	Contains    *Schema  `json:"contains"`

	// Applying subschemas to objects
	Properties           map[string]Schema `json:"properties"`
	PatternProperties    map[string]Schema `json:"patternProperties"`
	AdditionalProperties *Schema           `json:"additionalProperties"`
	PropertyNames        *Schema           `json:"propertyNames"`

	// Validation
	Type  TypeSet       `json:"type"`
	Enum  []interface{} `json:"enum"`
	Const interface{}   `json:"const"`

	// Validation for numbers
	MultipleOf       json.Number `json:"multipleOf"`
	Maximum          json.Number `json:"maximum"`
	ExclusiveMaximum json.Number `json:"exclusiveMaximum"`
	Minimum          json.Number `json:"minimum"`
	ExclusiveMinimum json.Number `json:"exclusiveMinimum"`

	// Validation for strings
	MaxLength int    `json:"maxLength"`
	MinLength int    `json:"minLength"`
	Pattern   string `json:"pattern"`

	// Validation for arrays
	MaxItems    int  `json:"maxItems"`
	MinItems    int  `json:"minItems"`
	UniqueItems bool `json:"uniqueItems"`
	MaxContains int  `json:"maxContains"`
	MinContains int  `json:"minContains"`

	// Validation for objects
	MaxProperties     int                 `json:"maxProperties"`
	MinProperties     int                 `json:"minProperties"`
	Required          []string            `json:"required"`
	DependentRequired map[string][]string `json:"dependentRequired"`

	// Basic metadata annotations
	Title       string        `json:"title"`
	Description string        `json:"description"`
	Default     interface{}   `json:"default"`
	Deprecated  bool          `json:"deprecated"`
	ReadOnly    bool          `json:"readOnly"`
	WriteOnly   bool          `json:"writeOnly"`
	Examples    []interface{} `json:"examples"`
}

func (*Schema) IsFalse

func (schema *Schema) IsFalse() bool

func (*Schema) IsTrue

func (schema *Schema) IsTrue() bool

func (*Schema) UnmarshalJSON

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

type Type

type Type string
const (
	TypeNull    Type = "null"
	TypeBoolean Type = "boolean"
	TypeObject  Type = "object"
	TypeArray   Type = "array"
	TypeNumber  Type = "number"
	TypeString  Type = "string"
	TypeInteger Type = "integer"
)

type TypeSet

type TypeSet []Type

func (*TypeSet) UnmarshalJSON

func (ts *TypeSet) UnmarshalJSON(b []byte) error

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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