internals

package
v0.19.2 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2025 License: MIT Imports: 7 Imported by: 5

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 &ZogIssue{}
	},
}

Functions

func Clear added in v0.16.1

func Clear()

func ClearPools added in v0.16.1

func ClearPools()

func EQ

func EQ[T comparable](n T) (Test, BoolTFunc)

func FreeIssue added in v0.18.0

func FreeIssue(i *ZogIssue)

func FreeStringBuilder added in v0.16.4

func FreeStringBuilder(sb *strings.Builder)

func GT

func GT[T constraints.Ordered](n T) (Test, BoolTFunc)

func GTE

func GTE[T constraints.Ordered](n T) (Test, BoolTFunc)

func GetKeyFromField added in v0.18.1

func GetKeyFromField(field reflect.StructField, fallback string, tag *string) string

func In

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

func IsParseZeroValue added in v0.12.0

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 LT

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

func LTE

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

func Len

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

func LenMax

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

func LenMin

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

func NewStringBuilder added in v0.16.4

func NewStringBuilder() *strings.Builder

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 TestFuncFromBool added in v0.19.0

func TestFuncFromBool(fn BoolTFunc, test *Test)

func TestNotFuncFromBool added in v0.19.2

func TestNotFuncFromBool(fn BoolTFunc, test *Test)

Types

type BoolTFunc added in v0.19.0

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

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 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 *ZogIssue)

func (*ErrsList) Free added in v0.16.1

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 *ZogIssue)

func (*ErrsMap) Free added in v0.16.1

func (s *ErrsMap) Free()

func (*ErrsMap) IsEmpty

func (s *ErrsMap) IsEmpty() bool

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) AddIssue added in v0.16.0

func (c *ExecCtx) AddIssue(e *ZogIssue)

Adds a ZogIssue to the execution context.

func (*ExecCtx) FmtErr added in v0.16.0

func (c *ExecCtx) FmtErr(e *ZogIssue)

Internal. Used to format errors

func (*ExecCtx) Free added in v0.16.1

func (c *ExecCtx) Free()

func (*ExecCtx) Get added in v0.16.0

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

func (*ExecCtx) HasErrored added in v0.16.0

func (c *ExecCtx) HasErrored() bool

func (*ExecCtx) Issue added in v0.19.0

func (c *ExecCtx) Issue() *ZogIssue

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

func (*ExecCtx) NewValidateSchemaCtx added in v0.16.0

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

func (*ExecCtx) Set added in v0.16.0

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

func (*ExecCtx) SetIssueFormatter added in v0.16.0

func (c *ExecCtx) SetIssueFormatter(fmter IssueFmtFunc)

type IsZeroValueFunc added in v0.12.0

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

type IssueFmtFunc added in v0.16.0

type IssueFmtFunc = func(e *ZogIssue, 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
	// contains filtered or unexported fields
}

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 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 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) AddIssue added in v0.18.4

func (c *SchemaCtx) AddIssue(e *ZogIssue)

func (*SchemaCtx) Free added in v0.16.1

func (c *SchemaCtx) Free()

Frees the context to be reused

func (*SchemaCtx) Issue added in v0.16.0

func (c *SchemaCtx) Issue() *ZogIssue

func (*SchemaCtx) IssueFromCoerce added in v0.16.0

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

Please don't depend on this method it may change

func (*SchemaCtx) IssueFromTest added in v0.16.0

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

Please don't depend on this method it may change

func (*SchemaCtx) IssueFromUnknownError added in v0.16.0

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 TFunc added in v0.19.0

type TFunc = func(val any, ctx Ctx)

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

func Required

func Required() Test

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

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

func (i *ZogIssue) Error() string

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

func (i *ZogIssue) SetDType(t string) *ZogIssue

SetDType sets the destination type for the issue and returns the issue for chaining

func (*ZogIssue) SetError added in v0.16.0

func (i *ZogIssue) SetError(e error) *ZogIssue

SetError sets the wrapped error for the issue and returns the issue for chaining

func (*ZogIssue) SetMessage added in v0.16.0

func (i *ZogIssue) SetMessage(m string) *ZogIssue

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

func (i *ZogIssue) SetParams(p map[string]any) *ZogIssue

SetParams sets the params map for the issue and returns the issue for chaining

func (*ZogIssue) SetPath added in v0.16.0

func (i *ZogIssue) SetPath(p string) *ZogIssue

SetPath sets the path for the issue and returns the issue for chaining

func (*ZogIssue) SetValue added in v0.16.0

func (i *ZogIssue) SetValue(v any) *ZogIssue

SetValue sets the data value that caused the issue and returns the issue for chaining

func (*ZogIssue) String added in v0.16.0

func (i *ZogIssue) String() string

String returns the string representation of the ZogIssue (same as Error())

func (*ZogIssue) Unwrap added in v0.16.0

func (i *ZogIssue) Unwrap() error

Unwrap returns the wrapped error or nil if none

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

type ZogIssues interface {
	Add(path string, err *ZogIssue)
	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