-
Notifications
You must be signed in to change notification settings - Fork 34
feat: support for custom strings, numbers and booleans #131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
19c931f
checkpoint
Oudwins f1543b7
fix: missing generics
Oudwins d081014
checkpoint
Oudwins 90be1b0
feat: exported coercer func type directly from zog package
Oudwins dd34192
feat: added eq method to bool schema
Oudwins 0bd6b10
docs: added bool schema eq method
Oudwins df1125e
test: tests for custom types
Oudwins 94f9e37
refactor: removed custom interface schema stuff
Oudwins 5857eb0
docs: custom schemas
Oudwins 4d140f4
docs: fixed md
Oudwins 3c0ea03
refactor: removed custom experiments
Oudwins File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| --- | ||
| sidebar_position: 8 | ||
| sidebar_position: 20 | ||
| --- | ||
|
|
||
| # Configuration | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| --- | ||
| sidebar_position: 5 | ||
| toc_min_heading_level: 2 | ||
| toc_max_heading_level: 4 | ||
| --- | ||
|
|
||
| # Creating Custom Schemas | ||
|
|
||
| > Please read the [Anatomy of a Schema](/core-concepts/anatomy-of-schema) page before continuing. | ||
|
|
||
| Currently Zog plans to support three different ways of creating custom schemas. Although this is subject to change and some of these are not yet implemented so keep an eye out on this page as it gets updated, [more details on my thoughts here](https://github.com/Oudwins/zog/discussions/132). | ||
|
|
||
| 1. Generics on Primitive Schemas for custom strings, numbers, booleans, etc... | ||
| 2. Custom Schemas Interface you can implement to create a 100% custom schema (Not yet implemented) | ||
| 3. A system by which you can define a schema for a custom type or interface that after some transformation can become a normal zog schema. | ||
|
|
||
| ## Creating Custom Schemas for Primitive Types | ||
|
|
||
| This is quite simple to do for the supported primitive types (string, number, boolean). Here is an example: | ||
|
|
||
| ```go | ||
| // definition in your code | ||
| type Env string | ||
| const ( | ||
| Prod Env = "prod". | ||
| Dev Env = "env" | ||
| ) | ||
| func EnvSchema() *StringSchema[Env] { | ||
| s := &z.StringSchema[Env] | ||
| return s.OneOf([]Env{Prod, Dev}) // you can also just return the schema and define the tests when calling it it doesn't matter | ||
| } | ||
| // usage | ||
| type S struct { | ||
| Environment Env | ||
| } | ||
| schema := z.Struct( | ||
| z.Schema{ | ||
| "Environment": EnvSchema() // All string methods will now be typed to Env type | ||
| } | ||
| ) | ||
| ``` | ||
|
|
||
| This becomes a little more complex if you need to use `Parse` instead of just `Validate` since you need to define a custom `Coercer` function. Here is what I would recommend and it is also very similar to the way Zog creates the schemas you use: | ||
|
|
||
| ```go | ||
| // Definition | ||
| func EnvSchema(opts ...z.SchemaOption) *StringSchema[Env] { | ||
| s := &StringSchema[Env]{} | ||
| ops = append([]z.SchemaOption{ | ||
| // This is required if you want to use Parse since we don't use reflection to set the value you need to coerce it manually | ||
| WithCoercer(func(x any) (any, error) { | ||
| v, e := conf.DefaultCoercers.String(x) | ||
| if e != nil { | ||
| return nil, e | ||
| } | ||
| return Env(v.(string)), nil | ||
| }), | ||
| ...opts, | ||
| }) | ||
| for _, op := range ops { | ||
| op(s) | ||
| } | ||
| return s | ||
| } | ||
| // Usage is the same as before | ||
| ``` | ||
|
|
||
| > Why is this so verbose? | ||
| > Although we considered introducing an API that would allow you to define this types of schemas in a more concise way (and we may still do so), to keep code consistency & reusability we recommend that you make a factory function like the one above for your custom types. And we felt that providing a simpler API could lead to people just inlining the schema's which would make it impossible to reuse them. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| --- | ||
| sidebar_position: 5 | ||
| sidebar_position: 6 | ||
| toc_min_heading_level: 2 | ||
| toc_max_heading_level: 4 | ||
| --- | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| --- | ||
| sidebar_position: 9 | ||
| sidebar_position: 21 | ||
| --- | ||
|
|
||
| # Performance | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Code Example (Primitive Types): Go Syntax and Consistency
"prod".). It is recommended to remove the period.s := &z.StringSchema[Env]may be more idiomatically written ass := &z.StringSchema[Env]{}to clearly initialize a new instance.📝 Committable suggestion