-
-
Notifications
You must be signed in to change notification settings - Fork 247
How can I allow null as input value of a schema, but not as a valid output? #1088
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
Comments
this is my workaround as for now const schema = object({
duration_minutes: number(),
});
type Nullable<T> = { [K in keyof T]: T[K] | null }; // https://typeofnan.dev/making-every-object-property-nullable-in-typescript/
type InputSchema = Nullable<InferInput<typeof schema>>;
type OutputSchema = InferOutput<typeof schema>; |
I would simply write: import * as v from 'valibot';
const Schema = v.pipe(v.nullable(v.number()), v.number());
type Input = v.InferInput<typeof Schema>; // number | null
type Output = v.InferOutput<typeof Schema>; // number The first schema of the pipeline defines the input type and the last one the output type. |
I'm currently looking for the same thing and experimenting with Valibot. I like the The goal is to allow const person = v.object({
name: v.string(),
address: v.pipe(
v.nullable(
v.object({
street: v.string(),
country: v.string(),
}),
),
v.object({
street: v.string(),
country: v.string(),
}),
),
bestFriend: v.pipe(
v.nullable(
v.object({
name: v.string(),
age: v.number(),
}),
),
v.object({
name: v.string(),
age: v.number(),
}),
),
}); I could do const address = v.object({
street: v.string(),
country: v.string(),
});
const bestFriend = v.object({
name: v.string(),
age: v.number(),
});
const person = v.object({
name: v.string(),
address: v.pipe(v.nullable(address), address),
bestFriend: v.pipe(v.nullable(bestFriend), bestFriend),
}); But now, if I want const bestFriend = v.object({
name: v.string(),
age: v.pipe(v.nullable(v.number()), v.number()),
});
const person = v.object({
bestFriend: v.pipe(v.nullable(bestFriend), bestFriend),
}); This means I have to repeat the whole object: const person = v.object({
bestFriend: v.pipe(
v.nullable(
v.object({
name: v.string(),
age: v.nullable(v.number()),
}),
),
v.object({
name: v.string(),
age: v.number(),
}),
),
}); Again, this is not a problem with simple and small objects. However, it can become a problem with more complex objects. What do you think? |
This is fixable. I will share some code later. |
You have two options. The first is to use import * as v from 'valibot';
const Schema = v.nullable(v.object({ key: v.string() }), { key: '' });
type Input = v.InferInput<typeof Schema>; // { key: string } | null
type Output = v.InferOutput<typeof Schema>; // { key: string } The second option is to keep your current code, but write a custom function to avoid repeating yourself. See this playground. import * as v from 'valibot';
function nullableInput<TSchema extends v.GenericSchema>(schema: TSchema) {
return v.pipe(v.nullable(schema), schema);
}
const Schema = nullableInput(v.object({ key: v.string() }));
type Input = v.InferInput<typeof Schema>; // { key: string } | null
type Output = v.InferOutput<typeof Schema>; // { key: string } |
Thank you very much for your answer! While the first approach doesn't work for form validation because I want to get an error if the input is |
This TS error is strange. If more people encounter this problem, I will investigate it. |
Hello! |
@lukasvice I know the reason for the TS error. The first schema will never output |
@elmehdielhamdi is the only difference the |
@fabian-hiller I see, that makes sense. Still, it's strange that it works with the |
I know why but we will probably keep this implementation for now. The implementation is safe and does not cause any runtime errors and in most cases this TS error is helpful to write more efficient schemas. |
example
related issue from zod repo: colinhacks/zod#1206
My common use case is when dealing with form libraries like react-hook-form. I want components value like DatePicker or FilePicker to be null initially, but they should not pass the schema.
The text was updated successfully, but these errors were encountered: