-
-
Notifications
You must be signed in to change notification settings - Fork 212
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
omit doesn't work with objectWithRest #678
Comments
From my point of view, this is the intended behavior. Valibot tries to behave similar to TypeScript and We can leave this issue open for now to see if more developers see this differently and if that's the case we can rethink |
I will say that, at the very least, it's unintuitive. However, from a TypeScript behavior view, Fabian is right. The resulting type is what you'd expect, but the data still conforms to that type. const schema = v.omit(
v.objectWithRest({
key1: v.string(),
key2: v.string()
}, v.unknown()),
['key1']
);
// resulting type
type Schema = {
key2: string;
} & {
[key: string]: unknown;
}
// No error
const data: Schema = {
key1: 'a',
key2: 'b'
}
// Same: No error
const result = v.parse(schema, data);
console.log(result); What you're looking for is to transform the result based on the properties you want to omit, rather than conforming to the normal omit behavior. This schema will do what you want. const newSchema = v.pipe(
v.objectWithRest({
key1: v.string(),
key2: v.string()
}, v.unknown()),
v.transform(({ key1, ...rest }) => rest)
); Playground Example@fabian-hiller I do think there should be a more intuitive method/action for this. I like the idea of having an |
|
@fabian-hiller, the However, as @sillvva noted, the behavior is unintuitive. For example, using As you mentioned, I strongly believe |
Here is another workaround that can be used in the meantime: const Schema = v.objectWithRest(
{ key1: v.never(), key2: v.string() },
v.unknown()
);
It only has no effect because the specified rest is marked as If we should change the behavior of |
@fabian-hiller I've tried this workaround but it gives an error when the key exists. The
can you please provide a quick example? btw huge thank you for the library and all the time you spend on its improvements and conversations |
Currently, you get a TS error if you specify an invalid key: |
@fabian-hiller that's what I've thought about, thank you. I'm not so deep into TS but is it possible to have |
Yes, that could be possible. |
omit
is not working when used withobjectWithRest
schemaexample:
expected
omit
removes specifiedkey1
entry, same as usingobject
schema:actual output
omit
doesn't remove any entry:valibot 0.34
example playground
related - #481
The text was updated successfully, but these errors were encountered: