colinhacks / zod

TypeScript-first schema validation with static type inference
https://zod.dev
MIT License
33.09k stars 1.15k forks source link

[Zod 4] RFC: `ZodIssue` formats #3713

Open colinhacks opened 1 month ago

colinhacks commented 1 month ago

Old formats: https://github.com/colinhacks/zod/blob/v4-rfc-issues/rfcs/v4-issue-formats/old_formats.ts

export type ZodIssue =
  | ZodInvalidTypeIssue
  | ZodInvalidLiteralIssue
  | ZodUnrecognizedKeysIssue
  | ZodInvalidUnionIssue
  | ZodInvalidUnionDiscriminatorIssue
  | ZodInvalidEnumValueIssue
  | ZodInvalidArgumentsIssue
  | ZodInvalidReturnTypeIssue
  | ZodInvalidDateIssue
  | ZodInvalidStringIssue
  | ZodTooSmallIssue
  | ZodTooBigIssue
  | ZodInvalidIntersectionTypesIssue
  | ZodNotMultipleOfIssue
  | ZodNotFiniteIssue
  | ZodCustomIssue;

New formats: https://github.com/colinhacks/zod/blob/v4-rfc-issues/rfcs/v4-issue-formats/new_formats.ts

export type ZodIssue =
  | ZodInvalidTypeIssue
  | ZodInvalidStringIssue
  | ZodInvalidNumberIssue
  | ZodInvalidBigIntIssue
  | ZodInvalidDateIssue
  | ZodInvalidArrayIssue
  | ZodInvalidSetIssue
  | ZodInvalidObjectIssue
  | ZodInvalidFileIssue
  | ZodCustomIssue;

Conceptually speaking these are the major changes:

  1. ZodIssue is a 2-level discriminated union. The first level discriminates on code: invalid_type, invalid_stringm etc. For each code, there are sub-issues that are discriminated with the check field.
  2. Merges invalid_union, invalid_literal, invalid_enum_value into invalid_type
  3. Splits up too_big and too_small across the specific types for which they are defined: invalid_string, invalid_array, etc.
  4. Within each of the type-specific error code (e.g. invalid_string) there is an additional layer of discrimination using the check key. Instead of a top-level ZodNotMultipleOfIssue, this is now a check: "multiple_of" within code: "invalid_string".
  5. Drops invalid_arguments, invalid_return_type, invalid_discriminator_value as these issues are specific to types that will be removed in Zod 4.
netlify[bot] commented 1 month ago

Deploy Preview for guileless-rolypoly-866f8a ready!

Name Link
Latest commit 8bf2cc1f0e414b1aeef33aadcf83f949e19f161a
Latest deploy log https://app.netlify.com/sites/guileless-rolypoly-866f8a/deploys/66bfcffdecd444000843d905
Deploy Preview https://deploy-preview-3713--guileless-rolypoly-866f8a.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.