Skip to content
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

Actions: Allow effect chaining on form input validators #11809

Merged
merged 6 commits into from
Aug 22, 2024

Conversation

bholmesdev
Copy link
Contributor

@bholmesdev bholmesdev commented Aug 21, 2024

Changes

Allow chaining utilities on z.object() when validating forms. This includes .refine(), .transform(), and .passthrough().

Testing

  • Add unit test for passthrough
  • Add integration tests for refine and transform

Docs

N/A

Copy link

changeset-bot bot commented Aug 21, 2024

🦋 Changeset detected

Latest commit: 6505eab

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added pkg: astro Related to the core `astro` package (scope) pr: docs A PR that includes documentation for review labels Aug 21, 2024
@gtrabanco
Copy link

gtrabanco commented Aug 21, 2024

Sorry but I think that test case for .pipe is missed in the test? I think it should work if transform works.

export const passwordSchema = z
  .string()
  .min(8, 'Password should be at least 8 chars length')
  .max(128, 'Password length exceeded. Max 128 chars.');

const inputChangePasswordSchema = z
  .object({
    current_password: passwordSchema,
    new_password: passwordSchema,
    new_password_repeat: passwordSchema,
  })
  .required()
  .refine(
    ({ new_password, new_password_repeat }) => new_password === new_password_repeat,
    'The new password confirmation does not match'
  )
  .refine(
    ({ current_password, new_password }) => current_password !== new_password,
    'The old password and the new password must not match'
  )
  .transform((input) => ({
    currentPassword: input.current_password,
    newPassword: input.new_password,
  }))
  .pipe(
    z.object({
      currentPassword: passwordSchema,
      newPassword: passwordSchema,
    })
  )

@bholmesdev
Copy link
Contributor Author

Thanks for catching me @gtrabanco! Turns out pipe() isn't a ZodEffect. TIL. Added a test case and fixed the code.

@bholmesdev bholmesdev merged commit 62e97a2 into main Aug 22, 2024
14 checks passed
@bholmesdev bholmesdev deleted the feat/action-input-effects branch August 22, 2024 09:51
@astrobot-houston astrobot-houston mentioned this pull request Aug 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope) pr: docs A PR that includes documentation for review
Projects
None yet
3 participants