Skip to main content

Zog Context

What is context?

Zog uses a z.Ctx interface to pass around information related to a specific schema.Parse() or schema.Validate() call. Currently use of the parse context is quite limited but it will be expanded upon in the future. The context interface currently looks like this:

type Ctx interface {
// 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
}

Uses of context

Create issues manually

In any Zog function that has access to the context you may add issues manually. This is useful for many reasons. But mostly for creating complex custom tests, check the custom tests section for more information.

Pass custom data to functions

Here is an example with a pretransform

nameSchema := z.String().Min(3).PreTransform(func(data any, ctx z.Ctx) (any, error) {
char := ctx.Get("split_by")
return strings.Split(data.(string), char), nil
})
nameSchema.Parse("Michael Jackson", &dest, z.WithCtxValue("split_by", " "))

Change the issue formatter for this execution

This might be useful for localization, or for changing the error messages for one specific execution.

nameSchema := z.String().Min(3)
nameSchema.Parse(data, &dest, z.WithIssueFormatter(MyCustomErrorMessageFormatter))