Documentation
¶
Index ¶
- Variables
- func Clear()
- func ClearPools()
- func EQ[T comparable](n T) (Test, BoolTFunc)
- func FreeIssue(i *ZogIssue)
- func FreeStringBuilder(sb *strings.Builder)
- func GT[T constraints.Ordered](n T) (Test, BoolTFunc)
- func GTE[T constraints.Ordered](n T) (Test, BoolTFunc)
- func GetKeyFromField(field reflect.StructField, fallback string, tag *string) string
- func In[T any](values []T) (Test, BoolTFunc)
- func IsParseZeroValue(val any, ctx Ctx) bool
- func IsZeroValue(x any) bool
- func LT[T constraints.Ordered](n T) (Test, BoolTFunc)
- func LTE[T constraints.Ordered](n T) (Test, BoolTFunc)
- func Len[T LengthCapable[any]](n int) (Test, BoolTFunc)
- func LenMax[T LengthCapable[any]](n int) (Test, BoolTFunc)
- func LenMin[T LengthCapable[any]](n int) (Test, BoolTFunc)
- func NewStringBuilder() *strings.Builder
- func SafeError(x error) string
- func SafeString(x any) string
- func TestFuncFromBool(fn BoolTFunc, test *Test)
- func TestNotFuncFromBool(fn BoolTFunc, test *Test)
- type BoolTFunc
- type Ctx
- type DataProvider
- type DpFactory
- type EmptyDataProvider
- type ErrsList
- type ErrsMap
- type ExecCtx
- func (c *ExecCtx) AddIssue(e *ZogIssue)
- func (c *ExecCtx) FmtErr(e *ZogIssue)
- func (c *ExecCtx) Free()
- func (c *ExecCtx) Get(key string) any
- func (c *ExecCtx) HasErrored() bool
- func (c *ExecCtx) Issue() *ZogIssue
- func (c *ExecCtx) NewError(path *PathBuilder, e *ZogIssue)deprecated
- func (c *ExecCtx) NewSchemaCtx(val any, destPtr any, path *PathBuilder, dtype zconst.ZogType) *SchemaCtx
- func (c *ExecCtx) NewValidateSchemaCtx(valPtr any, path *PathBuilder, dtype zconst.ZogType) *SchemaCtx
- func (c *ExecCtx) Set(key string, val any)
- func (c *ExecCtx) SetIssueFormatter(fmter IssueFmtFunc)
- type IsZeroValueFunc
- type IssueFmtFunc
- type LengthCapable
- type MapDataProvider
- type PathBuilder
- type PostTransform
- type PreTransform
- type SchemaCtx
- type TFunc
- type Test
- type TestOption
- type ZogIssue
- func (i *ZogIssue) Error() string
- func (i *ZogIssue) SetCode(c zconst.ZogIssueCode) *ZogIssue
- func (i *ZogIssue) SetDType(t string) *ZogIssue
- func (i *ZogIssue) SetError(e error) *ZogIssue
- func (i *ZogIssue) SetMessage(m string) *ZogIssue
- func (i *ZogIssue) SetParams(p map[string]any) *ZogIssue
- func (i *ZogIssue) SetPath(p string) *ZogIssue
- func (i *ZogIssue) SetValue(v any) *ZogIssue
- func (i *ZogIssue) String() string
- func (i *ZogIssue) Unwrap() error
- type ZogIssueList
- type ZogIssueMap
- type ZogIssues
- type ZogPrimitive
Constants ¶
This section is empty.
Variables ¶
var ExecCtxPool = sync.Pool{ New: func() any { return &ExecCtx{} }, }
var InternalIssueListPool = sync.Pool{ New: func() any { return &ErrsList{} }, }
var InternalIssueMapPool = sync.Pool{ New: func() any { return &ErrsMap{} }, }
var PathBuilderPool = sync.Pool{ New: func() any { pb := make(PathBuilder, 0, 5) return &pb }, }
var SchemaCtxPool = sync.Pool{ New: func() any { return &SchemaCtx{} }, }
var StringBuilderPool = sync.Pool{ New: func() any { sb := strings.Builder{} return &sb }, }
var ZogIssuePool = sync.Pool{ New: func() any { return &ZogIssue{} }, }
Functions ¶
func ClearPools ¶ added in v0.16.1
func ClearPools()
func EQ ¶
func EQ[T comparable](n T) (Test, BoolTFunc)
func FreeStringBuilder ¶ added in v0.16.4
func GetKeyFromField ¶ added in v0.18.1
func GetKeyFromField(field reflect.StructField, fallback string, tag *string) string
func IsParseZeroValue ¶ added in v0.12.0
checks if the value is the zero value but only for parsing purposes (i.e the parse function)
func IsZeroValue ¶
checks that the value is the zero value for its type
func NewStringBuilder ¶ added in v0.16.4
func SafeString ¶ added in v0.11.0
func TestFuncFromBool ¶ added in v0.19.0
func TestNotFuncFromBool ¶ added in v0.19.2
Types ¶
type BoolTFunc ¶ added in v0.19.0
A TestFunc that takes the data as input and returns a boolean indicating if it is valid or not
type Ctx ¶ added in v0.16.0
type Ctx interface { /** METHOD YOU ARE FREE TO USE */ // Get a value from the context Get(key string) any // Adds an issue to the schema execution. AddIssue(e *ZogIssue) // Returns a new issue with the current schema context's data prefilled /* Usage: func MyCustomTestFunc(val any, ctx z.Ctx) { if reason1 { ctx.AddIssue(ctx.Issue().SetMessage("Reason 1")) } else if reason2 { ctx.AddIssue(ctx.Issue().SetMessage("Reason 2")) } else { ctx.AddIssue(ctx.Issue().SetMessage("Reason 3")) } } */ Issue() *ZogIssue /** METHOD YOU SHOULD NOT USE */ // Deprecated: Use Ctx.AddIssue() instead // Please don't depend on this interface it may change NewError(p *PathBuilder, e *ZogIssue) // Please don't depend on this interface it may change HasErrored() bool }
Zog Context interface. This is the interface that is passed to schema tests, pre and post transforms
type DataProvider ¶
type DataProvider interface { Get(key string) any GetByField(field reflect.StructField, fallback string) (any, string) GetNestedProvider(key string) DataProvider GetUnderlying() any // returns the underlying value the dp is wrapping }
This is used for parsing structs & maps
func NewMapDataProvider ¶
func NewMapDataProvider[T any](m map[string]T, tag *string) DataProvider
func NewSafeMapDataProvider ¶ added in v0.16.0
func NewSafeMapDataProvider[T any](m map[string]T) DataProvider
func TryNewAnyDataProvider ¶
func TryNewAnyDataProvider(val any) (DataProvider, error)
type DpFactory ¶
type DpFactory = func() (DataProvider, *ZogIssue)
type EmptyDataProvider ¶
type EmptyDataProvider struct {
Underlying any
}
func (*EmptyDataProvider) Get ¶
func (e *EmptyDataProvider) Get(key string) any
func (*EmptyDataProvider) GetByField ¶ added in v0.18.1
func (e *EmptyDataProvider) GetByField(field reflect.StructField, fallback string) (any, string)
func (*EmptyDataProvider) GetNestedProvider ¶
func (e *EmptyDataProvider) GetNestedProvider(key string) DataProvider
func (*EmptyDataProvider) GetUnderlying ¶
func (e *EmptyDataProvider) GetUnderlying() any
type ExecCtx ¶ added in v0.16.0
type ExecCtx struct { Fmter IssueFmtFunc Errors ZogIssues // contains filtered or unexported fields }
func NewExecCtx ¶ added in v0.16.0
func NewExecCtx(errs ZogIssues, fmter IssueFmtFunc) *ExecCtx
func (*ExecCtx) HasErrored ¶ added in v0.16.0
func (*ExecCtx) NewError
deprecated
added in
v0.16.0
func (c *ExecCtx) NewError(path *PathBuilder, e *ZogIssue)
Deprecated: Use Ctx.AddIssue() instead This is old interface. It will be removed soon
func (*ExecCtx) NewSchemaCtx ¶ added in v0.16.0
func (*ExecCtx) NewValidateSchemaCtx ¶ added in v0.16.0
func (*ExecCtx) SetIssueFormatter ¶ added in v0.16.0
func (c *ExecCtx) SetIssueFormatter(fmter IssueFmtFunc)
type IsZeroValueFunc ¶ added in v0.12.0
type IssueFmtFunc ¶ added in v0.16.0
this is the function that formats the error message given a zog error
type LengthCapable ¶
type MapDataProvider ¶
func (*MapDataProvider[T]) Get ¶
func (m *MapDataProvider[T]) Get(key string) any
func (*MapDataProvider[T]) GetByField ¶ added in v0.18.1
func (m *MapDataProvider[T]) GetByField(field reflect.StructField, fallback string) (any, string)
returns value + key used
func (*MapDataProvider[T]) GetNestedProvider ¶
func (m *MapDataProvider[T]) GetNestedProvider(key string) DataProvider
func (*MapDataProvider[T]) GetUnderlying ¶
func (m *MapDataProvider[T]) GetUnderlying() any
type PathBuilder ¶
type PathBuilder []string
func NewPathBuilder ¶ added in v0.16.6
func NewPathBuilder() *PathBuilder
func (*PathBuilder) Free ¶ added in v0.16.6
func (p *PathBuilder) Free()
func (*PathBuilder) Pop ¶
func (p *PathBuilder) Pop()
func (*PathBuilder) Push ¶
func (p *PathBuilder) Push(path *string) *PathBuilder
func (*PathBuilder) String ¶
func (p *PathBuilder) String() string
type PostTransform ¶
type for functions called after validation & parsing is done
type PreTransform ¶
takes the data as input and returns the new data which will then be passed onto the next functions. If the function returns an error all validation will be skipped & the error will be returned. You may return a ZogIssue or an error. If its an error it will be wraped inside a ZogIssue
type SchemaCtx ¶ added in v0.16.0
type SchemaCtx struct { *ExecCtx Val any DestPtr any Path *PathBuilder DType zconst.ZogType CanCatch bool Exit bool HasCaught bool Test *Test }
func (*SchemaCtx) IssueFromCoerce ¶ added in v0.16.0
Please don't depend on this method it may change
func (*SchemaCtx) IssueFromTest ¶ added in v0.16.0
Please don't depend on this method it may change
func (*SchemaCtx) IssueFromUnknownError ¶ added in v0.16.0
Please don't depend on this method it may change Wraps an error in a ZogIssue if it is not already a ZogIssue
type TFunc ¶ added in v0.19.0
TFunc is the function that tests hold that execute on the data for validation. They use the z.Ctx to add issues if needed
type Test ¶
type Test struct { // The issue code that will be used to create the issue if the test fails IssueCode zconst.ZogIssueCode // The path to the issue IssuePath string // The params for the test which are passed to the issue and can be used for customizing issue messages. For example `z.String().Min(3)` will have a param with the key `min` and the value `3`. Params map[string]any // The formatter for the issue. You can use this if you want to customize the message formatter for issues generated by this specific test. Not super useful but just here for completeness. IssueFmtFunc IssueFmtFunc // The function that will be executed when evaluating the test. Func TFunc }
Test is a 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.
func NewTestFunc ¶ added in v0.19.0
func NewTestFunc(IssueCode zconst.ZogIssueCode, fn BoolTFunc, options ...TestOption) *Test
type TestOption ¶ added in v0.19.0
type TestOption = func(test *Test)
TestOption is the option for a test
type ZogIssue ¶ added in v0.16.0
type ZogIssue struct { // Code is the unique identifier for the issue. Generally also the ID for the Test that caused the issue. Code zconst.ZogIssueCode // Path is the path to the field that caused the issue Path string // Value is the data value that caused the issue. // If using Schema.Parse(data, dest) then this will be the value of data. Value any // Dtype is the destination type. i.e The zconst.ZogType of the value that was validated. // If using Schema.Parse(data, dest) then this will be the type of dest. Dtype string // Params is the params map for the issue. Taken from the Test that caused the issue. // This may be nil if Test has no params. Params map[string]any // Message is the human readable, user-friendly message for the issue. // This is safe to expose to the user. Message string // Err is the wrapped error or nil if none Err error }
ZogIssue represents an issue that occurred during parsing or validation. When printed it looks like: ZogIssue{Code: coercion_issue, Params: map[], Type: number, Value: not_empty, Message: number is invalid, Error: failed to coerce string int: strconv.Atoi: parsing "not_empty": invalid syntax}
func NewZogIssue ¶ added in v0.19.0
func NewZogIssue() *ZogIssue
func (*ZogIssue) Error ¶ added in v0.16.0
Error returns the string representation of the ZogIssue (same as String())
func (*ZogIssue) SetCode ¶ added in v0.16.0
func (i *ZogIssue) SetCode(c zconst.ZogIssueCode) *ZogIssue
SetCode sets the issue code for the issue and returns the issue for chaining
func (*ZogIssue) SetDType ¶ added in v0.16.0
SetDType sets the destination type for the issue and returns the issue for chaining
func (*ZogIssue) SetError ¶ added in v0.16.0
SetError sets the wrapped error for the issue and returns the issue for chaining
func (*ZogIssue) SetMessage ¶ added in v0.16.0
SetMessage sets the human readable, user-friendly message for the issue and returns the issue for chaining
func (*ZogIssue) SetParams ¶ added in v0.16.0
SetParams sets the params map for the issue and returns the issue for chaining
func (*ZogIssue) SetPath ¶ added in v0.16.0
SetPath sets the path for the issue and returns the issue for chaining
func (*ZogIssue) SetValue ¶ added in v0.16.0
SetValue sets the data value that caused the issue and returns the issue for chaining
type ZogIssueList ¶ added in v0.16.0
type ZogIssueList = []*ZogIssue
list of errors. This is returned by processors for simple types (e.g. strings, numbers, booleans)
type ZogIssueMap ¶ added in v0.16.0
type ZogIssueMap = map[string]ZogIssueList
map of errors. This is returned by processors for complex types (e.g. maps, slices, structs)
type ZogIssues ¶ added in v0.16.0
INTERNAL ONLY: Interface used to add errors during parsing & validation. It represents a group of errors (map or slice)
type ZogPrimitive ¶
Primitive types that can be used in Zod schemas