jsonvalidator

package module
v0.0.0-...-21fa4be Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2020 License: MIT Imports: 9 Imported by: 0

README

go-jsonvalidator

A Golang package for validating json data against json schema.

Documentation

Index

Constants

View Source
const (
	TYPE_OBJECT  = "object"
	TYPE_ARRAY   = "array"
	TYPE_STRING  = "string"
	TYPE_NUMBER  = "number"
	TYPE_INTEGER = "integer"
	TYPE_BOOLEAN = "boolean"
	TYPE_NULL    = "null"
)

Valid Json Schema types

View Source
const (
	ENCODING_7BIT             = "7bit"
	ENCODING_8bit             = "8bit"
	ENCODING_BINARY           = "binary"
	ENCODING_QUOTED_PRINTABLE = "quoted-printable"
	ENCODING_BASE64           = "base64"
)

Valid values for "contentEncoding" field

View Source
const (
	FORMAT_DATE_TIME             = "date-time"
	FORMAT_TIME                  = "time"
	FORMAT_DATE                  = "date"
	FORMAT_EMAIL                 = "email"
	FORMAT_IDN_EMAIL             = "idn-email"
	FORMAT_HOSTNAME              = "hostname"
	FORMAT_IDN_HOSTNAME          = "idn-hostname"
	FORMAT_IPV4                  = "ipv4"
	FORMAT_IPV6                  = "ipv6"
	FORMAT_URI                   = "uri"
	FORMAT_URI_REFERENCE         = "uri-reference"
	FORMAT_IRI                   = "iri"
	FORMAT_IRI_REFERENCE         = "iri-reference"
	FORMAT_URI_TEMPLATE          = "uri-template"
	FORMAT_JSON_POINTER          = "json-pointer"
	FORMAT_RELATIVE_JSON_POINTER = "relative-json-pointer"
	FORMAT_REGEX                 = "regex"
)

Valid values for "format" fields

Variables

This section is empty.

Functions

This section is empty.

Types

type InvalidDraftError

type InvalidDraftError string

func (InvalidDraftError) Error

func (e InvalidDraftError) Error() string

type InvalidReferenceError

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

func (InvalidReferenceError) Error

func (e InvalidReferenceError) Error() string

type JsonSchema

type JsonSchema struct {
	// RejectAll is ***not*** a json schema keyword!
	// It is an internal flag for internal use that represents a json schema
	// that suppose to reject all json values.
	// If it is true, all other field will be ignored and validateJsonData()
	// will always return false.
	RejectAll bool `json:"rejectAll,omitempty"`

	// The $schema keyword is used to declare that a JSON fragment is
	// actually a piece of JSON Schema.
	Schema *schema `json:"$schema,omitempty"`

	// The value of $ref is a URI, and the part after # sign is in a format
	// called JSON Pointer.
	Ref *ref `json:"$ref,omitempty"`

	// The $id property is a URI that serves two purposes:
	// It declares a unique identifier for the schema
	// It declares a base URI against which $ref URIs are resolved.
	Id *id `json:"$id,omitempty"`

	// The $comment keyword is strictly intended for adding comments
	// to the JSON schema source. Its value must always be a string.
	Comment *comment `json:"$comment,omitempty"`

	// Title and Description used to describe the schema and not used for
	// validation.
	Title       *title       `json:"title,omitempty"`
	Description *description `json:"description,omitempty"`

	// The value of this keyword MUST be either a string or an array. If it is
	// an array, elements of the array MUST be strings and MUST be unique.
	// String values MUST be one of the six primitive types
	// ("null", "boolean", "object", "array", "number", or "string"),
	// or "integer" which matches any number with a zero fractional part.
	Type *_type `json:"type,omitempty"`

	// The default keyword specifies a default value for an item.
	Default _default `json:"default,omitempty"`

	// The examples keyword is a place to provide an array of examples
	// that validate against the schema.
	Examples examples `json:"examples,omitempty"`

	// The value of this keyword MUST be an array.
	// An instance validates successfully against this keyword if its value is
	// equal to one of the elements in this keyword's array value.
	Enum enum `json:"enum,omitempty"`

	// The value of this keyword MAY be of any type, including null.
	// An instance validates successfully against this keyword if its value is
	// equal to the value of the keyword.
	Const *_const `json:"const,omitempty"`

	// The "definitions" keywords provides a standardized location for schema
	// authors to inline re-usable JSON Schemas into a more general schema. The
	// keyword does not directly affect the validation result.
	// This keyword's value MUST be an object. Each member value of this
	// object MUST be a valid JSON Schema.
	Definitions definitions `json:"definitions,omitempty"`

	// The value of "properties" MUST be an object. Each value of this object
	// MUST be a valid JSON Schema.
	// This keyword determines how child instances validate for objects, and
	// does not directly validate the immediate instance itself.
	// Validation succeeds if, for each name that appears in both the instance
	// and as a name within this keyword's value, the child instance for that
	// name successfully validates against the corresponding schema.
	Properties properties `json:"properties,omitempty"`

	// The value of "additionalProperties" MUST be a valid JSON Schema.
	// This keyword determines how child instances validate for objects,
	// and does not directly validate the immediate instance itself.
	// Validation with "additionalProperties" applies only to the child values
	// of instance names that do not match any names in "properties", and do
	// not match any regular expression in "patternProperties".
	// For all such properties, validation succeeds if the child instance
	// validates against the "additionalProperties" schema.
	AdditionalProperties *additionalProperties `json:"additionalProperties,omitempty"`

	// The value of this keyword MUST be an array. Elements of this array,
	// if any, MUST be strings, and MUST be unique.
	// An object instance is valid against this keyword if every item in the
	// array is the name of a property in the instance.
	Required required `json:"required,omitempty"`

	// The value of "propertyNames" MUST be a valid JSON Schema.
	// If the instance is an object, this keyword validates if every property
	// name in the instance validates against the provided schema. Note the
	// property name that the schema is testing will always be a string
	PropertyNames *propertyNames `json:"propertyNames,omitempty"`

	// This keyword specifies rules that are evaluated if the instance is an
	// object and contains a certain property.
	// This keyword's value MUST be an object. Each property specifies a
	// dependency. Each dependency value MUST be an array or a valid JSON
	// Schema.
	// If the dependency value is a subschema, and the dependency key is a
	// property in the instance, the entire instance must validate against the
	// dependency value.
	// If the dependency value is an array, each element in the array, if any,
	// MUST be a string, and MUST be unique. If the dependency key is a
	// property in the instance, each of the items in the dependency value
	// must be a property that exists in the instance.
	Dependencies dependencies `json:"dependencies,omitempty"`

	// The value of "patternProperties" MUST be an object. Each property name
	// of this object SHOULD be a valid regular expression, according to the
	// ECMA 262 regular expression dialect. Each property value of this object
	// MUST be a valid JSON Schema.
	// This keyword determines how child instances validate for objects, and
	// does not directly validate the immediate instance itself. Validation of
	// the primitive instance type against this keyword always succeeds.
	// Validation succeeds if, for each instance name that matches any regular
	// expressions that appear as a property name in this keyword's value, the
	// child instance for that name successfully validates against each schema
	// that corresponds to a matching regular expression.
	PatternProperties patternProperties `json:"patternProperties,omitempty"`

	// The value of "items" MUST be either a valid JSON Schema or an array of
	// valid JSON Schemas.
	// This keyword determines how child instances validate for arrays, and
	// does not directly validate the immediate instance itself.
	// If "items" is a schema, validation succeeds if all elements in the array
	// successfully validate against that schema.
	// If "items" is an array of schemas, validation succeeds if each element
	// of the instance validates against the schema at the same position,
	// if any.
	Items items `json:"items,omitempty"`

	// The value of this keyword MUST be a valid JSON Schema.
	// An array instance is valid against "contains" if at least one of its
	// elements is valid against the given schema. Note that when collecting
	// annotations, the subschema MUST be applied to every array element even
	// after the first match has been found. This is to ensure that all
	// possible annotations are collected.
	Contains *contains `json:"contains,omitempty"`

	// The value of "additionalItems" MUST be a valid JSON Schema.
	// This keyword determines how child instances validate for arrays, and
	// does not directly validate the immediate instance itself.
	// If "items" is an array of schemas, validation succeeds if every
	// instance element at a position greater than the size of "items"
	// validates against "additionalItems".
	// Otherwise, "additionalItems" MUST be ignored, as the "items" schema
	// (possibly the default value of an empty schema) is applied to all
	// elements.
	AdditionalItems *additionalItems `json:"additionalItems,omitempty"`

	// array limitations
	MinItems    *minItems    `json:"minItems,omitempty"`
	MaxItems    *maxItems    `json:"maxItems,omitempty"`
	UniqueItems *uniqueItems `json:"uniqueItems,omitempty"`

	// string limitations
	MinLength *minLength `json:"minLength,omitempty"`
	MaxLength *maxLength `json:"maxLength,omitempty"`
	Pattern   *pattern   `json:"pattern,omitempty"`
	Format    *format    `json:"format,omitempty"`

	// integer/number limitations
	MultipleOf       *multipleOf       `json:"multipleOf,omitempty"`
	Minimum          *minimum          `json:"minimum,omitempty"`
	Maximum          *maximum          `json:"maximum,omitempty"`
	ExclusiveMinimum *exclusiveMinimum `json:"exclusiveMinimum,omitempty"`
	ExclusiveMaximum *exclusiveMaximum `json:"exclusiveMaximum,omitempty"`

	// object size limitations
	MinProperties *minProperties `json:"minProperties,omitempty"`
	MaxProperties *maxProperties `json:"maxProperties,omitempty"`

	// The contentMediaType keyword specifies the MIME type of the contents
	// of a string.
	ContentMediaType *contentMediaType `json:"contentMediaType,omitempty"`

	// The contentEncoding keyword specifies the encoding used to store
	// the contents.
	ContentEncoding *contentEncoding `json:"contentEncoding,omitempty"`

	// Must be valid against any of the sub-schemas.
	AnyOf anyOf `json:"anyOf,omitempty"`

	// Must be valid against all of the sub-schemas.
	AllOf allOf `json:"allOf,omitempty"`

	// Must be valid against exactly one of the sub-schemas.
	OneOf oneOf `json:"oneOf,omitempty"`

	// Must not be valid against the given schema.
	Not *not `json:"not,omitempty"`

	// The if, then and else keywords allow the application of a sub-schema
	// based on the outcome of another schema.
	If   *_if   `json:"if,omitempty"`
	Then *_then `json:"then,omitempty"`
	Else *_else `json:"else,omitempty"`

	// If "readOnly" has a value of boolean true, it indicates that the value
	// of the instance is managed exclusively by the owning authority, and
	// attempts by an application to modify the value of this property are
	// expected to be ignored or rejected by that owning authority.
	ReadOnly *readOnly `json:"readOnly,omitempty"`

	// If "writeOnly" has a value of boolean true, it indicates that the value
	// is never present when the instance is retrieved from the owning
	// authority.
	// It can be present when sent to the owning authority to update or create
	// the document (or the resource it represents), but it will not be
	// included in any updated or newly created version of the instance.
	WriteOnly *writeOnly `json:"writeOnly,omitempty"`
}

func NewJsonSchema

func NewJsonSchema(bytes []byte) (*JsonSchema, error)

NewJsonSchema created a new JsonSchema instance, Unmarshals the byte array into the instance, and return the instance.

func (*JsonSchema) UnmarshalJSON

func (js *JsonSchema) UnmarshalJSON(bytes []byte) error

type KeywordValidationError

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

func (KeywordValidationError) Error

func (e KeywordValidationError) Error() string

type RootJsonSchema

type RootJsonSchema struct {
	JsonSchema
	// contains filtered or unexported fields
}

RootJsonSchema is struct that contains a JsonSchema embedded into it (and therefore inherits all JsonSchema's methods) and a map of json path and a pointer to JsonSchema instance called subSchemaMap. subSchemaMap holds a record for each sub-schema that the root-schema contains.

func NewRootJsonSchema

func NewRootJsonSchema(bytes []byte) (*RootJsonSchema, error)

NewJsonSchema creates a new RootJsonSchema instance, Unmarshals the byte array into the instance, and returns a pointer to the instance.

type SchemaCompilationError

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

func (SchemaCompilationError) Error

func (e SchemaCompilationError) Error() string

type SchemaValidationError

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

func (SchemaValidationError) Error

func (e SchemaValidationError) Error() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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