Giter VIP home page Giter VIP logo

Comments (2)

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

The coerce method has been removed because I felt it was an insecure API. In most cases, you don't want to coerce an unknown input into a specific data type. Instead, you want to transform a specific data type into another specific data type. For example, a string or a number into a date. To explicitly define the input type, we recommend using the new pipe method together with the transform action to achieve the same functionality.

// Before
const DateSchema = v.coerce(v.date(), (input) => new Date(input));

// After
const DateSchema = v.pipe(
  v.union([v.string(), v.number()]), // <-- Select which input types to accept
  v.transform((input) => new Date(input))
);

You can also accept unknown to have the same behavior as with the older versions.

const DateSchema = v.pipe(
  v.unknown()
  v.transform((input) => new Date(input))
);

from valibot.

chimame avatar chimame commented on June 17, 2024 1

I always appreciate a quick response.
Using unknown is a good workaround. I modified the code I presented in this way to get the expected behavior.

describe('sample', () => {
  it('should throw an error when the string is empty', () => {
    // When using version 0.30.0 of valibot
    // const schema = coerce(string(), (value) => value === '' ? undefined : value)
    // When using version 0.31.0-rc.1 of valibot
    const schema = pipe(unknown(), transform((value) => value === '' ? undefined : value), string())

    const result = safeParse(schema, '')
    expect(result.success).toBe(false)
  })
})

I will modify my library based on this code.
Thank you so much for your response. I hope Valibot, a great library, will be more widely adopted.

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.