-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
String .endsWith
& .startsWith
could perhaps be typed more strongly
#1747
Comments
I've opened a PR for that. Let's see what people think. |
I have some reservations discussed here: #1730 (comment) |
I really like the idea of better static types for @alii or @santosmarco-caribou, do you have some ideas on how to address backwards compatibility? |
One way would be to add an overload to these methods (almost like I did on my PR for We could leave the current implementation at the top of the overloads so the users will not feel any difference. This type of "exact" inference would be turned off by default. Then, as a second overload, we add a definition that forces the user to pass Something like this: startsWith(prefix: string, options?: { message?: string; exact?: never }): ZodString
startsWith<Start extends string>(prefix: Start, options: { message?: string; exact: true }): ZodString<Start, End>
startsWith(prefix: string, options?: { message?: string; exact?: true }): ZodString {
// no change in implementation, the `exact` is solely for turning the inference on.
} |
I like it. Any objections/reservations @colinhacks? |
I think this is a breaking change because the second argument is startsWith(prefix: string, options?: string | { message?: string; exact?: never }): ZodString
startsWith<Start extends string>(prefix: Start, options: { message?: string; exact: true }): ZodString<Start, End>
startsWith(prefix: string, options?: string | { message?: string; exact?: true }): ZodString {
// no change in implementation, the `exact` is solely for turning the inference on.
} IMO, I prefer creating a separate method for template literals because it looks cleaner when chaining methods: const schemaUrl = z
.string()
.startsWith("https://", {message: "Protocol must be https!", exact: true})
.endsWith("__schema.json", {exact: true})
.max(255);
const schemaUrl = z
.string()
.startsWithLiteral("https://", "Protocol must be https!")
.endsWithLiteral("__schema.json")
.max(255); |
Edit: Oops forgive me, the proposal below was already mentioned by @tachibanayui in the PR 😆 I just had a thought, perhaps the existence of For example, ZodString could be the default for all |
Yeah, I like the idea of a You pass an array of components (mainly But that's a bit complex to handle. But anyway, just wanted to share this idea too. Back to our I don't like the idea of a |
If the passed value was a literal, we could have a generic in ZodString that will include a prefix / suffix.
Use case:
At the moment, I am using the code below to validate that a value is a specific ID format that has a constant prefix
Imagine if this could be done with just
.startsWith
and.endsWith
— ZodString could then have a single generic of the output, which would default to just a string and can be made more specific with these two methods.Should note that I am not sure if this is a good idea or not, but I wanted to just throw the idea out either way — it's a use case I have very frequently and always use
.refine
to solve.The text was updated successfully, but these errors were encountered: