Documentation ¶
Index ¶
- Constants
- func UseFormat(name string, fn format.Function)
- type Array
- func (element Array) Clean(value any) error
- func (element Array) DefaultValue() any
- func (element Array) Get(object reflect.Value, path list.List) (reflect.Value, error)
- func (element Array) GetElement(path list.List) (Element, error)
- func (element Array) IsRequired() bool
- func (element Array) MarshalMap() map[string]any
- func (element Array) Remove(object reflect.Value, path list.List) (reflect.Value, error)
- func (element Array) Set(object reflect.Value, path list.List, value any) (reflect.Value, error)
- func (element Array) Type() reflect.Type
- func (element *Array) UnmarshalMap(data map[string]any) error
- func (element Array) Validate(value any) error
- type BoolGetter
- type BoolSetter
- type Boolean
- func (element Boolean) Clean(value any) error
- func (element Boolean) DefaultValue() any
- func (element Boolean) Get(object reflect.Value, path list.List) (reflect.Value, error)
- func (element Boolean) GetElement(path list.List) (Element, error)
- func (element Boolean) IsRequired() bool
- func (element Boolean) MarshalJSON() ([]byte, error)
- func (element Boolean) MarshalMap() map[string]any
- func (element Boolean) Remove(_ reflect.Value, _ list.List) (reflect.Value, error)
- func (element Boolean) Set(object reflect.Value, path list.List, value any) (reflect.Value, error)
- func (element Boolean) Type() reflect.Type
- func (element *Boolean) UnmarshalMap(data map[string]any) error
- func (element Boolean) Validate(object any) error
- type Element
- type ElementMap
- type Enumerator
- type FloatGetter
- type FloatSetter
- type Getter
- type Int64Getter
- type Int64Setter
- type IntGetter
- type IntSetter
- type Integer
- func (element Integer) Clean(value any) error
- func (element Integer) DefaultValue() any
- func (element Integer) Enumerate() []string
- func (element Integer) Get(object reflect.Value, path list.List) (reflect.Value, error)
- func (element Integer) GetElement(path list.List) (Element, error)
- func (element Integer) IsRequired() bool
- func (element Integer) MarshalMap() map[string]any
- func (element Integer) Remove(_ reflect.Value, _ list.List) (reflect.Value, error)
- func (element Integer) Set(object reflect.Value, path list.List, value any) (reflect.Value, error)
- func (element Integer) Type() reflect.Type
- func (element *Integer) UnmarshalMap(data map[string]any) error
- func (element Integer) Validate(value any) error
- type Nullable
- type Number
- func (element Number) Clean(value any) error
- func (element Number) DefaultValue() any
- func (element Number) Enumerate() []string
- func (element Number) Get(object reflect.Value, path list.List) (reflect.Value, error)
- func (element Number) GetElement(path list.List) (Element, error)
- func (element Number) IsRequired() bool
- func (element Number) MarshalMap() map[string]any
- func (element Number) Remove(_ reflect.Value, _ list.List) (reflect.Value, error)
- func (element Number) Set(object reflect.Value, path list.List, value any) (reflect.Value, error)
- func (element Number) Type() reflect.Type
- func (element *Number) UnmarshalMap(data map[string]any) error
- func (element Number) Validate(value any) error
- type Object
- func (element Object) Clean(value any) error
- func (element Object) DefaultValue() any
- func (element Object) Get(object reflect.Value, path list.List) (reflect.Value, error)
- func (element Object) GetElement(path list.List) (Element, error)
- func (element Object) IsRequired() bool
- func (element Object) MarshalMap() map[string]any
- func (element Object) Remove(object reflect.Value, path list.List) (reflect.Value, error)
- func (element Object) Set(object reflect.Value, path list.List, value any) (reflect.Value, error)
- func (element Object) Type() reflect.Type
- func (element *Object) UnmarshalMap(data map[string]any) error
- func (element Object) Validate(value any) error
- type Schema
- func (schema Schema) Clean(value any) error
- func (schema Schema) DefaultValue() any
- func (schema *Schema) ElementType() reflect.Type
- func (schema Schema) Get(object any, path string) (any, error)
- func (schema Schema) GetBool(object any, path string) bool
- func (schema Schema) GetElement(path string) (Element, error)
- func (schema Schema) GetFloat(object any, path string) float64
- func (schema Schema) GetInt(object any, path string) int
- func (schema Schema) GetInt64(object any, path string) int64
- func (schema Schema) GetString(object any, path string) string
- func (schema Schema) MarshalJSON() ([]byte, error)
- func (schema Schema) MarshalMap() map[string]any
- func (schema Schema) Remove(object any, path string) error
- 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) UnmarshalJSON(data []byte) error
- func (schema *Schema) UnmarshalMap(data map[string]any) error
- func (schema Schema) Validate(value any) error
- type Setter
- type String
- func (element String) Clean(value any) error
- func (element String) DefaultValue() any
- func (element String) Enumerate() []string
- func (element String) Get(object reflect.Value, path list.List) (reflect.Value, error)
- func (element String) GetElement(path list.List) (Element, error)
- func (element String) IsRequired() bool
- func (element String) MarshalMap() map[string]any
- func (element String) Remove(_ reflect.Value, _ list.List) (reflect.Value, error)
- func (element String) Set(object reflect.Value, path list.List, value any) (reflect.Value, error)
- func (element String) Type() reflect.Type
- func (element *String) UnmarshalMap(data map[string]any) error
- func (element String) Validate(value any) error
- type StringGetter
- type StringSetter
- type Type
- type ValidationError
- type WritableElement
Examples ¶
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.
const ValidationErrorCode = 422
ValidationErrorCode represents HTTP Status Code: 422 "Unproccessable Entity"
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Array ¶
type Array struct { Items Element MinLength int MaxLength int Required bool Delimiter string // DEPRECATED }
Array represents an array data type within a JSON-Schema.
func (Array) DefaultValue ¶ added in v0.6.0
DefaultValue returns the default value for this element type
func (Array) GetElement ¶ added in v0.6.0
GetElement returns a sub-element of this schema
func (Array) IsRequired ¶
IsRequired returns TRUE if this element is a required field
func (Array) MarshalMap ¶
MarshalMap populates object data into a map[string]any
func (*Array) UnmarshalMap ¶
UnmarshalMap tries to populate this object using data from a map[string]any
type BoolGetter ¶ added in v0.7.0
type BoolSetter ¶ added in v0.7.0
type Boolean ¶
Boolean represents a boolean data type within a JSON-Schema.
func (Boolean) DefaultValue ¶ added in v0.6.0
DefaultValue returns the default value for this element type
func (Boolean) GetElement ¶ added in v0.6.0
GetElement returns the element definition for a given path
func (Boolean) IsRequired ¶
IsRequired 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) Remove ¶ added in v0.6.0
Remove removes a value from the provided object/path. In the case of booleans, this is a no-op.
func (*Boolean) UnmarshalMap ¶
UnmarshalMap tries to populate this object using data from a map[string]any
type Element ¶
type Element interface { // Type returns the Type of this particular schema element Type() reflect.Type // Default returns the default value for this element DefaultValue() any // IsRequired returns true if this a value is required for this element IsRequired() bool // Get uses the path to locate a value in an object. Get(object reflect.Value, path list.List) (reflect.Value, error) // GetElement finds the schema element that defines the property at the end of the path GetElement(path list.List) (Element, error) // Set formats a value and applies it to the provided object/path Set(object reflect.Value, path list.List, value any) (reflect.Value, error) // Remove removes the value from the object at the designated path Remove(object reflect.Value, path list.List) (reflect.Value, error) // Validate validates the provided value Validate(value any) error // Clean updates a value to match the schema. The value must be a pointer. Clean(value any) error // MarshalMap populates the object data into a map[string]any MarshalMap() map[string]any }
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 ¶
type Enumerator ¶
type Enumerator interface {
Enumerate() []string
}
type FloatGetter ¶ added in v0.7.0
type FloatSetter ¶ added in v0.7.0
type Int64Getter ¶ added in v0.7.0
type Int64Setter ¶ added in v0.7.0
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:"emum"` Required bool }
Integer represents an integer data type within a JSON-Schema.
func (Integer) DefaultValue ¶ added in v0.6.0
DefaultValue returns the default value for this element type
func (Integer) GetElement ¶ added in v0.6.0
GetElement returns a sub-element of this schema
func (Integer) IsRequired ¶
IsRequired returns TRUE if this element is a required field
func (Integer) MarshalMap ¶
MarshalMap populates object data into a map[string]any
func (Integer) Remove ¶ added in v0.6.0
Remove removes a value from the provided object/path. In the case of integers, this is a no-op.
func (*Integer) UnmarshalMap ¶
UnmarshalMap tries to populate this object using data from a map[string]any
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 }
Number represents a number data type within a JSON-Schema.
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 returns a sub-element of this schema
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) Remove ¶ added in v0.6.0
Remove removes a value from the provided object/path. In the case of numbers, this is a no-op.
func (*Number) UnmarshalMap ¶
UnmarshalMap tries to populate this object using data from a map[string]any
type Object ¶
type Object struct { Properties ElementMap Unlisted Element RequiredProps []string Required bool }
Object represents an object data type within a JSON-Schema.
func (Object) DefaultValue ¶ added in v0.6.0
DefaultValue 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 a sub-element of this schema
func (Object) IsRequired ¶
IsRequired returns TRUE if this element is a required field
func (Object) MarshalMap ¶
MarshalMap populates object data into a map[string]any
func (Object) Remove ¶ added in v0.6.0
Remove removes a child of the target object. In the case of Maps, the map key is removed. In the case of Structs, the field is set to its default value.
func (*Object) UnmarshalMap ¶
UnmarshalMap tries to populate this object using data from a map[string]any
type Schema ¶
Schema defines a (simplified) JSON-Schema object, that can be Marshalled/Unmarshalled to JSON.
func Unmarshal ¶
Unmarshal converts a JSON string into a schema. If the string cannot be converted, then an empty schema is returned.
func (Schema) Clean ¶ added in v0.7.0
Clean tries to force a particular value to fit this schema by updating it (or all of its properties) to match. If values cannot be coerced to fit the schema, then an error is returned
func (Schema) DefaultValue ¶ added in v0.6.0
DefaultValue returns a value that matches the defaults for this schema.
func (*Schema) ElementType ¶ added in v0.6.0
ElementType returns the reflection type of this schema's top-level element
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) GetBool ¶
GetBool retrieves a bool value from this object. If the value is not defined in the object/schema, then the zero value (false) is returned
func (Schema) GetElement ¶ added in v0.6.0
func (Schema) GetFloat ¶
GetFloat retrieves a float64 value from this object. If the value is not defined in the object/schema, then the zero value (0.0) is returned
func (Schema) GetInt ¶
GetInt retrieves a int value from this object. If the value is not defined in the object/schema, then the zero value (0) is returned
func (Schema) GetInt64 ¶
GetInt64 retrieves an int64 value from this object. If the value is not defined in the object/schema, then the zero value (0) is returned
func (Schema) GetString ¶
GetString retrieves a string value from this object. If the value is not defined in the object/schema, then the zero value ("") is returned
func (Schema) MarshalJSON ¶
MarshalJSON converts a schema into JSON.
func (Schema) MarshalMap ¶
MarshalMap converts a schema into a map[string]any
func (Schema) Set ¶
Schema applies a value to the object at the given path. If the path is invalid then it returns an error
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 addedd successfully, then SetAll also uses Validate() 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
type String ¶
type String struct { Default string MinLength int MaxLength int Enum []string Pattern string Format string Required bool }
String represents a string data type within a JSON-Schema.
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 returns a sub-element of this schema
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) Remove ¶ added in v0.6.0
Remove removes a value from the provided object/path. In the case of strings, this is a no-op.
func (*String) UnmarshalMap ¶
UnmarshalMap tries to populate this object using data from a map[string]any
type StringGetter ¶ added in v0.7.0
type StringSetter ¶ added in v0.7.0
type ValidationError ¶
type ValidationError struct { Path string `json:"path"` // Identifies the PATH (or variable name) that has invalid input Message string `json:"message"` // Human-readable message that explains the problem with the input value. }
ValidationError represents an input validation error, and includes fields necessary to report problems back to the end user.
Example ¶
// Derp includes a custom error type for data validation, that tracks // the name (or path) of the invalid field and the reason that it is invalid err := Invalid("Field is required, or is too short, or is something else we don't like.") // ValidationErrors work anywhere that a standard error works fmt.Println(err.Error()) // Derp can also calculates the HTTP error code for ValidationErrors, which is 422 "Unprocessable Entity". fmt.Println(derp.ErrorCode(err))
Output: Field is required, or is too short, or is something else we don't like. 422
func Invalid ¶
func Invalid(message string) ValidationError
Invalid returns a fully populated ValidationError to the caller
func (ValidationError) Error ¶
func (v ValidationError) Error() string
Error returns a string representation of this ValidationError, and implements the builtin errors.error interface.
func (ValidationError) ErrorCode ¶
func (v ValidationError) ErrorCode() int
ErrorCode returns CodeValidationError for this ValidationError It implements the ErrorCodeGetter interface.