internals

package
v0.12.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 9, 2024 License: MIT Imports: 7 Imported by: 5

Documentation

Index

Constants

View Source
const (
	ERROR_KEY_FIRST = "$first"
	ERROR_KEY_ROOT  = "$root"
)

Variables

This section is empty.

Functions

func IsParseZeroValue added in v0.12.0

func IsParseZeroValue(val any, ctx ParseCtx) bool

checks if the value is the zero value but only for parsing purposes (i.e the parse function)

func IsZeroValue

func IsZeroValue(x any) bool

checks that the value is the zero value for its type

func SafeError added in v0.11.0

func SafeError(x error) string

func SafeString added in v0.11.0

func SafeString(x any) string

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

type ErrFmtFunc = func(e ZogError, p ParseCtx)

this is the function that formats the error message given a zog error

type ErrsList

type ErrsList struct {
	List ZogErrList
}

internal only

func NewErrsList

func NewErrsList() *ErrsList

internal only

func (*ErrsList) Add

func (e *ErrsList) Add(path PathBuilder, err ZogError)

func (*ErrsList) IsEmpty

func (e *ErrsList) IsEmpty() bool

type ErrsMap

type ErrsMap struct {
	M ZogErrMap
}

map implementation of Errs

func NewErrsMap

func NewErrsMap() *ErrsMap

Factory for errsMap

func (*ErrsMap) Add

func (s *ErrsMap) Add(p PathBuilder, err ZogError)

func (ErrsMap) IsEmpty

func (s ErrsMap) IsEmpty() bool

type IsZeroValueFunc added in v0.12.0

type IsZeroValueFunc = func(val any, ctx ParseCtx) bool

type LengthCapable

type LengthCapable[K any] interface {
	~[]any | ~[]K | string | map[any]any | ~chan any
}

type MapDataProvider

type MapDataProvider[T any] struct {
	M map[string]T
}

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 PostTransform = func(dataPtr any, ctx ParseCtx) error

type for functions called after validation & parsing is done

type PreTransform

type PreTransform = func(data any, ctx ParseCtx) (out any, err error)

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 In

func In[T any](values []T) Test

func LT

func LT[T constraints.Ordered](n T) Test

func LTE

func LTE[T constraints.Ordered](n T) Test

func Len

func Len[T LengthCapable[any]](n int) Test

func LenMax

func LenMax[T LengthCapable[any]](n int) Test

func LenMin

func LenMin[T LengthCapable[any]](n int) Test

func Required

func Required() Test

returns a required test to be used for processor.Required() method

type TestFunc

type TestFunc = func(val any, ctx ParseCtx) bool

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) Code

func (e *ZogErr) Code() zconst.ZogErrCode

error code, err uuid

func (*ZogErr) Dtype

func (e *ZogErr) Dtype() string

destination type TODO

func (*ZogErr) Error

func (e *ZogErr) Error() string

func (*ZogErr) Message

func (e *ZogErr) Message() string

func (*ZogErr) Params

func (e *ZogErr) Params() map[string]any

func (*ZogErr) SDType

func (e *ZogErr) SDType(t zconst.ZogType) ZogError

func (*ZogErr) SParams

func (e *ZogErr) SParams(p map[string]any) ZogError

func (*ZogErr) SValue

func (e *ZogErr) SValue(v any) ZogError

func (*ZogErr) SetMessage

func (e *ZogErr) SetMessage(msg string)

func (*ZogErr) String added in v0.11.0

func (e *ZogErr) String() string

func (*ZogErr) Unwrap

func (e *ZogErr) Unwrap() error

func (*ZogErr) Value

func (e *ZogErr) Value() any

value that caused the error

type ZogErrList

type ZogErrList = []ZogError

list of errors. This is returned by processors for simple types (e.g. strings, numbers, booleans)

type ZogErrMap

type ZogErrMap = map[string][]ZogError

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)

type ZogPrimitive

type ZogPrimitive interface {
	~string | ~int | ~float64 | ~bool | time.Time
}

Primitive types that can be used in Zod schemas

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL