schema

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ValidatorMinLength   = "minLength"
	ValidatorMaxLength   = "maxLength"
	ValidatorPattern     = "pattern"
	ValidatorEnum        = "enum"
	ValidatorMin         = "min"
	ValidatorMax         = "max"
	ValidatorMinItems    = "minItems"
	ValidatorMaxItems    = "maxItems"
	ValidatorUniqueItems = "uniqueItems"
)

Validator type constants

View Source
const (
	TypeString  = "string"
	TypeInteger = "integer"
	TypeNumber  = "number"
	TypeBoolean = "boolean"
	TypeObject  = "object"
	TypeArray   = "array"
)

Schema type constants

View Source
const (
	ErrUnknownProperty           = "unknown property"
	ErrRequiredFieldMissing      = "required field is missing"
	ErrUnknownValidatorType      = "unknown validator type: %s"
	ErrUnknownSchemaType         = "unknown type: %s"
	ErrExpectedType              = "expected %s, got %T"
	ErrExpectedIntegerGotFloat   = "expected integer, got float with decimal part"
	ErrValidatorOnlyForType      = "%s validator can only be applied to %s"
	ErrValidatorValueMustBeType  = "%s validator value must be %s"
	ErrStringLengthLessThanMin   = "string length %d is less than minimum %d"
	ErrStringLengthExceedsMax    = "string length %d exceeds maximum %d"
	ErrInvalidRegexPattern       = "invalid regex pattern: %s"
	ErrStringDoesNotMatchPattern = "string does not match pattern %s"
	ErrValueNotInEnum            = "value is not in allowed enum values: %s"
	ErrValueLessThanMin          = "value %v is less than minimum %v"
	ErrValueExceedsMax           = "value %v exceeds maximum %v"
	ErrArrayLengthLessThanMin    = "array length %d is less than minimum %d"
	ErrArrayLengthExceedsMax     = "array length %d exceeds maximum %d"
	ErrArrayContainsDuplicates   = "array contains duplicate items"
	ErrFloatHasDecimalPart       = "float value has decimal part"
	ErrCannotConvertType         = "cannot convert %T to %T"
)

Error message templates

Variables

This section is empty.

Functions

func ApplyDefaults

func ApplyDefaults(data map[string]any, schema CustomSchema) map[string]any

ApplyDefaults applies default values to data based on the schema

Types

type CustomSchema

type CustomSchema map[string]PropertyDefinition

CustomSchema represents the root schema structure

func (CustomSchema) GormDataType

func (cs CustomSchema) GormDataType() string

GormDataType returns the GORM data type for CustomSchema

func (CustomSchema) MarshalJSON

func (cs CustomSchema) MarshalJSON() ([]byte, error)

MarshalJSON implements custom properties.JSON marshaling for CustomSchema

func (*CustomSchema) Scan

func (cs *CustomSchema) Scan(value any) error

Scan implements the sql.Scanner interface

func (*CustomSchema) UnmarshalJSON

func (cs *CustomSchema) UnmarshalJSON(data []byte) error

UnmarshalJSON implements custom properties.JSON unmarshaling for CustomSchema

func (CustomSchema) Validate

func (cs CustomSchema) Validate() error

Validate checks if the CustomSchema is valid using go-playground/validator

func (CustomSchema) Value

func (cs CustomSchema) Value() (driver.Value, error)

Value implements the driver.Valuer interface

type PropertyDefinition

type PropertyDefinition struct {
	Type       string                        `json:"type" validate:"required,oneof=string integer number boolean object array"`
	Label      string                        `json:"label,omitempty"`
	Required   bool                          `json:"required,omitempty"`
	Default    any                           `json:"default,omitempty"`
	Validators []ValidatorDefinition         `json:"validators,omitempty" validate:"dive"`
	Properties map[string]PropertyDefinition `json:"properties,omitempty" validate:"dive"`
	Items      *PropertyDefinition           `json:"items,omitempty"`
}

PropertyDefinition defines a single property in the schema

type StandardArray

type StandardArray = []any

Standard target types for each schema type These are the canonical types we convert to for consistent comparisons

type StandardBoolean

type StandardBoolean = bool

Standard target types for each schema type These are the canonical types we convert to for consistent comparisons

type StandardInteger

type StandardInteger = int64

Standard target types for each schema type These are the canonical types we convert to for consistent comparisons

type StandardNumber

type StandardNumber = float64

Standard target types for each schema type These are the canonical types we convert to for consistent comparisons

type StandardObject

type StandardObject = map[string]any

Standard target types for each schema type These are the canonical types we convert to for consistent comparisons

type StandardString

type StandardString = string

Standard target types for each schema type These are the canonical types we convert to for consistent comparisons

type ValidationError

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

ValidationError represents a validation error with path context

func Validate

func Validate(data map[string]any, schema CustomSchema) []ValidationError

Validate validates data against the provided schema

func ValidateWithDefaults

func ValidateWithDefaults(data map[string]any, schema CustomSchema) (map[string]any, []ValidationError)

ValidateWithDefaults validates data against the schema and applies defaults

func (ValidationError) Error

func (e ValidationError) Error() string

type ValidatorDefinition

type ValidatorDefinition struct {
	Type  string `json:"type" validate:"required,oneof=minLength maxLength pattern enum min max minItems maxItems uniqueItems"`
	Value any    `json:"value" validate:"required"`
}

ValidatorDefinition defines a validation rule

Jump to

Keyboard shortcuts

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