schema

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2026 License: MIT Imports: 10 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
var (
	ErrUnsupportedType = errors.New("unsupported type")
	ErrMalformedTag    = errors.New("malformed tag")
	ErrNilSchema       = errors.New("nil schema")
)

Functions

func RegisterFormat

func RegisterFormat(name string, formatValidator FormatValidator)

RegisterFormat makes a format validator available to schemas referencing it by name.

Types

type FormatValidator

type FormatValidator func(value string) error

FormatValidator validates a text value against a named format.

type Issue

type Issue struct {
	Path    string `json:"path"`
	Message string `json:"message"`
}

Issue is a single validation failure at the value identified by Path (a JSON Pointer-style path; empty for the root value).

func (*Issue) String

func (issue *Issue) String() string

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

func NewFromType[T any]() (*Schema, error)

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) Clone

func (s *Schema) Clone() *Schema

Clone returns a deep copy of the schema.

func (*Schema) Validate

func (s *Schema) Validate(value any) error

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

func (s *Schema) ValidateArray(value []any) error

ValidateArray validates an array value.

func (*Schema) ValidateBoolean

func (s *Schema) ValidateBoolean(value bool) error

ValidateBoolean validates a boolean value.

func (*Schema) ValidateByteString

func (s *Schema) ValidateByteString(value []byte) error

ValidateByteString validates a byte-string value. Unlike ValidateBytes, the value is not decoded; it is the value.

func (*Schema) ValidateBytes

func (s *Schema) ValidateBytes(data []byte) error

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

func (s *Schema) ValidateInteger(value int64) error

ValidateInteger validates an integer value.

func (*Schema) ValidateMap

func (s *Schema) ValidateMap(value map[any]any) error

ValidateMap validates a map value.

func (*Schema) ValidateNull

func (s *Schema) ValidateNull() error

ValidateNull validates the null value.

func (*Schema) ValidateText

func (s *Schema) ValidateText(value string) error

ValidateText validates a text-string value.

type Type

type Type string
const (
	// TypeAny imposes no type constraint.
	TypeAny     Type = ""
	TypeMap     Type = "map"
	TypeArray   Type = "array"
	TypeText    Type = "text"
	TypeBytes   Type = "bytes"
	TypeInteger Type = "integer"
	TypeBoolean Type = "boolean"
	TypeNull    Type = "null"
)

type ValidateError

type ValidateError struct {
	Issues []*Issue
}

func (*ValidateError) Error

func (validateError *ValidateError) Error() string

Jump to

Keyboard shortcuts

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