Documentation ¶
Index ¶
- Constants
- func IsParseZeroValue(val any, ctx ParseCtx) bool
- func IsZeroValue(x any) bool
- func SafeError(x error) string
- func SafeString(x any) string
- func TryNewAnyDataProvider(val any) (DataProvider, ZogError)
- type DataProvider
- type DpFactory
- type EmptyDataProvider
- type ErrFmtFunc
- type ErrsList
- type ErrsMap
- type IsZeroValueFunc
- type LengthCapable
- type MapDataProvider
- type ParseCtx
- type PathBuilder
- type PostTransform
- type PreTransform
- 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 TestFunc
- type ZogErr
- func (e *ZogErr) Code() zconst.ZogErrCode
- func (e *ZogErr) Dtype() string
- func (e *ZogErr) Error() string
- func (e *ZogErr) Message() string
- func (e *ZogErr) Params() map[string]any
- 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) SetMessage(msg string)
- func (e *ZogErr) String() string
- func (e *ZogErr) Unwrap() error
- func (e *ZogErr) Value() any
- type ZogErrList
- type ZogErrMap
- type ZogError
- type ZogErrors
- type ZogParseCtx
- type ZogPrimitive
Constants ¶
const ( ERROR_KEY_FIRST = "$first" ERROR_KEY_ROOT = "$root" )
Variables ¶
This section is empty.
Functions ¶
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 SafeString ¶ added in v0.11.0
func TryNewAnyDataProvider ¶
func TryNewAnyDataProvider(val any) (DataProvider, ZogError)
Types ¶
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
type DpFactory ¶
type DpFactory = func() (DataProvider, ZogError)
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 ErrFmtFunc ¶
this is the function that formats the error message given a zog error
type ErrsList ¶
type ErrsList struct {
List ZogErrList
}
internal only
func (*ErrsList) Add ¶
func (e *ErrsList) Add(path PathBuilder, err ZogError)
type ErrsMap ¶
type ErrsMap struct {
M ZogErrMap
}
map implementation of Errs
func (*ErrsMap) Add ¶
func (s *ErrsMap) Add(p PathBuilder, err ZogError)
type IsZeroValueFunc ¶ added in v0.12.0
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 ParseCtx ¶
type ParseCtx interface { // Get a value from the context Get(key string) any // Please don't depend on this interface it may change NewError(p PathBuilder, e ZogError) // Please don't depend on this interface it may change HasErrored() bool }
type PathBuilder ¶
type PathBuilder string
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
type Test ¶
type Test struct { ErrCode zconst.ZogErrCode Params map[string]any ErrFmt ErrFmtFunc 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.ZogErrCode // error code 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 Err error // the underlying error }
INTERNAL ONLY: Error implementation
func (*ZogErr) SetMessage ¶
type ZogErrList ¶
type ZogErrList = []ZogError
list of errors. This is returned by processors for simple types (e.g. strings, numbers, booleans)
type ZogErrMap ¶
map of errors. This is returned by processors for complex types (e.g. maps, slices, structs)
type ZogError ¶
type ZogError interface { // returns the error code for the error. This is a unique identifier for the error. Generally also the ID for the Test that caused the error. Code() zconst.ZogErrCode // returns the data value that caused the error. // if using Schema.Parse(data, dest) then this will be the value of data. Value() any // Sets the data value that caused the error. // if using Schema.Parse(data, dest) then this will be the value of data. SValue(any) ZogError // 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 // 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) ZogError // 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 // 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) ZogError // 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 ZogError (same as String()) Error() string // returns the wrapped error or nil if none Unwrap() error // returns the string representation of the ZogError (same as Error()) String() string }
Error interface returned from all processors
type ZogErrors ¶
type ZogErrors interface { Add(path PathBuilder, err ZogError) IsEmpty() bool }
INTERNAL ONLY: Interface used to add errors during parsing & validation. It represents a group of errors (map or slice)
type ZogParseCtx ¶
type ZogParseCtx struct { Fmter ErrFmtFunc Errors ZogErrors // contains filtered or unexported fields }
func NewParseCtx ¶
func NewParseCtx(errs ZogErrors, fmter ErrFmtFunc) *ZogParseCtx
func (*ZogParseCtx) Get ¶
func (c *ZogParseCtx) Get(key string) any
func (*ZogParseCtx) HasErrored ¶
func (c *ZogParseCtx) HasErrored() bool
func (*ZogParseCtx) NewError ¶
func (c *ZogParseCtx) NewError(p PathBuilder, e ZogError)
func (*ZogParseCtx) Set ¶
func (c *ZogParseCtx) Set(key string, val any)
func (*ZogParseCtx) SetErrFormatter ¶
func (c *ZogParseCtx) SetErrFormatter(fmter ErrFmtFunc)