-
-
Notifications
You must be signed in to change notification settings - Fork 215
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
feature request; parse
that only accepts compatible inputs
#1024
Comments
Thank you for creating this issue! What is your use case? Why are you validating when you already know the data? I am asking to better understand the use case and make a better decision. How would you name such a function? |
Thank you for the reply! (I've revised this comment several times and am having a hard time figuring out whether or not it's coming across clearly, so apologies in advance if not ...) But roughly, I have a codebase with plenty of types, but no schemas. In new code, I'll define schemas and then use This normally means that:
type Result =
| { status: 'SUCCESS'; data: SomeOtherType}
| { status: 'ERROR'; issues: string[]};
const MySchema = v.object({
id: v.number,
name: v.string,
// etc ...
});
type MySchema = v.InferOutput<MySchema>;
function processData(input: Result): MySchema {
if (input.status === 'SUCCESS') {
return v.parse(MySchema, input); // will always throw!
}
// ... other code ...
} The thing is—if I didn't use valibot at all, TypeScript will happily surface the error: function processData(input: Result): {id: number; name: string} {
if (input.status === 'SUCCESS') {
return input; // type checking error
}
// ... other code ...
} Another way of framing this is that, with TypeScript alone as the boundary between other code and my own, issues like this will create static type errors like usual. When adding valibot, the Does this help clarify? |
Not quite sure I can follow you. Does this work for you? type Result<T> =
| { status: 'SUCCESS'; data: T }
| { status: 'ERROR'; issues: string[] };
type Product = {
id: number;
name: string;
// ...
};
function processProduct(input: Result<Product>): Product {
if (input.status === 'SUCCESS') {
return input.data;
}
throw new Error('...');
} |
tldr—I'd like to see a function similar to
parse
, but that only accepts inputs known to be compatible with the schema.Since
parse
(andsafeParse
, etc.) are all annotated withinput: unknown
, it is possible to pass an object that will never match the schema. The resulting code is statically correct, but will always fail to parse at runtime.Simple example
playground link
I can produce a more realistic example if this isn't compelling, but my request here is essentially for the library to guard against this.
I know that a workaround is to annotate the input objects as
v.InferInput<Schema>
. But, me knowing myself, I don't think "trust that future-me will remember to do this every time" is a sustainable approach 🙂Thank you for the great library; valibot is really a delight to use and I have already noticed it making my code clearer and safer.
The text was updated successfully, but these errors were encountered: