Documentation
¶
Index ¶
- type Condition
- type Spec
- type StructTagOptions
- type TestCase
- type TestCaseFile
- type ValidationError
- type ValidationResult
- func Validate(data any, spec *Spec) *ValidationResult
- func ValidateAndConvert[T any](data any, spec *Spec) (*ValidationResult, T, error)
- func ValidateJSON(jsonData []byte, spec *Spec) (*ValidationResult, error)
- func ValidateJSONString(jsonStr string, spec *Spec) (*ValidationResult, error)
- func ValidateStruct[T any](data any, specJSON ...string) (*ValidationResult, T, error)
- func ValidateStructValue[T any](structValue T, data any, specJSON ...string) (*ValidationResult, T, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Condition ¶
type Condition struct {
If string `json:"if"` // Expression to evaluate, e.g., "enabled == true", "count > 0"
Then map[string]*Spec `json:"then"` // Spec overrides to apply when condition is true
Else map[string]*Spec `json:"else,omitempty"` // Spec overrides to apply when condition is false
}
Condition defines a conditional validation rule
type Spec ¶
type Spec struct {
Type string `json:"type"` // string, number, integer, boolean, object, array, null
Properties map[string]*Spec `json:"properties,omitempty"` // For object type
Items *Spec `json:"items,omitempty"` // For array type
Required []string `json:"required,omitempty"` // For object type - list of required property names
Conditions []Condition `json:"conditions,omitempty"` // Conditional validation rules for object type
// Constraints
Min *float64 `json:"min,omitempty"` // For number/integer - minimum value
Max *float64 `json:"max,omitempty"` // For number/integer - maximum value
MinLength *int `json:"minLength,omitempty"` // For string/array - minimum length
MaxLength *int `json:"maxLength,omitempty"` // For string/array - maximum length
Pattern *string `json:"pattern,omitempty"` // For string - regex pattern (future: could support regex validation)
Enum []any `json:"enum,omitempty"` // Array of allowed values
AllowEmpty *bool `json:"allowEmpty,omitempty"` // For strings - allows empty string if true
}
Spec defines the validation specification structure
func GetSpecFromStruct ¶
GetSpecFromStruct generates a Spec from a struct type This is a convenience function that can be used to get the spec for a struct type
func MergeSpecs ¶
MergeSpecs merges two specs, with override taking precedence
func ParseSpecString ¶
ParseSpecString parses a JSON string into a Spec
func SpecFromStruct ¶
SpecFromStruct generates a Spec from a struct type using reflection and struct tags
type StructTagOptions ¶
type StructTagOptions struct {
Required bool
Min *float64
Max *float64
MinLength *int
MaxLength *int
Pattern *string
Enum []any
AllowEmpty *bool
}
StructTagOptions holds parsed validation options from struct tags
func ParseStructTag ¶
func ParseStructTag(tag string) (*StructTagOptions, error)
ParseStructTag parses a mowgli struct tag and returns validation options Example: `mowgli:"required,min=0,max=100,minLength=1"`
type TestCase ¶
type TestCase struct {
Name string `json:"name"`
Data map[string]any `json:"data"`
ExpectedValid bool `json:"expectedValid"`
}
TestCase represents a single test case
func LoadTestCases ¶
LoadTestCases loads test cases from a JSON file in the testdata directory
type TestCaseFile ¶
type TestCaseFile struct {
TestCases []TestCase `json:"testCases"`
}
TestCaseFile represents a file containing multiple test cases
type ValidationError ¶
ValidationError represents a validation error with a path to the field
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string
type ValidationResult ¶
type ValidationResult struct {
Valid bool
Errors []*ValidationError
}
ValidationResult contains the result of validation
func Validate ¶
func Validate(data any, spec *Spec) *ValidationResult
Validate validates a JSON value against a spec
func ValidateAndConvert ¶
func ValidateAndConvert[T any](data any, spec *Spec) (*ValidationResult, T, error)
ValidateAndConvert validates data and converts it to the target type This is a convenience function that combines validation and conversion
func ValidateJSON ¶
func ValidateJSON(jsonData []byte, spec *Spec) (*ValidationResult, error)
ValidateJSON validates a JSON byte slice against a spec
func ValidateJSONString ¶
func ValidateJSONString(jsonStr string, spec *Spec) (*ValidationResult, error)
ValidateJSONString validates a JSON string against a spec
func ValidateStruct ¶
func ValidateStruct[T any](data any, specJSON ...string) (*ValidationResult, T, error)
ValidateStruct validates data against a struct type and returns a typed result T is the struct type to validate against data is the raw data (map[string]any or compatible) specJSON is an optional JSON spec that will be merged on top of the struct-generated spec
func ValidateStructValue ¶
func ValidateStructValue[T any](structValue T, data any, specJSON ...string) (*ValidationResult, T, error)
ValidateStructValue validates data against a struct value (empty instance) and returns a typed result structValue should be an empty struct instance like MyStruct{} data is the raw data (map[string]any or compatible) specJSON is an optional JSON spec that will be merged on top of the struct-generated spec