jsonschema

package module
v0.0.0-...-9a691b8 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2025 License: Apache-2.0 Imports: 16 Imported by: 0

README

jsonschema

Go Reference codecov

jsonschema is a JSON Schema Draft 4 validator implementation

Usage

package example

import (
	"fmt"

	"github.com/tdakkota/jsonschema"
)

func Example() {
	schema, err := jsonschema.Parse([]byte(`{
      "type": "object",
      "properties": {
        "number": { "type": "number" },
        "street_name": { "type": "string" },
        "street_type": { "enum": ["Street", "Avenue", "Boulevard"] }
      }
    }`))
	if err != nil {
		panic(err)
	}

	if err := schema.Validate(
		[]byte(`{ "number": 1600, "street_name": "Pennsylvania", "street_type": "Avenue" }`),
	); err != nil {
		panic(err)
	}

	fmt.Println(schema.Validate([]byte(`{"number": "1600"}`)))
	// Output:
	// object: "number": string: type is not allowed
}

Install

go get -d github.com/tdakkota/jsonschema

Roadmap

See this issue.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AdditionalItems

type AdditionalItems = rawAdditional

AdditionalItems is JSON Schema additionalItems validator description.

type AdditionalProperties

type AdditionalProperties = rawAdditional

AdditionalProperties is JSON Schema additionalProperties validator description.

type Dependencies

type Dependencies struct {
	Required map[string][]string
	Schemas  map[string]RawSchema
}

Dependencies is unparsed JSON Schema dependencies validator description.

func (Dependencies) MarshalJSON

func (r Dependencies) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (*Dependencies) UnmarshalJSON

func (r *Dependencies) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

type Items

type Items struct {
	Array   bool // If set, "items" defined as array.
	Schema  RawSchema
	Schemas []RawSchema
}

Items is JSON Schema items validator description.

func (Items) MarshalJSON

func (p Items) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (*Items) UnmarshalJSON

func (p *Items) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

type NoRemote

type NoRemote struct {
}

NoRemote is no-op implementation of RemoteResolver. Always returns error.

func (NoRemote) Resolve

func (n NoRemote) Resolve(ctx context.Context, loc string) ([]byte, error)

Resolve implements RemoteResolver.

type Num

type Num jx.Num

Num represents JSON number.

func (Num) MarshalJSON

func (n Num) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (*Num) UnmarshalJSON

func (n *Num) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

type RawPatternProperties

type RawPatternProperties []RawPatternProperty

RawPatternProperties is unparsed JSON Schema patternProperties validator description.

func (RawPatternProperties) MarshalJSON

func (r RawPatternProperties) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (*RawPatternProperties) UnmarshalJSON

func (r *RawPatternProperties) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

type RawPatternProperty

type RawPatternProperty struct {
	Pattern string
	Schema  RawSchema
}

RawPatternProperty is item of RawPatternProperties.

type RawProperties

type RawProperties []RawProperty

RawProperties is unparsed JSON Schema properties validator description.

func (RawProperties) MarshalJSON

func (p RawProperties) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (*RawProperties) UnmarshalJSON

func (p *RawProperties) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

type RawProperty

type RawProperty struct {
	Name   string
	Schema RawSchema
}

RawProperty is item of RawProperties.

type RawSchema

type RawSchema struct {
	ID     string            `json:"id,omitempty"` // TODO(tdakkota): get id field name from draft struct
	Ref    string            `json:"$ref,omitempty"`
	Type   SchemaType        `json:"type,omitempty"`
	Format string            `json:"format,omitempty"`
	Enum   []json.RawMessage `json:"enum,omitempty"`

	AllOf []RawSchema `json:"allOf,omitempty"`
	AnyOf []RawSchema `json:"anyOf,omitempty"`
	OneOf []RawSchema `json:"oneOf,omitempty"`
	Not   *RawSchema  `json:"not,omitempty"`

	MinProperties        *uint64               `json:"minProperties,omitempty"`
	MaxProperties        *uint64               `json:"maxProperties,omitempty"`
	Required             []string              `json:"required,omitempty"`
	Properties           RawProperties         `json:"properties,omitempty"`
	PatternProperties    RawPatternProperties  `json:"patternProperties,omitempty"`
	AdditionalProperties *AdditionalProperties `json:"additionalProperties,omitempty"`
	Dependencies         Dependencies          `json:"dependencies,omitempty"`

	MinItems        *uint64          `json:"minItems,omitempty"`
	MaxItems        *uint64          `json:"maxItems,omitempty"`
	UniqueItems     bool             `json:"uniqueItems,omitempty"`
	Items           *Items           `json:"items,omitempty"`
	AdditionalItems *AdditionalItems `json:"additionalItems,omitempty"`

	Minimum          Num  `json:"minimum,omitempty"`
	ExclusiveMinimum bool `json:"exclusiveMinimum,omitempty"`
	Maximum          Num  `json:"maximum,omitempty"`
	ExclusiveMaximum bool `json:"exclusiveMaximum,omitempty"`
	MultipleOf       Num  `json:"multipleOf,omitempty"`

	MaxLength *uint64 `json:"maxLength,omitempty"`
	MinLength *uint64 `json:"minLength,omitempty"`
	Pattern   string  `json:"pattern,omitempty"`
}

RawSchema is unparsed JSON Schema.

type Remote

type Remote struct {
	HTTPClient    *http.Client
	AllowRelative bool
}

Remote is built-in implementation of RemoteResolver.

func (Remote) Resolve

func (n Remote) Resolve(ctx context.Context, loc string) ([]byte, error)

Resolve implements RemoteResolver.

type RemoteResolver

type RemoteResolver interface {
	Resolve(ctx context.Context, loc string) ([]byte, error)
}

RemoteResolver resolves remote references.

type Schema

type Schema struct {
	// contains filtered or unexported fields
}

Schema is a parsed schema structure.

func Parse

func Parse(data []byte) (*Schema, error)

Parse parses given JSON and compiles JSON Schema validator.

Example
package main

import (
	"fmt"

	"github.com/tdakkota/jsonschema"
)

func main() {
	schema, err := jsonschema.Parse([]byte(`{
  "type": "object",
  "properties": {
    "number": { "type": "number" },
    "street_name": { "type": "string" },
    "street_type": { "enum": ["Street", "Avenue", "Boulevard"] }
  }
}`))
	if err != nil {
		panic(err)
	}

	if err := schema.Validate(
		[]byte(`{ "number": 1600, "street_name": "Pennsylvania", "street_type": "Avenue" }`),
	); err != nil {
		panic(err)
	}

	fmt.Println(schema.Validate([]byte(`{"number": "1600"}`)))
}
Output:
object: "number": string: type is not allowed

func (*Schema) Validate

func (s *Schema) Validate(data []byte) error

Validate validates given data.

type SchemaType

type SchemaType []string

SchemaType represents JSON Schema type list.

func (*SchemaType) UnmarshalJSON

func (r *SchemaType) UnmarshalJSON(data []byte) error

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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