Skip to content

Commit

Permalink
docs: documentation for schema coercers
Browse files Browse the repository at this point in the history
  • Loading branch information
Oudwins committed Nov 9, 2024
1 parent 1d65859 commit 2f8ece5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
17 changes: 14 additions & 3 deletions docs/docs/core-concepts/2-parsing.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,22 @@ z.Struct(z.Schema{"name": z.String().Min(3)}).Parse(map[string]any{"name": "test

Under the hood Zog follows the [Parsing Execution Structure](#parsing-execution-structure) and does a bunch of things under the hood to make sure your data is parsed correctly. Such as checking for zero values, coercing types, etc...

Some of this might not be obvious so here is a table that breaksdown expected results of calling schema.Parse() on various inputs:
## Coercion

import ParsingResultsTable from "./parsing-table.tsx";
Zog will attempt to coerce the data into the correct type. For example if you have a `z.Int()` schema and you pass in a `"1"` it will be coerced into an `int` type. This behaviour, like almost everything else in Zog, can be customized. Zog provides two main ways to customize coercion:

<ParsingResultsTable />
- [Global Coercion](/configuration#coercion) - Change the default coercion behaviour for all schemas. More details on this in the [configuration page](/configuration#coercion).
- [Per schema coercers](#per-schema-coercers) - Define custom coercion functions for your schemas.

#### Per schema coercers

You can define custom coercion functions for your schemas by using the `z.WithCoercer()` schema option.

```go
z.String(z.WithCoercer(func(data any) (any, error) {
return "test", nil // now the result will be "test" no matter the input
})).Parse("abc", &dest) // dest will be "test"
```

## Parsing Context

Expand Down
13 changes: 13 additions & 0 deletions docs/docs/core-concepts/3-parsing-results.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
sidebar_position: 2
---

# Parsing Results Examples

Under the hood Zog follows the [Parsing Execution Structure](/core-concepts/parsing/#parsing-execution-structure) and does a bunch of things under the hood to make sure your data is parsed correctly. Such as checking for zero values, coercing types, etc...

Some of this might not be obvious so here is a summary table that breaksdown expected results of calling schema.Parse() on various inputs:

import ParsingResultsTable from "./parsing-table.tsx";

<ParsingResultsTable />
3 changes: 3 additions & 0 deletions docs/docs/schema-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ Use Time to validate `time.Time` instances
z.Time().After(time.Now()) // validates time is after now
z.Time().Before(time.Now()) // validates time is before now
z.Time().Is(time.Now()) // validates time is equal to now

// Schema Options
z.Time(z.Time.Format(time.RFC3339)) // If input is a string, it will be parsed as a time.Time using the provided layout. time.RFC3339 is the default
```

## Complex Types
Expand Down

0 comments on commit 2f8ece5

Please sign in to comment.