Documentation
¶
Index ¶
- Variables
- func Clear()
- func ClearPools()
- func IsParseZeroValue(val any, ctx Ctx) bool
- func IsZeroValue(x any) bool
- func SafeError(x error) string
- func SafeString(x any) string
- 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) 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 Test
- func EQ[T comparable](n T) Test
- func GT[T constraints.Ordered](n T) Test
- func GTE[T constraints.Ordered](n T) Test
- func In[T any](values []T) Test
- func LT[T constraints.Ordered](n T) Test
- func LTE[T constraints.Ordered](n T) Test
- func Len[T LengthCapable[any]](n int) Test
- func LenMax[T LengthCapable[any]](n int) Test
- func LenMin[T LengthCapable[any]](n int) Test
- func Required() Test
- type TestCtx
- type TestFunc
- type ZogErr
- func (e *ZogErr) Code() zconst.ZogIssueCode
- func (e *ZogErr) Dtype() string
- func (e *ZogErr) Error() string
- func (e *ZogErr) Free()
- func (e *ZogErr) Message() string
- func (e *ZogErr) Params() map[string]any
- func (e *ZogErr) Path() string
- func (e *ZogErr) SDType(t zconst.ZogType) ZogError
- func (e *ZogErr) SParams(p map[string]any) ZogError
- func (e *ZogErr) SValue(v any) ZogError
- func (e *ZogErr) SetCode(c zconst.ZogIssueCode) ZogError
- func (e *ZogErr) SetDType(t zconst.ZogType) ZogError
- func (e *ZogErr) SetError(err error) ZogError
- func (e *ZogErr) SetMessage(msg string)
- func (e *ZogErr) SetParams(p map[string]any) ZogError
- func (e *ZogErr) SetPath(p string) ZogError
- func (e *ZogErr) SetValue(v any) ZogError
- func (e *ZogErr) String() string
- func (e *ZogErr) Unwrap() error
- func (e *ZogErr) Value() any
- type ZogError
- type ZogIssue
- 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 &ZogErr{} }, }
Functions ¶
func ClearPools ¶
func ClearPools()
func IsParseZeroValue ¶
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 SafeString ¶
Types ¶
type Ctx ¶
type Ctx interface {
// Get a value from the context
Get(key string) any
// Deprecated: Use Ctx.AddIssue() instead
// Please don't depend on this interface it may change
NewError(p *PathBuilder, e ZogIssue)
// Adds an issue to the schema execution.
AddIssue(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
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) DataProvider
func NewSafeMapDataProvider ¶
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) GetNestedProvider ¶
func (e *EmptyDataProvider) GetNestedProvider(key string) DataProvider
func (*EmptyDataProvider) GetUnderlying ¶
func (e *EmptyDataProvider) GetUnderlying() any
type ExecCtx ¶
type ExecCtx struct {
Fmter IssueFmtFunc
Errors ZogIssues
// contains filtered or unexported fields
}
func NewExecCtx ¶
func NewExecCtx(errs ZogIssues, fmter IssueFmtFunc) *ExecCtx
func (*ExecCtx) HasErrored ¶
func (*ExecCtx) NewError
deprecated
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 ¶
func (*ExecCtx) NewValidateSchemaCtx ¶
func (*ExecCtx) SetIssueFormatter ¶
func (c *ExecCtx) SetIssueFormatter(fmter IssueFmtFunc)
type IsZeroValueFunc ¶
type IssueFmtFunc ¶
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]) 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 ¶
func NewPathBuilder() *PathBuilder
func (*PathBuilder) Free ¶
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 ¶
type SchemaCtx struct {
*ExecCtx
Val any
DestPtr any
Path *PathBuilder
DType zconst.ZogType
CanCatch bool
HasCaught bool
}
func (*SchemaCtx) IssueFromCoerce ¶
Please don't depend on this method it may change
func (*SchemaCtx) IssueFromTest ¶
Please don't depend on this method it may change
func (*SchemaCtx) IssueFromUnknownError ¶
Please don't depend on this method it may change Wraps an error in a ZogIssue if it is not already a ZogIssue
type Test ¶
type Test struct {
IssueCode zconst.ZogIssueCode
IssuePath string
Params map[string]any
IssueFmtFunc IssueFmtFunc
ValidateFunc TestFunc
}
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 EQ ¶
func EQ[T comparable](n T) Test
func GT ¶
func GT[T constraints.Ordered](n T) Test
func GTE ¶
func GTE[T constraints.Ordered](n T) Test
func LT ¶
func LT[T constraints.Ordered](n T) Test
func LTE ¶
func LTE[T constraints.Ordered](n T) Test
type TestFunc ¶
TestFunc is a function that takes the data as input and returns a boolean indicating if it is valid or not
type ZogErr ¶
type ZogErr struct {
C zconst.ZogIssueCode // error code
EPath string // path of the value that caused the error
ParamsM map[string]any // params for the error (e.g. min, max, len, etc)
Typ string // destination type
Val any // value that caused the error
Msg string // human readable message
Err error // the underlying error
}
INTERNAL ONLY: Error implementation
func (*ZogErr) SetMessage ¶
type ZogIssue ¶
type ZogIssue interface {
// returns the error code for the error. This is a unique identifier for the Issue. Generally also the ID for the Test that caused the error.
Code() zconst.ZogIssueCode
// Sets the error code for the Issue. This is a unique identifier for the Issue. Generally also the ID for the Test that caused the error.
SetCode(zconst.ZogIssueCode) ZogIssue
// returns the path of the Issue. This is the path of the value that caused the Issue.
Path() string
// Sets the path of the Issue. This is the path of the value that caused the Issue.
SetPath(string) ZogIssue
// returns the data value that caused the Issue.
// if using Schema.Parse(data, dest) then this will be the value of data.
Value() any
// Deprecated: Use SetValue() instead
// Sets the data value that caused the Issue.
// if using Schema.Parse(data, dest) then this will be the value of data.
SValue(any) ZogIssue
// Sets the data value that caused the Issue.
// if using Schema.Parse(data, dest) then this will be the value of data.
SetValue(any) ZogIssue
// Returns 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
// Deprecated: Use SetDType() instead
// Sets 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.
SDType(zconst.ZogType) ZogIssue
// Sets 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.
SetDType(zconst.ZogType) ZogIssue
// returns the params map for the error. Taken from the Test that caused the error. This may be nil if Test has no params.
Params() map[string]any
// Deprecated: Use SetParams() instead
// Sets the params map for the error. Taken from the Test that caused the error. This may be nil if Test has no params.
SParams(map[string]any) ZogIssue
// Sets the params map for the error. Taken from the Test that caused the error. This may be nil if Test has no params.
SetParams(map[string]any) ZogIssue
// returns the human readable, user-friendly message for the error. This is safe to expose to the user.
Message() string
// sets the human readable, user-friendly message for the error. This is safe to expose to the user.
SetMessage(string)
// returns the string representation of the ZogIssue (same as String())
Error() string
// Sets the wrapped error.
SetError(error) ZogIssue
// returns the wrapped error or nil if none
Unwrap() error
// returns the string representation of the ZogIssue (same as Error())
String() string
}
type ZogIssueList ¶
type ZogIssueList = []ZogError
list of errors. This is returned by processors for simple types (e.g. strings, numbers, booleans)
type ZogIssueMap ¶
map of errors. This is returned by processors for complex types (e.g. maps, slices, structs)
type ZogIssues ¶
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