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

Update to Custom Schemas example in Documentation #2718

Open
mitchgollub opened this issue Sep 7, 2023 · 2 comments
Open

Update to Custom Schemas example in Documentation #2718

mitchgollub opened this issue Sep 7, 2023 · 2 comments
Labels
documentation Improvements or additions to documentation enhancement New feature or request

Comments

@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;
});

Please let me know if this is a welcome change. I can make a fork of the repo and create a PR!

@JacobWeisenburger
Copy link
Collaborator

Sounds good to me. Please make the PR and I'll take a look.

@mitchgollub
Copy link
Contributor Author

Thank you for the opportunity to contribute @JacobWeisenburger! I've made my PR here: #2778

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants