Documentation
¶
Overview ¶
Package schema implements validation of decoded CBOR values (the value model produced by github.com/Motmedel/utils_go/pkg/cbor) against schemas. Schemas can be authored directly or derived from Go types with NewFromType, using the same struct tag grammar as the jsonschema library. The keyword set is deliberately small; unknown map keys are rejected unless AdditionalProperties is set.
Index ¶
- Variables
- func RegisterFormat(name string, formatValidator FormatValidator)
- type FormatValidator
- type Issue
- type Schema
- func (s *Schema) Clone() *Schema
- func (s *Schema) Validate(value any) error
- func (s *Schema) ValidateArray(value []any) error
- func (s *Schema) ValidateBoolean(value bool) error
- func (s *Schema) ValidateByteString(value []byte) error
- func (s *Schema) ValidateBytes(data []byte) error
- func (s *Schema) ValidateInteger(value int64) error
- func (s *Schema) ValidateMap(value map[any]any) error
- func (s *Schema) ValidateNull() error
- func (s *Schema) ValidateText(value string) error
- type Type
- type ValidateError
Constants ¶
This section is empty.
Variables ¶
var ( ErrUnsupportedType = errors.New("unsupported type") ErrMalformedTag = errors.New("malformed tag") ErrNilSchema = errors.New("nil schema") )
var ErrNotBareAddress = errors.New("not a bare address")
ErrNotBareAddress reports an email value with anything besides the address itself, e.g. a display name ("Name <user@example.com>").
Functions ¶
func RegisterFormat ¶
func RegisterFormat(name string, formatValidator FormatValidator)
RegisterFormat makes a format validator available to schemas referencing it by name.
Types ¶
type FormatValidator ¶
FormatValidator validates a text value against a named format.
type Issue ¶
Issue is a single validation failure at the value identified by Path (a JSON Pointer-style path; empty for the root value).
type Schema ¶
type Schema struct {
Type Type
// Nullable additionally permits null regardless of Type.
Nullable bool
// Properties describes map entries by text key (Type = TypeMap).
Properties map[string]*Schema
// Required lists property keys that must be present (Type = TypeMap).
Required []string
// AdditionalProperties describes map entries not named in Properties (Type = TypeMap). When
// nil, such entries are rejected.
AdditionalProperties *Schema
// Items describes array elements (Type = TypeArray).
Items *Schema
MinItems *int
MaxItems *int
// MinLength and MaxLength bound text length in runes and bytes length in bytes
// (Type = TypeText or TypeBytes).
MinLength *int
MaxLength *int
// Minimum and Maximum bound integers inclusively (Type = TypeInteger).
Minimum *int64
Maximum *int64
// Format names a registered format validator (Type = TypeText).
Format string
}
func NewFromType ¶
NewFromType derives a schema from a Go type, using the cborschema struct tag, falling back to jsonschema (same grammar) and json (with omitempty and omitzero marking fields optional).
func (*Schema) Validate ¶
Validate checks a decoded CBOR value against the schema, returning a *ValidateError carrying all violations, or nil if the value is valid. The value must use the type model produced by cbor.Decode: map[any]any, []any, string, []byte, int64, bool, nil, cbor.Tag, or cbor.Undefined.
func (*Schema) ValidateArray ¶
ValidateArray validates an array value.
func (*Schema) ValidateBoolean ¶
ValidateBoolean validates a boolean value.
func (*Schema) ValidateByteString ¶
ValidateByteString validates a byte-string value. Unlike ValidateBytes, the value is not decoded; it is the value.
func (*Schema) ValidateBytes ¶
ValidateBytes decodes data and validates the resulting value. When the decoded value is also needed afterwards (for cbor.UnmarshalValue, say), decode once and use Validate instead of decoding twice.
func (*Schema) ValidateInteger ¶
ValidateInteger validates an integer value.
func (*Schema) ValidateMap ¶
ValidateMap validates a map value.
func (*Schema) ValidateNull ¶
ValidateNull validates the null value.
func (*Schema) ValidateText ¶
ValidateText validates a text-string value.
type ValidateError ¶
type ValidateError struct {
Issues []*Issue
}
func (*ValidateError) Error ¶
func (validateError *ValidateError) Error() string