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

#2718 Updated Custom Schemas documentation example to use type narrowing #2778

Conversation

mitchgollub
Copy link
Contributor

Hi,

I'm interested in updating the Custom Schemas example in the documentation. The function type takes in an unknown, so the example casts the value to a string in order to operate on it with a RegExp

const px = z.custom<`${number}px`>((val) => {
  return /^\d+px$/.test(val as string);
});

I want to propose using Type Narrowing. It's helped me catch errors from validating a non-expected type, and I believe it would help others as well as a good practice with interacting with the unknown type.

const px = z.custom<`${number}px`>((val) => {
  return typeof val === "string" ? /^\d+px$/.test(val) : false;
});

This is a PR for issue #2718

@netlify
Copy link

netlify bot commented Sep 23, 2023

Deploy Preview for guileless-rolypoly-866f8a ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 5dc2b43
🔍 Latest deploy log https://app.netlify.com/sites/guileless-rolypoly-866f8a/deploys/650f333affe9460008b9ce94
😎 Deploy Preview https://deploy-preview-2778--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.

@JacobWeisenburger JacobWeisenburger merged commit ad2ee9c into colinhacks:master Sep 23, 2023
12 checks passed
@JacobWeisenburger
Copy link
Collaborator

Thanks @mitchgollub

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants