Documentation
¶
Index ¶
- Variables
- func NewMapDataProvider[T any](m map[string]T) p.DataProviderdeprecated
- func TestFunc(IssueCode zconst.ZogIssueCode, validateFunc p.TestFunc) p.Test
- type BoolSchema
- func (v *BoolSchema) Catch(val bool) *BoolSchema
- func (v *BoolSchema) Default(val bool) *BoolSchema
- func (v *BoolSchema) False() *BoolSchema
- func (v *BoolSchema) Optional() *BoolSchema
- func (v *BoolSchema) Parse(data any, dest *bool, options ...ExecOption) p.ZogIssueList
- func (v *BoolSchema) PostTransform(transform p.PostTransform) *BoolSchema
- func (v *BoolSchema) PreTransform(transform p.PreTransform) *BoolSchema
- func (v *BoolSchema) Required(options ...TestOption) *BoolSchema
- func (v *BoolSchema) Test(t p.Test, options ...TestOption) *BoolSchema
- func (v *BoolSchema) TestFunc(testFunc p.TestFunc, options ...TestOption) *BoolSchema
- func (v *BoolSchema) True() *BoolSchema
- func (v *BoolSchema) Validate(val *bool, options ...ExecOption) p.ZogIssueList
- type ComplexZogSchema
- type Ctx
- type ExecOption
- type NumberSchema
- func Float(opts ...SchemaOption) *NumberSchema[float64]deprecated
- func Float32(opts ...SchemaOption) *NumberSchema[float32]
- func Float64(opts ...SchemaOption) *NumberSchema[float64]
- func Int(opts ...SchemaOption) *NumberSchema[int]
- func Int32(opts ...SchemaOption) *NumberSchema[int32]
- func Int64(opts ...SchemaOption) *NumberSchema[int64]
- func (v *NumberSchema[T]) Catch(val T) *NumberSchema[T]
- func (v *NumberSchema[T]) Default(val T) *NumberSchema[T]
- func (v *NumberSchema[T]) EQ(n T, options ...TestOption) *NumberSchema[T]
- func (v *NumberSchema[T]) GT(n T, options ...TestOption) *NumberSchema[T]
- func (v *NumberSchema[T]) GTE(n T, options ...TestOption) *NumberSchema[T]
- func (v *NumberSchema[T]) LT(n T, options ...TestOption) *NumberSchema[T]
- func (v *NumberSchema[T]) LTE(n T, options ...TestOption) *NumberSchema[T]
- func (v *NumberSchema[T]) OneOf(enum []T, options ...TestOption) *NumberSchema[T]
- func (v *NumberSchema[T]) Optional() *NumberSchema[T]
- func (v *NumberSchema[T]) Parse(data any, dest *T, options ...ExecOption) p.ZogIssueList
- func (v *NumberSchema[T]) PostTransform(transform p.PostTransform) *NumberSchema[T]
- func (v *NumberSchema[T]) PreTransform(transform p.PreTransform) *NumberSchema[T]
- func (v *NumberSchema[T]) Required(options ...TestOption) *NumberSchema[T]
- func (v *NumberSchema[T]) Test(t p.Test, opts ...TestOption) *NumberSchema[T]
- func (v *NumberSchema[T]) TestFunc(testFunc p.TestFunc, options ...TestOption) *NumberSchema[T]
- func (v *NumberSchema[T]) Validate(data *T, options ...ExecOption) p.ZogIssueList
- type Numeric
- type ParseCtxdeprecated
- type ParsingOptiondeprecated
- type PointerSchema
- type PrimitiveZogSchema
- type Schema
- type SchemaOption
- type SliceSchema
- func (v *SliceSchema) Contains(value any, options ...TestOption) *SliceSchema
- func (v *SliceSchema) Default(val any) *SliceSchema
- func (v *SliceSchema) Len(n int, options ...TestOption) *SliceSchema
- func (v *SliceSchema) Max(n int, options ...TestOption) *SliceSchema
- func (v *SliceSchema) Min(n int, options ...TestOption) *SliceSchema
- func (v *SliceSchema) Optional() *SliceSchema
- func (v *SliceSchema) Parse(data any, dest any, options ...ExecOption) p.ZogIssueMap
- func (v *SliceSchema) PostTransform(transform p.PostTransform) *SliceSchema
- func (v *SliceSchema) PreTransform(transform p.PreTransform) *SliceSchema
- func (v *SliceSchema) Required(options ...TestOption) *SliceSchema
- func (v *SliceSchema) Test(t p.Test, opts ...TestOption) *SliceSchema
- func (v *SliceSchema) TestFunc(testFunc p.TestFunc, options ...TestOption) *SliceSchema
- func (v *SliceSchema) Validate(data any, options ...ExecOption) p.ZogIssueMap
- type StringSchema
- func (v *StringSchema) Catch(val string) *StringSchema
- func (v *StringSchema) Contains(sub string, options ...TestOption) *StringSchema
- func (v *StringSchema) ContainsDigit(options ...TestOption) *StringSchema
- func (v *StringSchema) ContainsSpecial(options ...TestOption) *StringSchema
- func (v *StringSchema) ContainsUpper(options ...TestOption) *StringSchema
- func (v *StringSchema) Default(val string) *StringSchema
- func (v *StringSchema) Email(options ...TestOption) *StringSchema
- func (v *StringSchema) HasPrefix(s string, options ...TestOption) *StringSchema
- func (v *StringSchema) HasSuffix(s string, options ...TestOption) *StringSchema
- func (v *StringSchema) Len(n int, options ...TestOption) *StringSchema
- func (v *StringSchema) Match(regex *regexp.Regexp, options ...TestOption) *StringSchema
- func (v *StringSchema) Max(n int, options ...TestOption) *StringSchema
- func (v *StringSchema) Min(n int, options ...TestOption) *StringSchema
- func (v *StringSchema) OneOf(enum []string, options ...TestOption) *StringSchema
- func (v *StringSchema) Optional() *StringSchema
- func (v *StringSchema) Parse(data any, dest *string, options ...ExecOption) p.ZogIssueList
- func (v *StringSchema) PostTransform(transform p.PostTransform) *StringSchema
- func (v *StringSchema) PreTransform(transform p.PreTransform) *StringSchema
- func (v *StringSchema) Required(options ...TestOption) *StringSchema
- func (v *StringSchema) Test(t p.Test, opts ...TestOption) *StringSchema
- func (v *StringSchema) TestFunc(testFunc p.TestFunc, options ...TestOption) *StringSchema
- func (v *StringSchema) Trim() *StringSchema
- func (v *StringSchema) URL(options ...TestOption) *StringSchema
- func (v *StringSchema) UUID(options ...TestOption) *StringSchema
- func (v *StringSchema) Validate(data *string, options ...ExecOption) p.ZogIssueList
- type StructSchema
- func (v *StructSchema) Extend(schema Schema) *StructSchema
- func (v *StructSchema) Merge(other *StructSchema, others ...*StructSchema) *StructSchema
- func (v *StructSchema) Omit(vals ...any) *StructSchema
- func (v *StructSchema) Optional() *StructSchemadeprecated
- func (v *StructSchema) Parse(data any, destPtr any, options ...ExecOption) p.ZogIssueMap
- func (v *StructSchema) Pick(picks ...any) *StructSchema
- func (v *StructSchema) PostTransform(transform p.PostTransform) *StructSchema
- func (v *StructSchema) PreTransform(transform p.PreTransform) *StructSchema
- func (v *StructSchema) Required(options ...TestOption) *StructSchemadeprecated
- func (v *StructSchema) Test(t p.Test, opts ...TestOption) *StructSchema
- func (v *StructSchema) TestFunc(testFunc p.TestFunc, options ...TestOption) *StructSchema
- func (v *StructSchema) Validate(dataPtr any, options ...ExecOption) p.ZogIssueMap
- type Test
- type TestOption
- type TimeFunc
- type TimeSchema
- func (v *TimeSchema) After(t time.Time, opts ...TestOption) *TimeSchema
- func (v *TimeSchema) Before(t time.Time, opts ...TestOption) *TimeSchema
- func (v *TimeSchema) Catch(val time.Time) *TimeSchema
- func (v *TimeSchema) Default(val time.Time) *TimeSchema
- func (v *TimeSchema) EQ(t time.Time, opts ...TestOption) *TimeSchema
- func (v *TimeSchema) Optional() *TimeSchema
- func (v *TimeSchema) Parse(data any, dest *time.Time, options ...ExecOption) p.ZogIssueList
- func (v *TimeSchema) PostTransform(transform p.PostTransform) *TimeSchema
- func (v *TimeSchema) PreTransform(transform p.PreTransform) *TimeSchema
- func (v *TimeSchema) Required(options ...TestOption) *TimeSchema
- func (v *TimeSchema) Test(t p.Test, opts ...TestOption) *TimeSchema
- func (v *TimeSchema) TestFunc(testFunc p.TestFunc, options ...TestOption) *TimeSchema
- func (v *TimeSchema) Validate(data *time.Time, options ...ExecOption) p.ZogIssueList
- type WhateverSchema
- type ZogErrListdeprecated
- type ZogErrMapdeprecated
- type ZogErrordeprecated
- type ZogIssue
- type ZogIssueList
- type ZogIssueMap
- type ZogSchema
Constants ¶
This section is empty.
Variables ¶
var Errors = errHelpers{}
Deprecated: This will be removed in the future. Use z.Issues instead Helper struct for dealing with zog errors. Beware this API may change
var Issues = issueHelpers{}
Functions ¶
func NewMapDataProvider
deprecated
func NewMapDataProvider[T any](m map[string]T) p.DataProvider
Deprecated: This will be removed in the future. You should just pass your map[string]T to the schema.Parse() function directly without using this: old: schema.Parse(z.NewMapDataProvider(m), &dest) new: schema.Parse(m, &dest)
Types ¶
type BoolSchema ¶
type BoolSchema struct {
// contains filtered or unexported fields
}
func (*BoolSchema) Catch ¶
func (v *BoolSchema) Catch(val bool) *BoolSchema
sets the catch value (i.e the value to use if the validation fails)
func (*BoolSchema) Default ¶
func (v *BoolSchema) Default(val bool) *BoolSchema
sets the default value
func (*BoolSchema) False ¶
func (v *BoolSchema) False() *BoolSchema
func (*BoolSchema) Parse ¶
func (v *BoolSchema) Parse(data any, dest *bool, options ...ExecOption) p.ZogIssueList
Parse data into destination pointer
func (*BoolSchema) PostTransform ¶
func (v *BoolSchema) PostTransform(transform p.PostTransform) *BoolSchema
Adds posttransform function to schema
func (*BoolSchema) PreTransform ¶
func (v *BoolSchema) PreTransform(transform p.PreTransform) *BoolSchema
Adds pretransform function to schema
func (*BoolSchema) Required ¶
func (v *BoolSchema) Required(options ...TestOption) *BoolSchema
! MODIFIERS marks field as required
func (*BoolSchema) Test ¶
func (v *BoolSchema) Test(t p.Test, options ...TestOption) *BoolSchema
func (*BoolSchema) TestFunc ¶
func (v *BoolSchema) TestFunc(testFunc p.TestFunc, options ...TestOption) *BoolSchema
Create a custom test function for the schema. This is similar to Zod's `.refine()` method.
func (*BoolSchema) True ¶
func (v *BoolSchema) True() *BoolSchema
func (*BoolSchema) Validate ¶
func (v *BoolSchema) Validate(val *bool, options ...ExecOption) p.ZogIssueList
Validate data against schema
type ComplexZogSchema ¶
type ComplexZogSchema interface {
ZogSchema
Parse(val any, dest any, options ...ExecOption) ZogIssueMap
}
This is a common interface for all complex schemas (i.e structs, slices, pointers...) You can use this to pass any complex schema around
type Ctx ¶
This is the context that is passed through an entire execution of `schema.Parse()` or `schema.Validate()`. You can use it to pass a key/value for a specific execution. More about context in the [docs](https://zog.dev/context)
type ExecOption ¶
Options that can be passed to a `schema.Parse()` call
func WithCtxValue ¶
func WithCtxValue(key string, val any) ExecOption
func WithErrFormatter
deprecated
func WithErrFormatter(fmter p.IssueFmtFunc) ExecOption
Deprecated: use WithIssueFormatter instead Deprecated for naming consistency
func WithIssueFormatter ¶
func WithIssueFormatter(fmter p.IssueFmtFunc) ExecOption
Sets the issue formatter for the execution context. This is used to format the issues messages during execution. This follows principle of most specific wins. So default formatter < execution formatter < test specific formatter (i.e MessageFunc)
type NumberSchema ¶
type NumberSchema[T Numeric] struct { // contains filtered or unexported fields }
func Float
deprecated
func Float(opts ...SchemaOption) *NumberSchema[float64]
Deprecated: Use Float64 instead creates a new float64 schema
func Float32 ¶
func Float32(opts ...SchemaOption) *NumberSchema[float32]
func Float64 ¶
func Float64(opts ...SchemaOption) *NumberSchema[float64]
func Int32 ¶
func Int32(opts ...SchemaOption) *NumberSchema[int32]
func Int64 ¶
func Int64(opts ...SchemaOption) *NumberSchema[int64]
func (*NumberSchema[T]) Catch ¶
func (v *NumberSchema[T]) Catch(val T) *NumberSchema[T]
sets the catch value (i.e the value to use if the validation fails)
func (*NumberSchema[T]) Default ¶
func (v *NumberSchema[T]) Default(val T) *NumberSchema[T]
sets the default value
func (*NumberSchema[T]) EQ ¶
func (v *NumberSchema[T]) EQ(n T, options ...TestOption) *NumberSchema[T]
checks for equality
func (*NumberSchema[T]) GT ¶
func (v *NumberSchema[T]) GT(n T, options ...TestOption) *NumberSchema[T]
checks for greater
func (*NumberSchema[T]) GTE ¶
func (v *NumberSchema[T]) GTE(n T, options ...TestOption) *NumberSchema[T]
checks for greater or equal
func (*NumberSchema[T]) LT ¶
func (v *NumberSchema[T]) LT(n T, options ...TestOption) *NumberSchema[T]
checks for lesser
func (*NumberSchema[T]) LTE ¶
func (v *NumberSchema[T]) LTE(n T, options ...TestOption) *NumberSchema[T]
checks for lesser or equal
func (*NumberSchema[T]) OneOf ¶
func (v *NumberSchema[T]) OneOf(enum []T, options ...TestOption) *NumberSchema[T]
Check that the value is one of the enum values
func (*NumberSchema[T]) Optional ¶
func (v *NumberSchema[T]) Optional() *NumberSchema[T]
marks field as optional
func (*NumberSchema[T]) Parse ¶
func (v *NumberSchema[T]) Parse(data any, dest *T, options ...ExecOption) p.ZogIssueList
parses the value and stores it in the destination
func (*NumberSchema[T]) PostTransform ¶
func (v *NumberSchema[T]) PostTransform(transform p.PostTransform) *NumberSchema[T]
Adds posttransform function to schema
func (*NumberSchema[T]) PreTransform ¶
func (v *NumberSchema[T]) PreTransform(transform p.PreTransform) *NumberSchema[T]
func (*NumberSchema[T]) Required ¶
func (v *NumberSchema[T]) Required(options ...TestOption) *NumberSchema[T]
marks field as required
func (*NumberSchema[T]) Test ¶
func (v *NumberSchema[T]) Test(t p.Test, opts ...TestOption) *NumberSchema[T]
custom test function call it -> schema.Test(test, options)
func (*NumberSchema[T]) TestFunc ¶
func (v *NumberSchema[T]) TestFunc(testFunc p.TestFunc, options ...TestOption) *NumberSchema[T]
Create a custom test function for the schema. This is similar to Zod's `.refine()` method.
func (*NumberSchema[T]) Validate ¶
func (v *NumberSchema[T]) Validate(data *T, options ...ExecOption) p.ZogIssueList
Validates a number pointer
type Numeric ¶
type Numeric = constraints.Ordered
type ParsingOption
deprecated
type ParsingOption = ExecOption
Deprecated: use ExecOption instead
type PointerSchema ¶
type PointerSchema struct {
// contains filtered or unexported fields
}
func (*PointerSchema) NotNil ¶
func (v *PointerSchema) NotNil(options ...TestOption) *PointerSchema
func (*PointerSchema) Parse ¶
func (v *PointerSchema) Parse(data any, dest any, options ...ExecOption) p.ZogIssueMap
Parse the data into the destination pointer
func (*PointerSchema) Validate ¶
func (v *PointerSchema) Validate(data any, options ...ExecOption) p.ZogIssueMap
Validates a pointer pointer
type PrimitiveZogSchema ¶
type PrimitiveZogSchema[T p.ZogPrimitive] interface { ZogSchema Parse(val any, dest *T, options ...ExecOption) ZogIssueList }
This is a common interface for all primitive schemas (i.e strings, numbers, booleans, time.Time...) You can use this to pass any primitive schema around
type SchemaOption ¶
type SchemaOption = func(s ZogSchema)
Options that can be passed to a `schema.New()` call
func WithCoercer ¶
func WithCoercer(c conf.CoercerFunc) SchemaOption
type SliceSchema ¶
type SliceSchema struct {
// contains filtered or unexported fields
}
func Slice ¶
func Slice(schema ZogSchema, opts ...SchemaOption) *SliceSchema
Creates a slice schema. That is a Zog representation of a slice. It takes a ZogSchema which will be used to validate against all the items in the slice.
func (*SliceSchema) Contains ¶
func (v *SliceSchema) Contains(value any, options ...TestOption) *SliceSchema
Slice contains a specific value
func (*SliceSchema) Default ¶
func (v *SliceSchema) Default(val any) *SliceSchema
sets the default value
func (*SliceSchema) Len ¶
func (v *SliceSchema) Len(n int, options ...TestOption) *SliceSchema
Exact number of items
func (*SliceSchema) Max ¶
func (v *SliceSchema) Max(n int, options ...TestOption) *SliceSchema
Maximum number of items
func (*SliceSchema) Min ¶
func (v *SliceSchema) Min(n int, options ...TestOption) *SliceSchema
Minimum number of items
func (*SliceSchema) Optional ¶
func (v *SliceSchema) Optional() *SliceSchema
marks field as optional
func (*SliceSchema) Parse ¶
func (v *SliceSchema) Parse(data any, dest any, options ...ExecOption) p.ZogIssueMap
Only supports parsing from data=slice[any] to a dest =&slice[] (this can be typed. Doesn't have to be any)
func (*SliceSchema) PostTransform ¶
func (v *SliceSchema) PostTransform(transform p.PostTransform) *SliceSchema
Adds posttransform function to schema
func (*SliceSchema) PreTransform ¶
func (v *SliceSchema) PreTransform(transform p.PreTransform) *SliceSchema
Adds pretransform function to schema
func (*SliceSchema) Required ¶
func (v *SliceSchema) Required(options ...TestOption) *SliceSchema
marks field as required
func (*SliceSchema) Test ¶
func (v *SliceSchema) Test(t p.Test, opts ...TestOption) *SliceSchema
custom test function call it -> schema.Test(t z.Test, opts ...TestOption)
func (*SliceSchema) TestFunc ¶
func (v *SliceSchema) TestFunc(testFunc p.TestFunc, options ...TestOption) *SliceSchema
Create a custom test function for the schema. This is similar to Zod's `.refine()` method.
func (*SliceSchema) Validate ¶
func (v *SliceSchema) Validate(data any, options ...ExecOption) p.ZogIssueMap
Validates a slice
type StringSchema ¶
type StringSchema struct {
// contains filtered or unexported fields
}
func (*StringSchema) Catch ¶
func (v *StringSchema) Catch(val string) *StringSchema
sets the catch value (i.e the value to use if the validation fails)
func (*StringSchema) Contains ¶
func (v *StringSchema) Contains(sub string, options ...TestOption) *StringSchema
Test: checks that the value contains the substring
func (*StringSchema) ContainsDigit ¶
func (v *StringSchema) ContainsDigit(options ...TestOption) *StringSchema
Test: checks that the value contains a digit
func (*StringSchema) ContainsSpecial ¶
func (v *StringSchema) ContainsSpecial(options ...TestOption) *StringSchema
Test: checks that the value contains a special character
func (*StringSchema) ContainsUpper ¶
func (v *StringSchema) ContainsUpper(options ...TestOption) *StringSchema
Test: checks that the value contains an uppercase letter
func (*StringSchema) Default ¶
func (v *StringSchema) Default(val string) *StringSchema
sets the default value
func (*StringSchema) Email ¶
func (v *StringSchema) Email(options ...TestOption) *StringSchema
Test: checks that the value is a valid email address
func (*StringSchema) HasPrefix ¶
func (v *StringSchema) HasPrefix(s string, options ...TestOption) *StringSchema
Test: checks that the value has the prefix
func (*StringSchema) HasSuffix ¶
func (v *StringSchema) HasSuffix(s string, options ...TestOption) *StringSchema
Test: checks that the value has the suffix
func (*StringSchema) Len ¶
func (v *StringSchema) Len(n int, options ...TestOption) *StringSchema
Test: checks that the value is exactly n characters long
func (*StringSchema) Match ¶
func (v *StringSchema) Match(regex *regexp.Regexp, options ...TestOption) *StringSchema
Test: checks that value matches to regex
func (*StringSchema) Max ¶
func (v *StringSchema) Max(n int, options ...TestOption) *StringSchema
Test: checks that the value is at most n characters long
func (*StringSchema) Min ¶
func (v *StringSchema) Min(n int, options ...TestOption) *StringSchema
Test: checks that the value is at least n characters long
func (*StringSchema) OneOf ¶
func (v *StringSchema) OneOf(enum []string, options ...TestOption) *StringSchema
Test: checks that the value is one of the enum values
func (*StringSchema) Optional ¶
func (v *StringSchema) Optional() *StringSchema
marks field as optional
func (*StringSchema) Parse ¶
func (v *StringSchema) Parse(data any, dest *string, options ...ExecOption) p.ZogIssueList
Parses the data into the destination string. Returns a list of ZogIssues
func (*StringSchema) PostTransform ¶
func (v *StringSchema) PostTransform(transform p.PostTransform) *StringSchema
Adds posttransform function to schema
func (*StringSchema) PreTransform ¶
func (v *StringSchema) PreTransform(transform p.PreTransform) *StringSchema
Adds pretransform function to schema
func (*StringSchema) Required ¶
func (v *StringSchema) Required(options ...TestOption) *StringSchema
marks field as required
func (*StringSchema) Test ¶
func (v *StringSchema) Test(t p.Test, opts ...TestOption) *StringSchema
! Tests custom test function call it -> schema.Test(t z.Test, opts ...TestOption)
func (*StringSchema) TestFunc ¶
func (v *StringSchema) TestFunc(testFunc p.TestFunc, options ...TestOption) *StringSchema
Create a custom test function for the schema. This is similar to Zod's `.refine()` method.
func (*StringSchema) Trim ¶
func (v *StringSchema) Trim() *StringSchema
PreTransform: trims the input data of whitespace if it is a string
func (*StringSchema) URL ¶
func (v *StringSchema) URL(options ...TestOption) *StringSchema
Test: checks that the value is a valid URL
func (*StringSchema) UUID ¶
func (v *StringSchema) UUID(options ...TestOption) *StringSchema
Test: checks that the value is a valid uuid
func (*StringSchema) Validate ¶
func (v *StringSchema) Validate(data *string, options ...ExecOption) p.ZogIssueList
Validate Given string
type StructSchema ¶
type StructSchema struct {
// contains filtered or unexported fields
}
func Struct ¶
func Struct(schema Schema) *StructSchema
Returns a new StructSchema which can be used to parse input data into a struct
func (*StructSchema) Extend ¶
func (v *StructSchema) Extend(schema Schema) *StructSchema
Extend creates a new schema by adding additional fields from the provided schema. Fields in the provided schema override any existing fields with the same key.
Parameters:
- schema: The schema containing fields to add
Returns a new schema with the additional fields
func (*StructSchema) Merge ¶
func (v *StructSchema) Merge(other *StructSchema, others ...*StructSchema) *StructSchema
Merge combines two or more schemas into a new schema. It performs a shallow merge, meaning:
- Fields with the same key from later schemas override earlier ones
- PreTransforms, PostTransforms and tests are concatenated in order
- Modifying nested schemas may affect the original schemas
Parameters:
- other: The first schema to merge with
- others: Additional schemas to merge
Returns a new schema containing the merged fields and transforms
func (*StructSchema) Omit ¶
func (v *StructSchema) Omit(vals ...any) *StructSchema
Omit creates a new schema with specified fields removed. It accepts either strings or map[string]bool as arguments:
- Strings directly specify fields to omit
- For maps, fields are omitted when their boolean value is true
Returns a new schema with the specified fields removed
func (*StructSchema) Optional
deprecated
func (v *StructSchema) Optional() *StructSchema
Deprecated: structs are not required or optional. They pass through to the fields. If you want to say that an entire struct may not exist you should use z.Ptr(z.Struct(...)) marks field as optional
func (*StructSchema) Parse ¶
func (v *StructSchema) Parse(data any, destPtr any, options ...ExecOption) p.ZogIssueMap
Parses val into destPtr and validates each field based on the schema. Only supports val = map[string]any & dest = &struct
func (*StructSchema) Pick ¶
func (v *StructSchema) Pick(picks ...any) *StructSchema
Pick creates a new schema keeping only the specified fields. It accepts either strings or map[string]bool as arguments:
- Strings directly specify fields to keep
- For maps, fields are kept when their boolean value is true
Returns a new schema containing only the specified fields
func (*StructSchema) PostTransform ¶
func (v *StructSchema) PostTransform(transform p.PostTransform) *StructSchema
Adds posttransform function to schema
func (*StructSchema) PreTransform ¶
func (v *StructSchema) PreTransform(transform p.PreTransform) *StructSchema
Add a pretransform step to the schema
func (*StructSchema) Required
deprecated
func (v *StructSchema) Required(options ...TestOption) *StructSchema
Deprecated: structs are not required or optional. They pass through to the fields. If you want to say that an entire struct may not exist you should use z.Ptr(z.Struct(...)) This now is a noop. But I believe most people expect it to work how it does now. marks field as required
func (*StructSchema) Test ¶
func (v *StructSchema) Test(t p.Test, opts ...TestOption) *StructSchema
! VALIDATORS custom test function call it -> schema.Test(t z.Test, opts ...TestOption)
func (*StructSchema) TestFunc ¶
func (v *StructSchema) TestFunc(testFunc p.TestFunc, options ...TestOption) *StructSchema
Create a custom test function for the schema. This is similar to Zod's `.refine()` method.
func (*StructSchema) Validate ¶
func (v *StructSchema) Validate(dataPtr any, options ...ExecOption) p.ZogIssueMap
Validate a struct pointer given the struct schema. Usage: userSchema.Validate(&User, ...options)
type Test ¶
Test is the test object. It is the struct that represents an individual validation. For example `z.String().Min(3)` is a test that checks if the string is at least 3 characters long.
type TestOption ¶
Options that can be passed to a test
func IssueCode ¶
func IssueCode(code zconst.ZogIssueCode) TestOption
IssueCode is a function that allows you to set a custom issue code for the test. Most useful for TestFuncs:
z.String().TestFunc(..., z.IssueCode("just_provide_a_string" or use values in zconst))
func IssuePath ¶
func IssuePath(path string) TestOption
IssuePath is a function that allows you to set a custom issue path for the test. Beware with using this as it is not typesafe and can lead to unexpected behavior if you change the schema or have a typo. Usage:
z.Struct(
z.Schema {
"Name": z.String().Required(z.IssuePath("fullname")),
"Fullname": z.String(),
}
)
func Message ¶
func Message(msg string) TestOption
Message is a function that allows you to set a custom message for the test.
func MessageFunc ¶
func MessageFunc(fn p.IssueFmtFunc) TestOption
MessageFunc is a function that allows you to set a custom message formatter for the test.
func Params ¶
func Params(params map[string]any) TestOption
Params is a function that allows you to set a custom params for the test. You may then access these values when formatting test errors in the IssueFmtFunc
type TimeFunc ¶
type TimeFunc func(opts ...SchemaOption) *TimeSchema
var Time TimeFunc = func(opts ...SchemaOption) *TimeSchema { t := &TimeSchema{ coercer: conf.Coercers.Time, } for _, opt := range opts { opt(t) } return t }
Returns a new Time Schema
func (TimeFunc) Format ¶
func (t TimeFunc) Format(format string) SchemaOption
WARNING ONLY SUPPOORTS Schema.Parse! Sets the string format for the time schema Usage is: z.Time(z.Time.Format(time.RFC3339))
func (TimeFunc) FormatFunc ¶
WARNING ONLY SUPPOORTS Schema.Parse! Sets the format function for the time schema. Usage is:
z.Time(z.Time.FormatFunc(func(data string) (time.Time, error) {
return time.Parse(time.RFC3339, data)
}))
type TimeSchema ¶
type TimeSchema struct {
// contains filtered or unexported fields
}
func (*TimeSchema) After ¶
func (v *TimeSchema) After(t time.Time, opts ...TestOption) *TimeSchema
Checks that the value is after the given time
func (*TimeSchema) Before ¶
func (v *TimeSchema) Before(t time.Time, opts ...TestOption) *TimeSchema
Checks that the value is before the given time
func (*TimeSchema) Catch ¶
func (v *TimeSchema) Catch(val time.Time) *TimeSchema
sets the catch value (i.e the value to use if the validation fails)
func (*TimeSchema) Default ¶
func (v *TimeSchema) Default(val time.Time) *TimeSchema
sets the default value
func (*TimeSchema) EQ ¶
func (v *TimeSchema) EQ(t time.Time, opts ...TestOption) *TimeSchema
Checks that the value is equal to the given time
func (*TimeSchema) Parse ¶
func (v *TimeSchema) Parse(data any, dest *time.Time, options ...ExecOption) p.ZogIssueList
Parses the data into the destination time.Time. Returns a list of errors
func (*TimeSchema) PostTransform ¶
func (v *TimeSchema) PostTransform(transform p.PostTransform) *TimeSchema
Adds posttransform function to schema
func (*TimeSchema) PreTransform ¶
func (v *TimeSchema) PreTransform(transform p.PreTransform) *TimeSchema
Adds pretransform function to schema
func (*TimeSchema) Required ¶
func (v *TimeSchema) Required(options ...TestOption) *TimeSchema
marks field as required
func (*TimeSchema) Test ¶
func (v *TimeSchema) Test(t p.Test, opts ...TestOption) *TimeSchema
custom test function call it -> schema.Test("error_code", func(val any, ctx ParseCtx) bool {return true})
func (*TimeSchema) TestFunc ¶
func (v *TimeSchema) TestFunc(testFunc p.TestFunc, options ...TestOption) *TimeSchema
Create a custom test function for the schema. This is similar to Zod's `.refine()` method.
func (*TimeSchema) Validate ¶
func (v *TimeSchema) Validate(data *time.Time, options ...ExecOption) p.ZogIssueList
Validates an existing time.Time
type WhateverSchema ¶
type WhateverSchema struct {
// contains filtered or unexported fields
}
func (*WhateverSchema) Parse ¶
func (v *WhateverSchema) Parse(data any, dest any, options ...ExecOption) p.ZogIssueMap
Parse the data into the destination Whatever
func (*WhateverSchema) Validate ¶
func (v *WhateverSchema) Validate(data any, options ...ExecOption) p.ZogIssueMap
Validates a Whatever Whatever
type ZogErrList
deprecated
type ZogErrList = p.ZogIssueList
Deprecated: This will be removed in the future. Use z.ZogIssueList instead This is a type for the ZogErrList type. It is a list of ZogIssues returned from parsing primitive schemas. The type is []ZogError
type ZogErrMap
deprecated
type ZogErrMap = p.ZogIssueMap
Deprecated: This will be removed in the future. Use z.ZogIssueMap instead This is a type for the ZogErrMap type. It is a map[string][]ZogError returned from parsing complex schemas. The type is map[string][]ZogError All errors are returned in a flat map, not matter how deep the schema is. For example:
schema := z.Struct(z.Schema{
"address": z.Struct(z.Schema{
"street": z.String().Min(3).Max(10),
"city": z.String().Min(3).Max(10),
}),
"fields": z.Slice(z.String().Min(3).Max(10)),
})
errors = map[string][]ZogError{
"address.street": []ZogError{....}, // error for the street field in the address struct
"fields[0]": []ZogError{...}, // error for the first field in the slice
}
type ZogIssue ¶
This is a type for the ZogIssue= interface. It is the interface that all errors returned from zog implement.
type ZogIssueList ¶
type ZogIssueList = p.ZogIssueList
This is a type for the ZogErrList type. It is a list of ZogIssues returned from parsing primitive schemas. The type is []ZogIssue
type ZogIssueMap ¶
type ZogIssueMap = p.ZogIssueMap
This is a type for the ZogIssueMap type. It is a map[string][]ZogIssue returned from parsing complex schemas. The type is map[string][]ZogIssue All errors are returned in a flat map, not matter how deep the schema is. For example:
schema := z.Struct(z.Schema{
"address": z.Struct(z.Schema{
"street": z.String().Min(3).Max(10),
"city": z.String().Min(3).Max(10),
}),
"fields": z.Slice(z.String().Min(3).Max(10)),
})
errors = map[string][]ZogIssue{
"address.street": []ZogIssue{....}, // error for the street field in the address struct
"fields[0]": []ZogIssue{...}, // error for the first field in the slice
}