internals

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ExecCtxPool = sync.Pool{
	New: func() any {
		return &ExecCtx{}
	},
}
View Source
var InternalIssueListPool = sync.Pool{
	New: func() any {
		return &ErrsList{}
	},
}
View Source
var InternalIssueMapPool = sync.Pool{
	New: func() any {
		return &ErrsMap{}
	},
}
View Source
var PathBuilderPool = sync.Pool{
	New: func() any {
		pb := make(PathBuilder, 0, 5)
		return &pb
	},
}
View Source
var SchemaCtxPool = sync.Pool{
	New: func() any {
		return &SchemaCtx{}
	},
}
View Source
var StringBuilderPool = sync.Pool{
	New: func() any {
		sb := strings.Builder{}
		return &sb
	},
}
View Source
var ZogIssuePool = sync.Pool{
	New: func() any {
		return &ZogErr{}
	},
}

Functions

func Clear

func Clear()

func ClearPools

func ClearPools()

func IsParseZeroValue

func IsParseZeroValue(val any, ctx Ctx) 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

func SafeError(x error) string

func SafeString

func SafeString(x any) string

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 ErrsList

type ErrsList struct {
	List ZogIssueList
}

internal only

func NewErrsList

func NewErrsList() *ErrsList

internal only

func (*ErrsList) Add

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

func (*ErrsList) Free

func (e *ErrsList) Free()

func (*ErrsList) IsEmpty

func (e *ErrsList) IsEmpty() bool

type ErrsMap

type ErrsMap struct {
	M ZogIssueMap
}

map implementation of Errs

func NewErrsMap

func NewErrsMap() *ErrsMap

Factory for errsMap

func (*ErrsMap) Add

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

func (*ErrsMap) Free

func (s *ErrsMap) Free()

func (*ErrsMap) IsEmpty

func (s *ErrsMap) IsEmpty() bool

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

func (c *ExecCtx) AddIssue(e ZogIssue)

Adds a ZogIssue to the execution context.

func (*ExecCtx) FmtErr

func (c *ExecCtx) FmtErr(e ZogIssue)

Internal. Used to format errors

func (*ExecCtx) Free

func (c *ExecCtx) Free()

func (*ExecCtx) Get

func (c *ExecCtx) Get(key string) any

func (*ExecCtx) HasErrored

func (c *ExecCtx) HasErrored() bool

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 (c *ExecCtx) NewSchemaCtx(val any, destPtr any, path *PathBuilder, dtype zconst.ZogType) *SchemaCtx

func (*ExecCtx) NewValidateSchemaCtx

func (c *ExecCtx) NewValidateSchemaCtx(valPtr any, path *PathBuilder, dtype zconst.ZogType) *SchemaCtx

func (*ExecCtx) Set

func (c *ExecCtx) Set(key string, val any)

func (*ExecCtx) SetIssueFormatter

func (c *ExecCtx) SetIssueFormatter(fmter IssueFmtFunc)

type IsZeroValueFunc

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

type IssueFmtFunc

type IssueFmtFunc = func(e ZogError, p Ctx)

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

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

type for functions called after validation & parsing is done

type PreTransform

type PreTransform = func(data any, ctx Ctx) (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. 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) Free

func (c *SchemaCtx) Free()

Frees the context to be reused

func (*SchemaCtx) Issue

func (c *SchemaCtx) Issue() ZogIssue

func (*SchemaCtx) IssueFromCoerce

func (c *SchemaCtx) IssueFromCoerce(err error) ZogIssue

Please don't depend on this method it may change

func (*SchemaCtx) IssueFromTest

func (c *SchemaCtx) IssueFromTest(test *Test, val any) ZogIssue

Please don't depend on this method it may change

func (*SchemaCtx) IssueFromUnknownError

func (c *SchemaCtx) IssueFromUnknownError(err error) ZogIssue

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 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 TestCtx

type TestCtx struct {
	*SchemaCtx
	Test *Test
}

func (*TestCtx) AddIssue

func (c *TestCtx) AddIssue(e ZogIssue)

func (*TestCtx) FmtErr

func (c *TestCtx) FmtErr(e ZogIssue)

func (*TestCtx) Issue

func (c *TestCtx) Issue() ZogIssue

type TestFunc

type TestFunc = func(val any, ctx Ctx) 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.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) Code

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

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

func (e *ZogErr) Free()

func (*ZogErr) Message

func (e *ZogErr) Message() string

func (*ZogErr) Params

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

func (*ZogErr) Path

func (e *ZogErr) Path() string

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

func (e *ZogErr) SetCode(c zconst.ZogIssueCode) ZogError

func (*ZogErr) SetDType

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

func (*ZogErr) SetError

func (e *ZogErr) SetError(err error) ZogError

func (*ZogErr) SetMessage

func (e *ZogErr) SetMessage(msg string)

func (*ZogErr) SetParams

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

func (*ZogErr) SetPath

func (e *ZogErr) SetPath(p string) ZogError

func (*ZogErr) SetValue

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

func (*ZogErr) String

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 ZogError

type ZogError = ZogIssue

Error interface returned from all processors

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

type ZogIssueMap = map[string][]ZogError

map of errors. This is returned by processors for complex types (e.g. maps, slices, structs)

type ZogIssues

type ZogIssues interface {
	Add(path string, err ZogError)
	IsEmpty() bool
	Free()
}

INTERNAL ONLY: Interface used to add errors during parsing & validation. It represents a group of errors (map or slice)

type ZogPrimitive

type ZogPrimitive interface {
	~string | ~bool | time.Time | constraints.Ordered
}

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