Conversation
Member
Author
|
@phpnode I have a minimal implementation of the parsing strategy that handles basic schemas. Let me know what you think 😄 |
razor-x
commented
Mar 4, 2025
Comment on lines
10
to
37
| ### Allowed Zod Schemas | ||
|
|
||
| - The top-level schema must be an `z.object()` or `z.union()` of `z.object()`. | ||
| - Properties may be a `z.object()` or `z.union()` of objects. | ||
| - All union object types must flatten to a parseable object schema with non-conflicting property types. | ||
| - Primitive properties must be a `z.string()`, `z.number()`, `z.boolean()` or `z.date()`. | ||
| - Properties must be a single-value type | ||
| - The primitives `z.bigint()` and `z.symbol()` are not supported. | ||
| - Strings with zero length are not allowed. | ||
| If not specified, a `z.string()` is always assumed to be `z.string().min(1)`. | ||
| - Using `z.enum()` is allowed and equivalent to `z.string()`. | ||
| - Any property may be `z.optional()` or `z.never()`. | ||
| - No property may `z.void()`, `z.undefined()`, `z.any()`, or `z.unknown()`. | ||
| - Any property may be `z.nullable()` except `z.array()`. | ||
| - Properties that are `z.literal()` are allowed and must still obey all of these rules. | ||
| - A `z.array()` must be of a single value-type. | ||
| - The value-types must obey all the same basic rules | ||
| for primitive object, union, and property types. | ||
| - Value-types may not be `z.nullable()` or `z.undefined()`. | ||
| - The value-type cannot be an `z.array()` or contain a nested `z.array()` at any level. | ||
| - A `z.record()` has less-strict schema constraints but weaker parsing guarantees: | ||
| - They keys must be `z.string()`. | ||
| - The value-type may be a single primitive type. | ||
| - The value-type may be a union of primitives. | ||
| This union must include `z.string()` | ||
| and all values will be parsed as `z.string()`. | ||
| - The value-type may be `z.nullable()`. | ||
| - The value-type may not be a `z.record()`, `z.array()`, or `z.object()`. |
Member
Author
There was a problem hiding this comment.
@phpnode Let me know if these rules make sense 🙏🏻
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Note: consider adding a strict mode parameter that both rejects non-invertable schemas (for example record types with union value types) as well ignoring as values that would not be generated by the serializer for example the bracket array format.