Documentation ¶
Index ¶
- Constants
- func UseFormat(name string, fn format.Function)
- type Any
- func (element Any) Get(object reflect.Value, path string) (any, Element, error)
- func (element Any) IsRequired() bool
- func (element Any) MarshalMap() map[string]any
- func (element Any) Set(object reflect.Value, path string, value any) error
- func (element Any) Type() reflect.Type
- func (element *Any) UnmarshalMap(data map[string]any) error
- func (element Any) Validate(value any) error
- type Array
- func (element Array) Get(object reflect.Value, path string) (any, Element, error)
- func (element Array) IsRequired() bool
- func (element Array) MarshalMap() map[string]any
- func (element Array) Set(object reflect.Value, path string, value any) error
- func (element Array) Type() reflect.Type
- func (element *Array) UnmarshalMap(data map[string]any) error
- func (element Array) Validate(value any) error
- type Boolean
- func (element Boolean) Get(object reflect.Value, path string) (any, Element, error)
- func (element Boolean) IsRequired() bool
- func (element Boolean) MarshalJSON() ([]byte, error)
- func (element Boolean) MarshalMap() map[string]any
- func (element Boolean) Set(object reflect.Value, path string, value any) 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 Integer
- func (element Integer) Enumerate() []string
- func (element Integer) Get(object reflect.Value, path string) (any, Element, error)
- func (element Integer) IsRequired() bool
- func (element Integer) MarshalMap() map[string]any
- func (element Integer) Set(object reflect.Value, path string, value any) 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) Enumerate() []string
- func (element Number) Get(object reflect.Value, path string) (any, Element, error)
- func (element Number) IsRequired() bool
- func (element Number) MarshalMap() map[string]any
- func (element Number) Set(object reflect.Value, path string, value any) 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) Get(object reflect.Value, path string) (any, Element, error)
- func (element Object) IsRequired() bool
- func (element Object) MarshalMap() map[string]any
- func (element Object) Set(object reflect.Value, path string, value any) error
- func (element Object) Type() reflect.Type
- func (element *Object) UnmarshalMap(data map[string]any) error
- func (element Object) Validate(value any) error
- type PathGetter
- type PathSetter
- type Schema
- func (schema Schema) Get(object any, path string) (any, Element, error)
- func (schema Schema) GetBool(object any, path string) bool
- 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) 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 String
- func (element String) Enumerate() []string
- func (element String) Get(object reflect.Value, path string) (any, Element, error)
- func (element String) IsRequired() bool
- func (element String) MarshalMap() map[string]any
- func (element String) Set(object reflect.Value, path string, value any) error
- func (element String) Type() reflect.Type
- func (element *String) UnmarshalMap(data map[string]any) error
- func (element String) Validate(value any) error
- 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 Any ¶
type Any struct {
Required bool
}
Any represents a any data type within a JSON-Schema.
func (Any) IsRequired ¶
IsRequired returns TRUE if this element is a required field
func (Any) MarshalMap ¶
MarshalMap populates object data into a map[string]any
func (*Any) UnmarshalMap ¶
UnmarshalMap tries to populate this object using data from a map[string]any
type Array ¶
Array represents an array data type within a JSON-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 Boolean ¶
Boolean represents a boolean data type within a JSON-Schema.
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) 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 // Find uses the path to locate a value in an object along with the schema that defines it. Get(object reflect.Value, path string) (any, Element, error) // Set formats a value and applies it to the provided object/path Set(object reflect.Value, path string, value any) error // Validate validates the provided value Validate(value any) error // MarshalMap populates the object data into a map[string]any MarshalMap() map[string]any IsRequired() bool }
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 Integer ¶
type Integer struct { Default null.Int64 `json:"default"` // TODO: Int64?? Minimum null.Int64 `json:"minimum"` Maximum null.Int64 `json:"maximum"` MultipleOf null.Int64 `json:"multipleOf"` Enum []int `json:"emum"` Required bool }
Integer represents an integer data type within a JSON-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) 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"` Enum []float64 `json:"enum"` Required bool }
Number represents a number data type within a JSON-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) UnmarshalMap ¶
UnmarshalMap tries to populate this object using data from a map[string]any
type Object ¶
type Object struct { Properties ElementMap RequiredProps []string Required bool }
Object represents an object data type within a JSON-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) UnmarshalMap ¶
UnmarshalMap tries to populate this object using data from a map[string]any
type PathGetter ¶
type PathSetter ¶
type Schema ¶
Schema defines a (simplified) JSON-Schema object, that can be Marshalled/Unmarshalled to JSON.
func New ¶
func New(properties ElementMap) Schema
New generates a fully initialized Schema object using the provided properties
func Unmarshal ¶
Unmarshal converts a JSON string into a schema. If the string cannot be converted, then an empty schema is returned.
func (Schema) Get ¶
Get retrieves a generic value from the object, along with the schema element that defines it. 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) 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 null.Int MaxLength null.Int Enum []string Pattern string Format string Required bool }
String represents a string data type within a JSON-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) UnmarshalMap ¶
UnmarshalMap tries to populate this object using data from a map[string]any
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.