Comments (16)
This is the intended behavior. It is documented in our API reference, but I can understand why this might be confusing. I will think about it for further development of the library.
Hint: You can add the
rest
argument again when callingomit
.
from valibot.
Thank you very much! I will look into this in the next few weeks. Maybe we should also add an except
method in addition to the omit
functionality.
from valibot.
This is documented here: https://valibot.dev/api/omit/
Because
omit
changes the data type of the input and output, it is not allowed to pass a schema that has been modified by thepipe
method, as this may cause runtime errors. Please use thepipe
method after you have modified the schema withomit
.
I do not recommend your workaround, instead write your schema this way:
import * as v from "valibot";
const Schema = v.object({
email: v.pipe(v.string(), v.email()),
password: v.pipe(v.string(), v.minLength(8)),
});
const Without = v.pipe(v.omit(Schema, ["password"]), v.brand("Hello"));
from valibot.
@fabian-hiller I've read the note after opening the issue, still didn't figure out the must be added again
part to make expected output. Used transform
as a workaround for now.
If it's somehow possible to make it work using must be added again
(adding unknown()
again or smth else) I'd be happy to see an example and add to the docs 🙌
from valibot.
Check out our playground for an example.
import * as v from 'valibot';
const ObjectSchema1 = v.object(
{
key1: v.string(),
key2: v.string(),
key3: v.string(),
},
v.unknown()
);
const ObjectSchema2 = v.omit(
ObjectSchema1,
['key2'],
v.unknown() // <-- Add `rest` again
);
from valibot.
@fabian-hiller thank you for the example. I'm still confused 😅
Here's my playground
From what I expect, key2
should be omitted.
However it's still in the output.
Basically ObjectSchema2
does nothing 🤔
This key is also removed from autocomplete but we see it's there in the output:
I remember trying it with the same result.
p.s. it may be the intended behavior and just not the one I'd expect from the code
from valibot.
Because of the rest
argument v.unknown()
, the schema allows any unknown entry to pass. This includes key2
even if you have omitted it.
from valibot.
@fabian-hiller yeah, it kinda reverts what omit()
does.
So basically it's not possible to omit key2
from object and leave rest unknown props untouched using omit()
function, only transform
can do this.
I'd consider this a bug in omit()
since it clearly does more than just omitting key2
by omitting all unknown props from the object.
from valibot.
How would you do the same with pure TypeScript?
from valibot.
@fabian-hiller a quick one:
const data = {
a: 'a',
b: 'b',
c: 'c',
d: 'd'
}
function omit<T, K extends keyof T>(object: T, keys: K[]): Omit<T, K> {
const result = { ...object }
keys.forEach((key) => {
delete result[key]
})
return result
}
const result = omit(data, ['a', 'b'])
console.log(result)
console.log(result.c)
I'd probably use some kind of Object.keys
, Object.entries
, Object.fromEntries
or reduce
but it requires a lot more time spent on typings 😅
from valibot.
And how would you implement it if you wanted it to allow unknown entries to pass? I am not sure if I understand you correctly, but I think you want to obmit a specific key, but also allow any unknown key to pass. If that's your goal, there's probably only one solution: You need to explicitly declare that key as never
.
from valibot.
Yes, thats exactly what I want - omit only specific key and leave the rest untouched (be it specified keys from schema or unknown ones).
// initial object
const data = { a: 1, b: 2, c: 3, d: 4 }
// remove ONLY a
// result is { b: 2, c: 3, d: 4 }
omit(data, ['a'])
// remove ONLY a AND c
// result is { b: 2, d: 4 }
omit(data, ['a', 'c'])
incorrect
I've tried Typescript Omit for correct output type but it's not smart enough for index signature:
type Data = {
[key: string]: unknown
a: number
b: number
c: number
d: number
}
type Omitted = Omit<Data, 'a'>
The output type loses the overview of defined keys.
correct
The correct one can be seen using Except
from magical 🧙 type-fest:
import type { Except } from 'type-fest'
type Data = {
[key: string]: unknown
a: number
b: number
c: number
d: number
}
type Omitted = Except<Data, 'a'>
No wonder type-fest has over 160 million downloads 😅
That's basically what I expect omit
to do.
I also believe this is how other libraries work (tested on lodash omit)
- TS issue: microsoft/TypeScript#30825 (comment)
- Zod issue with suggested workaround: colinhacks/zod#2052 (hello
transform
)
from valibot.
Same bug
conclusion from type-fest
with identical examples and expectations - sindresorhus/type-fest#382
Good read on the topic - microsoft/TypeScript#30825
from valibot.
Hey folks! I have been trying out the latest (0.31.0-rc.6
) and was noticing some other breaking changes regarding .omit
with the new pipeline changes. Can create as a separate issue if need be, but I was able to repro in the playground. At the same time, I was able to find a workaround. Maybe helpful in the docs?
from valibot.
Thank you for the reply!
I missed that callout in the docs. A code example would make this more apparent I think.
fwiw, my real use case had a base Schema that included a brand A
that I was omitting a single property from to define the new Schema. Are you saying that the more correct approach would be to let the base Schema be defined unbranded, then derive the two variants with their own distinct brand?
from valibot.
Are you saying that the more correct approach would be to let the base Schema be defined unbranded, then derive the two variants with their own distinct brand?
Yes, this is the recommended way at this time. I understand that this can be cumbersome in some cases. That's why I will consider adding something like a removePipe
method. That way you could simply remove the pipe before applying omit
. Here is an example:
import * as v from "valibot";
const Schema = v.pipe(
v.object({
email: v.pipe(v.string(), v.email()),
password: v.pipe(v.string(), v.minLength(8)),
}),
v.brand("Hello"),
);
const Without = v.omit(v.removePipe(Schema), ["password"]);
What do you think of this idea? What could be alternative names for this function? I prefer short but still meaningful names.
from valibot.
Related Issues (20)
- [i18n] Add Spanish language HOT 1
- Empty array in optional as a default value turns the type into union of the type or never HOT 6
- Build error HOT 4
- Add `items` action HOT 1
- Can't run test code using 0.31.0-rc HOT 6
- Add an advanced `refine` method (similar to Zod's `superRefine`) HOT 3
- [0.31.0-rc] Enable to perform transform on the first argument of pipe method HOT 2
- EMOJI_REGEX throws Invalid regular expression flags on node v18 HOT 4
- TypeScript: InferInput always infers intersect as 'never' HOT 3
- [v0.31.0-rc.5] `Invalid type: Expected Object but received Object` because of `edge` runtime HOT 3
- abortPipeEarly influnces on abortEarly HOT 2
- Compiled type for actions missing typing for 'requirement' in ErrorMessage HOT 2
- [v0.31.0-rc.5] The internal function `_stringify` has a problem HOT 23
- cannot pipe a pipe HOT 2
- Enum error with React Hook Form HOT 6
- Reduce boilerplate for custom actions HOT 3
- Forward is changing the issues paths on flatten with an error in the forwarded property HOT 4
- @valibot/i18n could not be installed HOT 1
- Pipe to emit a single issue HOT 1
- Predefined keys for `ObjectSchema` HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from valibot.