Documentation
¶
Overview ¶
Package schema provides tools for defining, validating, and manipulating JSON-Schema-like structures in Go.
Index ¶
- Constants
- func Index(token string, maxLengths ...int) (int, bool)
- func SetProperty(element Element, object any, path string, value any) (err error)
- func UseFormat(name string, fn format.Generator)
- func Validate(schema Schema, value any) (any, bool, error)
- type Any
- func (element Any) AllProperties() ElementMap
- func (element Any) DefaultValue() any
- func (element Any) GetElement(_ string) (Element, bool)
- func (element Any) Inherit(_ Element)
- func (element Any) IsRequired() bool
- func (element Any) MarshalMap() map[string]any
- func (element *Any) UnmarshalMap(data map[string]any) error
- func (element Any) Validate(_ any) error
- func (element Any) ValidateRequiredIf(schema Schema, path list.List, value any) error
- type AnyGetter
- type AnySetter
- type Array
- func (element Array) AllProperties() ElementMap
- func (element Array) Append(value ArraySetter, item any) error
- func (Array) DefaultValue() any
- func (element Array) GetElement(name string) (Element, bool)
- func (Array) GetIndex(value any, index int) (any, bool)
- func (Array) GetLength(value any) (int, bool)
- func (Array) Inherit(_ Element)
- func (element Array) IsRequired() bool
- func (element Array) MarshalMap() map[string]any
- func (Array) SetIndex(value any, index int, item any) bool
- func (element *Array) UnmarshalMap(data map[string]any) error
- func (element Array) ValidateRequiredIf(schema Schema, path list.List, globalValue any) error
- type ArrayGetter
- type ArrayGetterSetter
- type ArraySetter
- type BoolGetter
- type BoolSetter
- type Boolean
- func (element Boolean) AllProperties() ElementMap
- func (element Boolean) DefaultValue() any
- func (element Boolean) GetElement(name string) (Element, bool)
- func (Boolean) Inherit(_ Element)
- func (element Boolean) IsRequired() bool
- func (element Boolean) MarshalJSON() ([]byte, error)
- func (element Boolean) MarshalMap() map[string]any
- func (element *Boolean) UnmarshalMap(data map[string]any) error
- func (element Boolean) Validate(object any) error
- func (element Boolean) ValidateRequiredIf(schema Schema, path list.List, globalValue any) error
- type Element
- type ElementMap
- type Enumerator
- type FloatGetter
- type FloatSetter
- type Int64Getter
- type Int64Setter
- type IntGetter
- type IntSetter
- type Integer
- func (element Integer) AllProperties() ElementMap
- func (element Integer) DefaultValue() any
- func (element Integer) Enumerate() []string
- func (element Integer) GetElement(name string) (Element, bool)
- func (element Integer) Inherit(_ Element)
- func (element Integer) IsRequired() bool
- func (element Integer) MarshalMap() map[string]any
- func (element *Integer) UnmarshalMap(data map[string]any) error
- func (element Integer) Validate(value any) error
- func (element Integer) ValidateRequiredIf(schema Schema, path list.List, globalValue any) error
- type KeysGetter
- type LengthGetter
- type Nullable
- type Number
- func (element Number) AllProperties() ElementMap
- func (element Number) DefaultValue() any
- func (element Number) Enumerate() []string
- func (element Number) GetElement(name string) (Element, bool)
- func (element Number) Inherit(_ Element)
- func (element Number) IsRequired() bool
- func (element Number) MarshalMap() map[string]any
- func (element *Number) UnmarshalMap(data map[string]any) error
- func (element Number) Validate(value any) error
- func (element Number) ValidateRequiredIf(schema Schema, path list.List, globalValue any) error
- type Object
- func (element Object) AllProperties() ElementMap
- func (element Object) DefaultValue() any
- func (element Object) GetElement(name string) (Element, bool)
- func (element Object) Inherit(parent Element)
- func (element Object) IsRequired() bool
- func (element Object) MarshalMap() map[string]any
- func (element *Object) UnmarshalMap(data map[string]any) error
- func (element Object) ValidateRequiredIf(schema Schema, path list.List, globalValue any) error
- type ObjectSetter
- type PointerGetter
- type Remover
- type Schema
- func (schema *Schema) AllProperties() ElementMap
- func (schema Schema) Append(object any, path string, value any) error
- func (schema Schema) Get(object any, path string) (any, error)
- func (schema Schema) GetArrayElement(path string) (Array, bool)
- func (schema Schema) GetBooleanElement(path string) (Boolean, bool)
- func (schema Schema) GetElement(path string) (Element, bool)
- func (schema Schema) GetIntegerElement(path string) (Integer, bool)
- func (schema Schema) GetNumberElement(path string) (Number, bool)
- func (schema Schema) GetObjectElement(path string) (Object, bool)
- func (schema Schema) GetStringElement(path string) (String, bool)
- func (schema *Schema) Inherit(parent Schema)
- func (schema Schema) MarshalJSON() ([]byte, error)
- func (schema Schema) MarshalMap() map[string]any
- func (schema Schema) Match(value any, expression exp.Expression) (bool, error)
- func (schema Schema) Remove(object any, path string) bool
- func (schema Schema) Set(object any, path string, value any) error
- func (schema Schema) SetAll(object any, values map[string]any) error
- func (schema Schema) SetURLValues(object any, values url.Values) error
- func (schema *Schema) UnmarshalJSON(data []byte) error
- func (schema *Schema) UnmarshalMap(data map[string]any) error
- func (schema Schema) Validate(value any) (bool, error)
- func (schema Schema) ValidateRequiredIf(value any) error
- type String
- func (element String) AllProperties() ElementMap
- func (element String) DefaultValue() any
- func (element String) Enumerate() []string
- func (element String) GetElement(name string) (Element, bool)
- func (String) Inherit(_ Element)
- func (element String) IsRequired() bool
- func (element String) MarshalMap() map[string]any
- func (element *String) UnmarshalMap(data map[string]any) error
- func (element String) Validate(value any) error
- func (element String) ValidateRequiredIf(schema Schema, path list.List, globalValue any) error
- type StringGetter
- type StringSetter
- type Type
- type ValueGetter
- type ValueSetter
- type WritableElement
Constants ¶
const TypeAny = Type("any")
TypeAny is the token used by JSON-Schema to designate that any kind of data
const TypeArray = Type("array")
TypeArray is the token used by JSON-Schema to designate that a schema describes an array.
const TypeBoolean = Type("boolean")
TypeBoolean is the token used by JSON-Schema to designate that a schema describes an boolean.
const TypeInteger = Type("integer")
TypeInteger is the token used by JSON-Schema to designate that a schema describes an integer.
const TypeNumber = Type("number")
TypeNumber is the token used by JSON-Schema to designate that a schema describes an number.
const TypeObject = Type("object")
TypeObject is the token used by JSON-Schema to designate that a schema describes an object.
const TypeString = Type("string")
TypeString is the token used by JSON-Schema to designate that a schema describes an string.
Variables ¶
This section is empty.
Functions ¶
func Index ¶ added in v0.10.0
Index converts a string into an array index that is bounded by zero and the maximum value provided. It returns the final index and a boolean that is TRUE if the index was converted successfully, and FALSE if it was truncated.
func SetProperty ¶ added in v0.26.0
SetProperty sets the value at the specified path within the object according to the provided schema
func UseFormat ¶
UseFormat adds a custom FormatFunc function to this library. Used to register custom validators.
UseFormat is only safe to call during program startup (e.g. from init), before any schema validation reads the registry. A call that arrives after the registry has been read is ignored and reported, because mutating the map concurrently with validation reads would be a data race.
Types ¶
type Any ¶
Any represents a schema that accepts any data type
func (Any) AllProperties ¶ added in v0.22.0
func (element Any) AllProperties() ElementMap
AllProperties implements the Element interface It returns a map of all properties for this element
func (Any) DefaultValue ¶ added in v0.10.0
DefaultValue returns the default value for this element
func (Any) GetElement ¶ added in v0.12.0
GetElement implements the Element interface It returns the element at the specified path
func (Any) Inherit ¶ added in v0.12.0
Inherit implements the Element interface It is a no-op for Any elements
func (Any) IsRequired ¶
IsRequired returns true if this a value is required for this element
func (Any) MarshalMap ¶
MarshalMap populates the object data into a map[string]any
func (*Any) UnmarshalMap ¶
UnmarshalMap tries to populate this object using data from a map[string]any
type AnyGetter ¶ added in v0.22.0
type AnyGetter interface {
// GetAnyOK gets the property with the specified name
GetAnyOK(string) (any, bool)
}
AnyGetter allows an object to get a property of any type element by name
type AnySetter ¶ added in v0.22.0
type AnySetter interface {
// SetAny sets the property with the specified name
SetAny(string, any) bool
}
AnySetter allows an object to set a property of any type element by name
type Array ¶
type Array struct {
Items Element `json:"items"`
MinLength int `json:"minLength"`
MaxLength int `json:"maxLength"`
Required bool `json:"required"`
RequiredIf string `json:"required-if"`
}
Array represents an array data type within a JSON-Schema.
func (Array) AllProperties ¶ added in v0.22.0
func (element Array) AllProperties() ElementMap
AllProperties implements the Element interface It returns a map of all properties for this element
func (Array) Append ¶ added in v0.24.0
func (element Array) Append(value ArraySetter, item any) error
Append adds a new item to the end of the array (if the object implements ArraySetter)
func (Array) DefaultValue ¶ added in v0.6.0
DefaultValue implements the Element interface It returns the default value for this element type
func (Array) GetElement ¶ added in v0.6.0
GetElement implements the Element interface It returns the element at the specified path
func (Array) GetIndex ¶ added in v0.22.0
GetIndex returns the value at a specific index in the array (if the object implements ArrayGetter)
func (Array) GetLength ¶ added in v0.22.0
GetLength returns the length of the array value (if the object implements ArrayGetter)
func (Array) Inherit ¶ added in v0.12.0
Inherit implements the Element interface It is a no-op for Array elements
func (Array) IsRequired ¶
IsRequired implements the Element interface It returns TRUE if this element is a required field
func (Array) MarshalMap ¶
MarshalMap populates object data into a map[string]any
func (Array) SetIndex ¶ added in v0.22.0
SetIndex sets the value at a specific index in the array (if the object implements ArraySetter)
func (*Array) UnmarshalMap ¶
UnmarshalMap tries to populate this object using data from a map[string]any
type ArrayGetter ¶ added in v0.22.0
type ArrayGetter interface {
// Length returns the length of the array
Length() int
// GetIndex gets the value at the specified index
GetIndex(int) (any, bool)
}
ArrayGetter allows an object to get a value at a specific index in an array
type ArrayGetterSetter ¶ added in v0.26.0
type ArrayGetterSetter interface {
ArrayGetter
ArraySetter
}
ArrayGetterSetter combines the ArrayGetter and ArraySetter interfaces, for objects that can both get and set array values.
type ArraySetter ¶ added in v0.22.0
type ArraySetter interface {
// SetIndex sets the value at the specified index
SetIndex(int, any) bool
// Length returns the length of the array
Length() int
}
ArraySetter allows an object to set a value at a specific index in an array
type BoolGetter ¶ added in v0.7.0
type BoolGetter interface {
// GetBoolOK gets the boolean property with the specified name
GetBoolOK(string) (bool, bool)
}
BoolGetter allows an object to get a boolean property by name
type BoolSetter ¶ added in v0.7.0
type BoolSetter interface {
// SetBool sets the boolean property with the specified name
SetBool(string, bool) bool
}
BoolSetter allows an object to set a boolean property by name
type Boolean ¶
type Boolean struct {
Default null.Bool `json:"default"`
Required bool `json:"required"`
RequiredIf string `json:"required-if"`
}
Boolean represents a boolean data type within a JSON-Schema.
func (Boolean) AllProperties ¶ added in v0.22.0
func (element Boolean) AllProperties() ElementMap
AllProperties returns a map of all properties for this element
func (Boolean) DefaultValue ¶ added in v0.6.0
DefaultValue is a part of the Element interface
func (Boolean) GetElement ¶ added in v0.6.0
GetElement implements the Element interface It returns the element at the specified path
func (Boolean) Inherit ¶ added in v0.12.0
Inherit implements the Element interface It is a no-op for Boolean elements
func (Boolean) IsRequired ¶
IsRequired is a part of the Element interface it returns TRUE if this element is a required field
func (Boolean) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface
func (Boolean) MarshalMap ¶
MarshalMap populates object data into a map[string]any
func (*Boolean) UnmarshalMap ¶
UnmarshalMap tries to populate this object using data from a map[string]any
type Element ¶
type Element interface {
// Default returns the default value for this element
DefaultValue() any
// IsRequired returns true if this a value is required for this element
IsRequired() bool
// ValidateRequiredIf handles conditional validation of a required field
ValidateRequiredIf(schema Schema, path list.List, globalValue any) error
// MarshalMap populates the object data into a map[string]any
MarshalMap() map[string]any
// getElement returns a named sub-element of this element, if it exists.
GetElement(string) (Element, bool)
Inherit(Element)
AllProperties() ElementMap
}
Element interface wraps all of the methods required for schema elements.
func UnmarshalJSON ¶
UnmarshalJSON tries to parse a []byte into a schema.Element
func UnmarshalMap ¶
UnmarshalMap tries to parse a map[string]any into a schema.Element
type ElementMap ¶
ElementMap is a map of string keys to Element values It is a shorthand for defining object properties
type Enumerator ¶
type Enumerator interface {
// Enumerate returns a list of string values representing the contents of this object
Enumerate() []string
}
Enumerator interface wraps the Enumerate method, that helps an object to return a list of string values representing its contents.
type FloatGetter ¶ added in v0.7.0
type FloatGetter interface {
// GetFloatOK gets the float64 property with the specified name
GetFloatOK(string) (float64, bool)
}
FloatGetter allows an object to get a float64 property by name
type FloatSetter ¶ added in v0.7.0
type FloatSetter interface {
// SetFloat sets the float64 property with the specified name
SetFloat(string, float64) bool
}
FloatSetter allows an object to set a float64 property by name
type Int64Getter ¶ added in v0.7.0
type Int64Getter interface {
// GetInt64OK gets the int64 property with the specified name
GetInt64OK(string) (int64, bool)
}
Int64Getter allows an object to get an int64 property by name
type Int64Setter ¶ added in v0.7.0
type Int64Setter interface {
// SetInt64 sets the int64 property with the specified name
SetInt64(string, int64) bool
}
Int64Setter allows an object to set an int64 property by name
type IntGetter ¶ added in v0.7.0
type IntGetter interface {
// GetIntOK gets the int property with the specified name
GetIntOK(string) (int, bool)
}
IntGetter allows an object to get an int property by name
type IntSetter ¶ added in v0.7.0
type IntSetter interface {
// SetInt sets the int property with the specified name
SetInt(string, int) bool
}
IntSetter allows an object to set an int property by name
type Integer ¶
type Integer struct {
Default null.Int64 `json:"default"`
Minimum null.Int64 `json:"minimum"`
Maximum null.Int64 `json:"maximum"`
MultipleOf null.Int64 `json:"multipleOf"`
BitSize int `json:"bitSize"`
Enum []int `json:"enum"`
Required bool `json:"required"`
RequiredIf string `json:"required-if"`
}
Integer represents an integer data type within a JSON-Schema.
func (Integer) AllProperties ¶ added in v0.22.0
func (element Integer) AllProperties() ElementMap
AllProperties returns a map of all properties for this element
func (Integer) DefaultValue ¶ added in v0.6.0
DefaultValue implements the Element interface It returns the default value for this element type
func (Integer) GetElement ¶ added in v0.6.0
GetElement implements the Element interface It returns the element at the specified path
func (Integer) Inherit ¶ added in v0.12.0
Inherit implements the Element interface It inherits properties from the parent element
func (Integer) IsRequired ¶
IsRequired implements the Element interface It returns TRUE if this element is a required field
func (Integer) MarshalMap ¶
MarshalMap populates object data into a map[string]any
func (*Integer) UnmarshalMap ¶
UnmarshalMap tries to populate this object using data from a map[string]any
type KeysGetter ¶ added in v0.22.0
type KeysGetter interface {
// Keys returns a list of the object's keys
Keys() []string
}
KeysGetter allows an object to return a list of its keys
type LengthGetter ¶ added in v0.10.0
type LengthGetter interface {
// Length returns the length of the array
Length() int
}
LengthGetter allows an object to return the length of an array
type Nullable ¶
type Nullable interface {
IsNull() bool
}
Nullable interface wraps the IsNull method, that helps an object to identify if it contains a null value or not. This mirrors the null.Nullable interface here, for convenience.
type Number ¶
type Number struct {
Default null.Float `json:"default"`
Minimum null.Float `json:"minimum"`
Maximum null.Float `json:"maximum"`
MultipleOf null.Float `json:"multipleOf"`
BitSize int `json:"bitSize"`
Enum []float64 `json:"enum"`
Required bool `json:"required"`
RequiredIf string `json:"required-if"`
}
Number represents a number data type within a JSON-Schema.
func (Number) AllProperties ¶ added in v0.22.0
func (element Number) AllProperties() ElementMap
AllProperties returns a map of all properties for this element
func (Number) DefaultValue ¶ added in v0.6.0
DefaultValue returns the default value for this element type
func (Number) GetElement ¶ added in v0.6.0
GetElement implements the Element interface It returns the element at the specified path
func (Number) Inherit ¶ added in v0.12.0
Inherit implements the Element interface It is a no-op for Number elements
func (Number) IsRequired ¶
IsRequired returns TRUE if this element is a required field
func (Number) MarshalMap ¶
MarshalMap populates object data into a map[string]any
func (*Number) UnmarshalMap ¶
UnmarshalMap tries to populate this object using data from a map[string]any
type Object ¶
type Object struct {
Properties ElementMap `json:"properties"`
Wildcard Element `json:"wildcard"`
Required bool `json:"required"`
RequiredIf string `json:"required-if"`
}
Object represents an object data type within a JSON-Schema.
func (Object) AllProperties ¶ added in v0.22.0
func (element Object) AllProperties() ElementMap
AllProperties implements the Element interface It returns a flat slice of all properties in this element (in this case, it returns all properties of this object)
func (Object) DefaultValue ¶ added in v0.6.0
DefaultValue implements the Element interface It returns the default value for this element type. In a special case for objects, which can be represented as both Go structs and maps, this returns a map[string]any that has been populated with any known default keys.
func (Object) GetElement ¶ added in v0.6.0
GetElement returns the element at the specified path within the object
func (Object) Inherit ¶ added in v0.12.0
Inherit implements the Element interface It inherits properties from the parent element
func (Object) IsRequired ¶
IsRequired implements the Element interface returns TRUE if this element is a required field
func (Object) MarshalMap ¶
MarshalMap populates object data into a map[string]any
func (*Object) UnmarshalMap ¶
UnmarshalMap tries to populate this object using data from a map[string]any
type ObjectSetter ¶ added in v0.10.0
type ObjectSetter interface {
// SetObject sets the child object at the specified path
SetObject(Element, list.List, any) error
}
ObjectSetter allows an object to set a child object by path
type PointerGetter ¶ added in v0.14.0
type PointerGetter interface {
// GetPointer gets a pointer to the child object with the specified name
GetPointer(string) (any, bool)
}
PointerGetter allows objects to return a pointer to a child object
type Remover ¶ added in v0.10.0
type Remover interface {
// Remove removes the child element with the specified name
Remove(string) bool
}
Remover allows an object to remove a child element by name
type Schema ¶
Schema defines a (simplified) JSON-Schema object, that can be Marshalled/Unmarshalled to JSON.
func Wildcard ¶ added in v0.22.0
func Wildcard() Schema
Wildcard returns a Schema that accepts any data type
func (*Schema) AllProperties ¶ added in v0.22.0
func (schema *Schema) AllProperties() ElementMap
AllProperties returns a flat slice of all properties in this schema
func (Schema) Append ¶ added in v0.24.0
Append appends a value to the array at the specified path within the object according to this schema
func (Schema) Get ¶
Get retrieves a generic value from the object. If the object is nil, Get still tries to return a default value if provided by the schema
func (Schema) GetArrayElement ¶ added in v0.22.0
GetArrayElement returns the array element at the specified path, or FALSE if invalid
func (Schema) GetBooleanElement ¶ added in v0.22.0
GetBooleanElement returns the boolean element at the specified path, or FALSE if invalide
func (Schema) GetElement ¶ added in v0.6.0
GetElement returns the element at the specified path, or FALSE if the element does not exist
func (Schema) GetIntegerElement ¶ added in v0.22.0
GetIntegerElement returns the integer element at the specified path, or FALSE if invalid
func (Schema) GetNumberElement ¶ added in v0.22.0
GetNumberElement returns the number element at the specified path, or FALSE if invalid
func (Schema) GetObjectElement ¶ added in v0.22.0
GetObjectElement returns the object element at the specified path, or FALSE if invalid
func (Schema) GetStringElement ¶ added in v0.22.0
GetStringElement returns the string element at the specified path, or FALSE if invalid
func (*Schema) Inherit ¶ added in v0.12.0
Inherit updates this schema with properties from the parent schema
func (Schema) MarshalJSON ¶
MarshalJSON converts a schema into JSON.
func (Schema) MarshalMap ¶
MarshalMap converts a schema into a map[string]any
func (Schema) Match ¶ added in v0.13.1
Match returns TRUE if the provided value (as accessed via this schema) matches the provided expression. This is useful for server-side data validation.
func (Schema) Set ¶
Set sets the value at the specified path within the object according to this schema
func (Schema) SetAll ¶
SetAll iterates over Set to apply all of the values to the object one at a time, stopping at the first error it encounters. If all values are added successfully, then SetAll also runs ValidateRequiredIf() to confirm that the object is still correct.
func (Schema) SetURLValues ¶ added in v0.24.3
SetURLValues iterates over Set to apply all of the values to the object one at a time, stopping at the first error it encounters. If all values are added successfully, then SetURLValues also runs ValidateRequiredIf() to confirm that the object is still correct.
func (*Schema) UnmarshalJSON ¶
UnmarshalJSON creates a new Schema object using a JSON-serialized byte array.
func (*Schema) UnmarshalMap ¶
UnmarshalMap updates a Schema using a map[string]any
func (Schema) Validate ¶
Validate checks the provided value against this schema, returning whether it was changed and any validation error.
func (Schema) ValidateRequiredIf ¶ added in v0.13.1
ValidateRequiredIf implements the Element interface It returns an error if the conditional expression is true but the value is empty
type String ¶
type String struct {
Default string `json:"default"`
MinLength int `json:"minLength"`
MaxLength int `json:"maxLength"`
Enum []string `json:"enum"`
MinValue string `json:"minValue"`
MaxValue string `json:"maxValue"`
Format string `json:"format"`
Required bool `json:"required"`
RequiredIf string `json:"required-if"`
}
String represents a string data type within a JSON-Schema.
func (String) AllProperties ¶ added in v0.22.0
func (element String) AllProperties() ElementMap
AllProperties returns a map of all properties for this element
func (String) DefaultValue ¶ added in v0.6.0
DefaultValue returns the default value for this element type
func (String) GetElement ¶ added in v0.6.0
GetElement implements the Element interface It returns the element at the specified path
func (String) Inherit ¶ added in v0.12.0
Inherit implements the Element interface It is a no-op for String elements
func (String) IsRequired ¶
IsRequired returns TRUE if this element is a required field
func (String) MarshalMap ¶
MarshalMap populates object data into a map[string]any
func (*String) UnmarshalMap ¶
UnmarshalMap tries to populate this object using data from a map[string]any
type StringGetter ¶ added in v0.7.0
type StringGetter interface {
// GetStringOK gets the string property with the specified name
GetStringOK(string) (string, bool)
}
StringGetter allows an object to get a string property by name
type StringSetter ¶ added in v0.7.0
type StringSetter interface {
// SetString sets the string property with the specified name
SetString(string, string) bool
}
StringSetter allows an object to set a string property by name
type ValueGetter ¶ added in v0.23.0
type ValueGetter interface {
// GetValue gets the entire value of the object
GetValue() any
}
ValueGetter allows an object to get its entire value
type ValueSetter ¶ added in v0.10.0
ValueSetter allows an object to set its entire value
Source Files
¶
- any.go
- array.go
- boolean.go
- element.go
- elementMap.go
- format.go
- index.go
- integer.go
- interfaces.go
- number.go
- object.go
- schema.go
- schemaType.go
- schema_append.go
- schema_get.go
- schema_remove.go
- schema_set.go
- string.go
- utils.go
- validate.go
- validate_any.go
- validate_array.go
- validate_boolean.go
- validate_integer.go
- validate_number.go
- validate_object.go
- validate_string.go