Coder Social home page Coder Social logo

Comments (16)

fabian-hiller avatar fabian-hiller commented on June 2, 2024 1

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 calling omit.

from valibot.

fabian-hiller avatar fabian-hiller commented on June 2, 2024 1

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.

fabian-hiller avatar fabian-hiller commented on June 2, 2024 1

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 the pipe method, as this may cause runtime errors. Please use the pipe method after you have modified the schema with omit.

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.

vladshcherbin avatar vladshcherbin commented on June 2, 2024

@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.

fabian-hiller avatar fabian-hiller commented on June 2, 2024

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.

vladshcherbin avatar vladshcherbin commented on June 2, 2024

@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:

image

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.

fabian-hiller avatar fabian-hiller commented on June 2, 2024

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.

vladshcherbin avatar vladshcherbin commented on June 2, 2024

@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.

fabian-hiller avatar fabian-hiller commented on June 2, 2024

How would you do the same with pure TypeScript?

from valibot.

vladshcherbin avatar vladshcherbin commented on June 2, 2024

@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.

fabian-hiller avatar fabian-hiller commented on June 2, 2024

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.

vladshcherbin avatar vladshcherbin commented on June 2, 2024

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'>
image

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'>
image

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)

from valibot.

vladshcherbin avatar vladshcherbin commented on June 2, 2024

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.

ct-gdf avatar ct-gdf commented on June 2, 2024

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?

edit: added playground link + wording
https://valibot.dev/playground/?code=JYWwDg9gTgLgBAKjgQwM5wG5wGZQiOAIg2QBtgAjCGQgbgCh6BjCAO1XgGUmALAUxDI4AXkwA6MMDB8AFPTjiIFAFZ8mMGQG95CuAOTBSALnGTpMjGI5RgrAOYyAlABpx+w05c6FYNKgDu0AAmJpZmspbWtg4u4iC2ADJ89jA8MgAcjl4KAL7Z4hRQyKxBMoQAEnykpBCEXo4MzGwccADqwKkQAK7wopb4HTLc-IKuANqEvqgBwYQAug2MLOzwUHyoXaQwAIwi4qjI2HwACshQqLLtnT2u2gruxkTKxXwAAnwAHsjgpHxiLCBCM4dFMZlAQkRtgAmADMABYAKwANgA7OkgfQ8o1li1WtAANZnbolPZhKSyHTDfTAhSWGBFdjYaAgGQyJSOEQAPjgd10v3gmjgoMC4NcYnFAxgMD4QTgOT2SgYujgaxgXSgrDgkulQSVcvq2Oaq3WmxgUNJVkOJzOFxkeKghLwXRKtx0DxMhGerDen2+YF+-3wGJ8fhFEMI0PhyLRGKxS2aEADNQcgrWGy221cadN5rjQA

from valibot.

ct-gdf avatar ct-gdf commented on June 2, 2024

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.

fabian-hiller avatar fabian-hiller commented on June 2, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.