schema

package
v0.52.0 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 23 Imported by: 6 modules, 15 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

schema

A fast, minimal subset of JSON-Schema. It unmarshals a schema from JSON, validates values against Array/Boolean/Integer/Number/Object/String element types (with custom string formats), and reads/writes data through a schema by dotted path (such as child.name or tags.0). This is the heart of rosetta — the engine that lets dynamic data (form inputs, CMS records) be typed and validated at runtime. If you need a complete, rigorous JSON-Schema implementation, use another tool.

Go Reference

Included: unmarshal-from-JSON, the six element-type validators, custom Format rules, and a happy API for walking the schema tree and getting/setting data by path. Left out: $ref references and loading remote schemas by URI.

How data access works

Schema does not reflect over your structs. Instead, the data structure must implement Getter/Setter interfaces so schema has a type-safe path to its values. mapof and sliceof implement them, so a mapof.Any is the easiest carrier for dynamic data; structs can implement them by hand:

type MyStruct struct {
	Name  string
	Email string
}

func (m MyStruct) GetStringOK(path string) (string, bool) {
	switch path {
	case "name":
		return m.Name, true
	case "email":
		return m.Email, true
	}
	return "", false
}

func (m *MyStruct) SetString(path string, value string) bool {
	switch path {
	case "name":
		m.Name = value
		return true
	case "email":
		m.Email = value
		return true
	}
	return false
}

Additional interfaces (GetPointer, SetKey, IsMap, Keys, array Length/GetIndex/SetIndex, …) let schema traverse nested objects and arrays.

What matters here

  • Schema.Set VALIDATES and may COERCE the value before storing it — it is not a plain assignment. Every Set runs the element's validate step, so a value can be clamped (min/max), truncated (maxLength), rewritten by a format, or rejected with an error. Callers that treat Set as a dumb setter can be surprised when the stored value differs from the input or when Set returns an error. (SetAll/SetURLValues additionally run ValidateRequiredIf after all values are set.)
  • Data structures must implement the Getter/Setter interfaces; schema never discovers fields by reflection. A type missing the interface for a given path fails the get or set with an error rather than panicking. This is the central design constraint — the interfaces in interfaces.go are the contract every carrier type must satisfy.
  • String formats are registered with UseFormat, and the registry FREEZES on first read. The first validation that looks up a format locks the registry; any UseFormat after that is rejected. Register all custom formats during init/startup, before validation runs. See format/.
  • The per-element Validate(any) methods (Boolean.Validate, Integer.Validate, Number.Validate, String.Validate, Any.Validate) are intentionally retained. They look like dead duplicates of the package-level validate/Validate flow, and they are outside the Element interface and only tests call them, but they are kept on purpose — do not prune them as unused.
  • required-if is not supported inside an Array's items. A condition such as adapter is S3 is a path measured from the schema root, so it can name a field in a fixed place but never "the adapter of the item I am in". An Array with no required-if of its own therefore does not visit its items, and a condition written on a field inside them is never evaluated. Use required-if on root-level fields and on Objects reached by a fixed path, and check list items in code. Decided by Ben on 2026-12-03 (BUG-005929 S2); TestRequiredIf_Array_ItemConditionsAreNotEvaluated pins it.
  • Pass a value, or ONE pointer to it. A pointer to a pointer is refused. An Object accepts a map or a struct, held by value or behind a single pointer. A **mapof.Any or a **MyStruct implements none of the Getter/Setter interfaces, so Validate and Normalize answer 422 "Expected a map, found a single value", and Set and SetAll answer 500 "Target Object must be a StringSetter or PointerGetter". The value is left untouched. The usual cause is taking the address of a variable that already holds a pointer: write Validate(object), not Validate(&object). v0.45.0 let a double pointer through without checking it against a Wildcard, and failed with a 500 on any named property.
  • Validation can rewrite values, and Validate reports every rewrite as an error. It writes the rewritten value through a pointer or a map and returns a derp.Validation error, so its changed result is always false. Normalize applies the same rewrites without an error and returns the path of each one.

Documentation

Overview

Package schema defines, validates, and manipulates JSON-Schema-like structures in Go.

A Schema wraps a single Element — Object, Array, String, Integer, Number, Boolean, or Any — each describing one node of a document along with its validation rules. Schemas are built in Go or unmarshaled from JSON, and are used for three jobs: validating a value, normalizing it into the shape the schema describes, and reading or writing individual values by path.

Path access is interface-driven rather than reflective. A target object opts in by implementing the narrow Getter and Setter interfaces it can support — StringGetter, IntSetter, ArrayGetter, and the rest — and the schema calls only what the type advertises. The mapof and sliceof packages implement these already, which makes them the default carriers when there is no Go struct to bind to.

Setting a value validates it, so a write can coerce, clamp, or truncate what it was given rather than storing it verbatim. String elements also pass the value through named formats from the format subpackage, and UseFormat registers additional ones.

Index

Constants

View Source
const TypeAny = Type("any")

TypeAny is the token used by JSON-Schema to designate that any kind of data

View Source
const TypeArray = Type("array")

TypeArray is the token used by JSON-Schema to designate that a schema describes an array.

View Source
const TypeBoolean = Type("boolean")

TypeBoolean is the token used by JSON-Schema to designate that a schema describes an boolean.

View Source
const TypeInteger = Type("integer")

TypeInteger is the token used by JSON-Schema to designate that a schema describes an integer.

View Source
const TypeNumber = Type("number")

TypeNumber is the token used by JSON-Schema to designate that a schema describes an number.

View Source
const TypeObject = Type("object")

TypeObject is the token used by JSON-Schema to designate that a schema describes an object.

View Source
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

func Index(token string, maxLengths ...int) (int, bool)

Index converts a string into an array index, and TRUE if it is a non-negative integer below every maximum provided. Otherwise it returns FALSE, with 0 or one less than the first maximum reached.

func IsRegisteredFormat added in v0.31.0

func IsRegisteredFormat(name string) bool

IsRegisteredFormat returns TRUE if a string format generator is registered under the given name. Like every registry read, the first call freezes the registry against further UseFormat calls, so it must not be called until all format registrations are complete.

func Normalize added in v0.30.0

func Normalize(schema Schema, value any) ([]string, error)

Normalize rewrites a value in place so that it conforms to the schema, returning the path of every rewritten value, or an error when the value cannot be made to conform.

func SetProperty added in v0.26.0

func SetProperty(element Element, object any, path string, value any) (err error)

SetProperty sets the value at the specified path within the object according to the provided schema

func UseFormat

func UseFormat(name string, fn format.Generator)

UseFormat adds a custom format.Generator 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.

func Validate added in v0.26.0

func Validate(schema Schema, value any) (any, bool, error)

Validate checks a value against the schema, returning an error when it does not conform as given, including when a value had to be rewritten to conform.

func ValidateElementFormats added in v0.31.0

func ValidateElementFormats(element Element, path string) error

ValidateElementFormats recursively checks the format names on an element and all of its children. The path argument locates errors within nested schemas; pass "" for the root element.

Types

type Any

type Any struct {
	Required   bool
	RequiredIf string
}

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

func (element Any) DefaultValue() any

DefaultValue returns the default value for this element

func (Any) GetElement added in v0.12.0

func (element Any) GetElement(_ string) (Element, bool)

GetElement implements the Element interface It returns the element at the specified path

func (Any) Inherit added in v0.12.0

func (element Any) Inherit(_ Element)

Inherit implements the Element interface It is a no-op for Any elements

func (Any) IsRequired

func (element Any) IsRequired() bool

IsRequired returns true if this a value is required for this element

func (Any) MarshalMap

func (element Any) MarshalMap() map[string]any

MarshalMap populates the object data into a map[string]any

func (*Any) UnmarshalMap

func (element *Any) UnmarshalMap(data map[string]any) error

UnmarshalMap tries to populate this object using data from a map[string]any

func (Any) Validate

func (element Any) Validate(_ any) error

Validate validates the provided value

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

func (Array) DefaultValue() any

DefaultValue implements the Element interface It returns the default value for this element type

func (Array) GetElement added in v0.6.0

func (element Array) GetElement(name string) (Element, bool)

GetElement implements the Element interface It returns the element at the specified path

func (Array) GetIndex added in v0.22.0

func (Array) GetIndex(value any, index int) (any, bool)

GetIndex returns the value at a specific index in the array (if the object implements ArrayGetter)

func (Array) GetLength added in v0.22.0

func (Array) GetLength(value any) (int, bool)

GetLength returns the length of the array value (if the object implements LengthGetter)

func (Array) Inherit added in v0.12.0

func (Array) Inherit(_ Element)

Inherit implements the Element interface It is a no-op for Array elements

func (Array) IsRequired

func (element Array) IsRequired() bool

IsRequired implements the Element interface It returns TRUE if this element is a required field

func (Array) MarshalMap

func (element Array) MarshalMap() map[string]any

MarshalMap populates object data into a map[string]any

func (Array) SetIndex added in v0.22.0

func (Array) SetIndex(value any, index int, item any) bool

SetIndex sets the value at a specific index in the array (if the object implements ArraySetter)

func (*Array) UnmarshalMap

func (element *Array) UnmarshalMap(data map[string]any) error

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 reads values out of the array
	ArrayGetter

	// ArraySetter writes values into the array
	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

func (element Boolean) DefaultValue() any

DefaultValue is a part of the Element interface

func (Boolean) GetElement added in v0.6.0

func (element Boolean) GetElement(name string) (Element, bool)

GetElement implements the Element interface It returns the element at the specified path

func (Boolean) Inherit added in v0.12.0

func (Boolean) Inherit(_ Element)

Inherit implements the Element interface It is a no-op for Boolean elements

func (Boolean) IsRequired

func (element Boolean) IsRequired() bool

IsRequired is a part of the Element interface it returns TRUE if this element is a required field

func (Boolean) MarshalJSON

func (element Boolean) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface

func (Boolean) MarshalMap

func (element Boolean) MarshalMap() map[string]any

MarshalMap populates object data into a map[string]any

func (*Boolean) UnmarshalMap

func (element *Boolean) UnmarshalMap(data map[string]any) error

UnmarshalMap tries to populate this object using data from a map[string]any

func (Boolean) Validate

func (element Boolean) Validate(object any) error

Validate validates a generic value using this schema. It is not part of the Element interface

type Element

type Element interface {

	// DefaultValue returns the default value for this element
	DefaultValue() any

	// IsRequired returns true if this a value is required for this element
	IsRequired() bool

	// 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 copies any properties that this element does not define from the parent element
	Inherit(Element)

	// AllProperties returns a flat map of every property reachable from this element
	AllProperties() ElementMap
	// contains filtered or unexported methods
}

Element interface wraps all of the methods required for schema elements.

func UnmarshalJSON

func UnmarshalJSON(data []byte) (Element, error)

UnmarshalJSON tries to parse a []byte into a schema.Element

func UnmarshalMap

func UnmarshalMap(data any) (Element, error)

UnmarshalMap tries to parse a map[string]any into a schema.Element

type ElementMap

type ElementMap map[string]Element

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

func (element Integer) DefaultValue() any

DefaultValue implements the Element interface It returns the default value for this element type, sized to the element's BitSize. A default that does not fit the declared bit size is clamped to that size's range rather than silently wrapping around.

func (Integer) Enumerate

func (element Integer) Enumerate() []string

Enumerate implements the "Enumerator" interface

func (Integer) GetElement added in v0.6.0

func (element Integer) GetElement(name string) (Element, bool)

GetElement implements the Element interface It returns the element at the specified path

func (Integer) Inherit added in v0.12.0

func (element Integer) Inherit(_ Element)

Inherit implements the Element interface It is a no-op for Integer elements

func (Integer) IsRequired

func (element Integer) IsRequired() bool

IsRequired implements the Element interface It returns TRUE if this element is a required field

func (Integer) MarshalMap

func (element Integer) MarshalMap() map[string]any

MarshalMap populates object data into a map[string]any

func (*Integer) UnmarshalMap

func (element *Integer) UnmarshalMap(data map[string]any) error

UnmarshalMap tries to populate this object using data from a map[string]any

func (Integer) Validate

func (element Integer) Validate(value any) error

Validate validates a value using this schema. It is not part of the Element interface

type KeySetter added in v0.44.0

type KeySetter interface {

	// SetKey stores the value under the key, or returns an error if the map cannot hold it
	SetKey(string, any) error
}

KeySetter allows a map to store a value under a single key, exactly as given

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 MapTyper added in v0.29.0

type MapTyper interface {

	// IsMap returns TRUE if this object should be treated as a map during schema traversal.
	IsMap() bool
}

MapTyper allows an object to explicitly declare that it is a map (an open set of keys) rather than a struct (a fixed set of fields). Object validation treats a key that the schema names but the map omits as a legitimately-empty optional property; a struct that simply forgot to wire up an accessor must NOT be mistaken for this case, so map-ness is declared explicitly here rather than inferred from the presence/absence of other interfaces.

type Nullable

type Nullable interface {

	// IsNull returns TRUE if this object contains a null value
	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

func (element Number) DefaultValue() any

DefaultValue returns the default value for this element type

func (Number) Enumerate

func (element Number) Enumerate() []string

Enumerate implements the "Enumerator" interface

func (Number) GetElement added in v0.6.0

func (element Number) GetElement(name string) (Element, bool)

GetElement implements the Element interface It returns the element at the specified path

func (Number) Inherit added in v0.12.0

func (element Number) Inherit(_ Element)

Inherit implements the Element interface It is a no-op for Number elements

func (Number) IsRequired

func (element Number) IsRequired() bool

IsRequired returns TRUE if this element is a required field

func (Number) MarshalMap

func (element Number) MarshalMap() map[string]any

MarshalMap populates object data into a map[string]any

func (*Number) UnmarshalMap

func (element *Number) UnmarshalMap(data map[string]any) error

UnmarshalMap tries to populate this object using data from a map[string]any

func (Number) Validate

func (element Number) Validate(value any) error

Validate validates a value against this schema

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 map of the named properties, with nested Objects flattened into dotted keys

func (Object) DefaultValue added in v0.6.0

func (element Object) DefaultValue() any

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

func (element Object) GetElement(name string) (Element, bool)

GetElement returns the element at the specified path within the object

func (Object) Inherit added in v0.12.0

func (element Object) Inherit(parent Element)

Inherit implements the Element interface It inherits properties from the parent element

func (Object) IsRequired

func (element Object) IsRequired() bool

IsRequired implements the Element interface returns TRUE if this element is a required field

func (Object) MarshalMap

func (element Object) MarshalMap() map[string]any

MarshalMap populates object data into a map[string]any

func (*Object) UnmarshalMap

func (element *Object) UnmarshalMap(data map[string]any) error

UnmarshalMap tries to populate this object using data from a map[string]any

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

type Schema struct {
	ID      string
	Comment string
	Element Element
}

Schema defines a (simplified) JSON-Schema object, that can be Marshalled/Unmarshalled to JSON.

func New

func New(element Element) Schema

New generates a fully initialized Schema object using the provided properties

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 map of the named properties in this schema, keyed by dotted path

func (Schema) Append added in v0.24.0

func (schema Schema) Append(object any, path string, value any) error

Append appends a value to the array at the specified path within the object according to this schema

func (Schema) Get

func (schema Schema) Get(object any, path string) (any, error)

Get retrieves a generic value from the object. It never returns a schema default: a read past the end of a list returns the zero value of the path's element, and a missing value is an error.

func (Schema) GetArrayElement added in v0.22.0

func (schema Schema) GetArrayElement(path string) (Array, bool)

GetArrayElement returns the array element at the specified path, or FALSE if invalid

func (Schema) GetBooleanElement added in v0.22.0

func (schema Schema) GetBooleanElement(path string) (Boolean, bool)

GetBooleanElement returns the boolean element at the specified path, or FALSE if invalide

func (Schema) GetElement added in v0.6.0

func (schema Schema) GetElement(path string) (Element, bool)

GetElement returns the element at the specified path, or FALSE if the element does not exist

func (Schema) GetIntegerElement added in v0.22.0

func (schema Schema) GetIntegerElement(path string) (Integer, bool)

GetIntegerElement returns the integer element at the specified path, or FALSE if invalid

func (Schema) GetNumberElement added in v0.22.0

func (schema Schema) GetNumberElement(path string) (Number, bool)

GetNumberElement returns the number element at the specified path, or FALSE if invalid

func (Schema) GetObjectElement added in v0.22.0

func (schema Schema) GetObjectElement(path string) (Object, bool)

GetObjectElement returns the object element at the specified path, or FALSE if invalid

func (Schema) GetStringElement added in v0.22.0

func (schema Schema) GetStringElement(path string) (String, bool)

GetStringElement returns the string element at the specified path, or FALSE if invalid

func (*Schema) Inherit added in v0.12.0

func (schema *Schema) Inherit(parent Schema)

Inherit updates this schema with properties from the parent schema

func (Schema) MarshalJSON

func (schema Schema) MarshalJSON() ([]byte, error)

MarshalJSON converts a schema into JSON.

func (Schema) MarshalMap

func (schema Schema) MarshalMap() map[string]any

MarshalMap converts a schema into a map[string]any

func (Schema) Match added in v0.13.1

func (schema Schema) Match(value any, expression exp.Expression) (bool, error)

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) Normalize added in v0.30.0

func (schema Schema) Normalize(value any) ([]string, error)

Normalize rewrites a value in place so that it conforms to this schema, returning the path of every rewritten value, or an error when the value cannot be made to conform.

func (Schema) Remove added in v0.6.0

func (schema Schema) Remove(object any, path string) bool

Remove removes a value from the object at the specified path

func (Schema) Set

func (schema Schema) Set(object any, path string, value any) error

Set sets the value at the specified path within the object according to this schema

func (Schema) SetAll

func (schema Schema) SetAll(object any, values map[string]any) error

SetAll sets every value in the object through Set, stopping at the first error, then confirms the object's required-if conditions.

func (Schema) SetURLValues added in v0.24.3

func (schema Schema) SetURLValues(object any, values url.Values) error

SetURLValues sets every form value in the object through Set, stopping at the first error, then confirms the object's required-if conditions.

func (*Schema) UnmarshalJSON

func (schema *Schema) UnmarshalJSON(data []byte) error

UnmarshalJSON creates a new Schema object using a JSON-serialized byte array.

func (*Schema) UnmarshalMap

func (schema *Schema) UnmarshalMap(data map[string]any) error

UnmarshalMap updates a Schema using a map[string]any

func (Schema) Validate

func (schema Schema) Validate(value any) (bool, error)

Validate checks the provided value against this schema and its required-if conditions. The bool is always FALSE, because a value that had to be rewritten is reported as a validation error.

func (Schema) ValidateFormats added in v0.31.0

func (schema Schema) ValidateFormats() error

ValidateFormats confirms that every format name used by a String element in this schema resolves in the format registry. String validation silently skips names it does not recognize, so a typo'd format otherwise degrades without error; calling this from a test (or at startup, after all UseFormat registrations) makes that failure loud instead.

func (Schema) ValidateRequiredIf added in v0.13.1

func (schema Schema) ValidateRequiredIf(value any) error

ValidateRequiredIf returns an error if any conditionally-required field in the schema is missing while its condition is true.

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"`
	Pattern    string   `json:"pattern"`
	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

func (element String) DefaultValue() any

DefaultValue returns the default value for this element type

func (String) Enumerate

func (element String) Enumerate() []string

Enumerate implements the "Enumerator" interface

func (String) GetElement added in v0.6.0

func (element String) GetElement(name string) (Element, bool)

GetElement implements the Element interface It returns the element at the specified path

func (String) Inherit added in v0.12.0

func (String) Inherit(_ Element)

Inherit implements the Element interface It is a no-op for String elements

func (String) IsRequired

func (element String) IsRequired() bool

IsRequired returns TRUE if this element is a required field

func (String) MarshalMap

func (element String) MarshalMap() map[string]any

MarshalMap populates object data into a map[string]any

func (*String) UnmarshalMap

func (element *String) UnmarshalMap(data map[string]any) error

UnmarshalMap tries to populate this object using data from a map[string]any

func (String) Validate

func (element String) Validate(value any) error

Validate compares a generic data value using this Schema

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 Type

type Type string

Type enumerates all of the data types that can make up a schema

func (Type) String

func (schemaType Type) String() string

String implements the ubiquitous "Stringer" interface, so that these types can be represented as strings, if necessary

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

type ValueSetter interface {

	// SetValue sets the entire value of the object
	SetValue(any) error
}

ValueSetter allows an object to set its entire value

type WritableElement

type WritableElement interface {

	// UnmarshalMap tries to populate this object using data from a map[string]any
	UnmarshalMap(map[string]any) error

	// Element provides the read-only half of the contract
	Element
}

WritableElement represents an Element (usually a pointer to a concrete type) whose value can be changed.

Directories

Path Synopsis
Package format provides the named string formats used by the schema package.
Package format provides the named string formats used by the schema package.

Jump to

Keyboard shortcuts

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