Reference
Generic Zog Schema Methods
These are methods that can generally be called on any schema type (Some exceptions might exist).
schema.Test(test) // create a custom test
schema.TestFunc(fn) // create a custom test from a function
schema.Required() // marks field as required
schema.Optional() // marks field as optional
schema.Default(value) // sets default value for field
schema.Catch(value) // sets catch value for field
schema.Transform(func(valPtr *T or any, ctx z.Ctx) (any, error)) // adds a transformation function to the schema. This is useful for things like trimming strings, etc.
// VALIDATION METHODS
schema.Parse(data, destPtr) // parses the data into the destination
schema.Validate(dataPtr) // validates the data structure directly. This is a pointer to a struct, slice, string, int, etc...
Options
Utility functions used to configure schemas, executions, tests, etc...
Test Options
These are options that can be passed to any test. For more on this checkout the Anatomy of a Schema page.
z.Message() // sets the issue message for messages generated by the tests
z.MessageFunc(fn) // sets the issue message for messages generated by the tests. This is a function that takes the data as input and returns a string
z.IssueCode() // sets the issue code for messages generated by the tests
z.IssuePath() // sets the issue path for messages generated by the tests
Schema Options
These are options that can be passed to schemas when creating them.
z.WithCoercer(fn) // sets the coercer for the schema. Only does anything if using schema.Parse()
Execution Options
These are options that can be passed to schema.Parse() & schema.Validate(). They configure the execution behaviour of the validation.
z.WithIssueFormatter(fn) // sets the issue formatter for the execution. This is used to format the issues messages during execution.
z.WithCtxValue(key, val) // sets a value in the execution context. This is useful for passing values to tests or post transforms.
Schema Types
// Primitives. Calling .Parse() on these will return []ZogIssue
z.String()
z.Int()
z.Int32()
z.Int64()
z.Float32()
z.Float64()
z.Bool()
z.Time()
// Custom Primitive Schemas
z.StringLike[T]()
z.IntLike[T]()
z.FloatLike[T]()
z.UintLike[T]()
z.BoolLike[T]()
// Complex Types. Calling .Parse() on these will return ZogIssueList (same as primitives). Each issue has a Path field ([]string) indicating where the issue occurred. Root-level issues have a nil Path.
z.Struct(z.Shape{
"name": z.String(),
})
z.Slice(z.String())
z.Ptr(z.String()) // pointer to string
z.Boxed[B, T](schema, unboxFunc, boxFunc) // boxed type wrapper
Utility Schemas
z.Preprocess()
// Usage:
z.Preprocess(func(data any, ctx z.ctx) (any, error) {
s := data.(string)
return strings.split(s, ","), nil
}, z.slice(z.string())))
Primitive Types
String
// Transforms
z.String().Trim() // trims the value of whitespace
// Tests / Validations
z.String().Test() // custom test
z.String().Min(5) // validates min length
z.String().Max(10) // validates max length
z.String().Len(5) // validates length
z.String().Email() // validates email
z.String().URL() // validates url
z.String().IPv4() // validates IPv4 address
z.String().UUID() // validates uuid v4
z.String().Match(regex) // matches a regex
z.String().Contains(substring) // validates string contains substring
z.String().ContainsUpper() // validates string contains uppercase letter
z.String().ContainsDigit() // validates string contains digit
z.String().ContainsSpecial() // validates string contains special character
z.String().HasPrefix(prefix) // validates string has prefix
z.String().HasSuffix(suffix) // validates string has suffix
z.String().OneOf([]string{"a", "b", "c"}) // validates string is one of the values. Similar to zod enums
// Utilities
z.String().Not() // Negates the next test/validation