Giter VIP home page Giter VIP logo

Comments (10)

fabian-hiller avatar fabian-hiller commented on May 23, 2024 2

v0.29.0 is available

from valibot.

fabian-hiller avatar fabian-hiller commented on May 23, 2024 1

Thank you for creating this issue! We could easily support this functionality by passing input to the getter call in line 36. Feel free to create a PR.

Because of this change, the name lazy may be more appropriate than recursive but please do not change it for now. Also, this looks similar to the idea of switch by @colinhacks in issue #2106 of Zod.

from valibot.

Mini-ghost avatar Mini-ghost commented on May 23, 2024 1

I fully support this idea. 🙌

In fact, when I work on my projects, my initial instinct is to use lazy. However, it was only after consulting the documentation that I discovered the function name used in valibot is recursive. This initially seemed a bit unintuitive to me, so changing it to lazy would be excellent!

Regarding switch of zod, I haven't yet delved deeply into understanding it, so I will need some time to fully grasp its context.

from valibot.

fabian-hiller avatar fabian-hiller commented on May 23, 2024 1

As far as I understand it, z.switch seems to be exactly the same as z.lazy. I am currently planning to rename recursive to lazy. Thanks for your feedback on this.

from valibot.

fabian-hiller avatar fabian-hiller commented on May 23, 2024 1

Thank you very much! I think your schema is better implemented with variant. However, I still see advantages and flexibility in a lazy schema in special cases.

import * as v from 'valibot';

const Schema = v.variant('placeOfBirth', [
  v.object({
    placeOfBirth: v.literal('Taipei'),
    idCard: v.string([
      v.regex(/^A\d{9}$/u, 'Should be A followed by 9 digits'),
    ]),
  }),
  v.object({
    placeOfBirth: v.literal('Taichung'),
    idCard: v.string([
      v.regex(/^B\d{9}$/u, 'Should be B followed by 9 digits'),
    ]),
  }),
  v.object({
    placeOfBirth: v.string([v.minLength(1, 'Place of birth is required')]),
    idCard: v.string([v.minLength(1, 'ID card is required')]),
  }),
]);

from valibot.

fabian-hiller avatar fabian-hiller commented on May 23, 2024 1

Thank you very much! Feel free to contact me if you have any questions on variant.

I have already implemented the changes for renaming it to lazy since it is a lot of work in the documentation.

If you are familiar with Yup, you could write a migration guide similar to the one I wrote for Zod.

from valibot.

fabian-hiller avatar fabian-hiller commented on May 23, 2024

@Mini-ghost do you have an opinion on renaming recursive to lazy? Do you have any other alternative ideas in mind?

from valibot.

fabian-hiller avatar fabian-hiller commented on May 23, 2024

Before renaming recursive to lazy, I'd like to know what your use case is for using recursive for a non-recursive schema, to investigate if your goal can be better achieved with another schema function of Valibot. It seems to me that the example above can be rewritten with union.

from valibot.

Mini-ghost avatar Mini-ghost commented on May 23, 2024

The example I provided was extracted from the documentation of yup.lazy, primarily to highlight the capability to adjust the schema based on the input.

In addition to the reasons mentioned in #441, I'd like to offer a real-world example for further clarification:

const order = {
  placeOfBirth: 'Taichung',
  idCard: 'B123456789'
}

In Taiwan, the first letter of the ID card number indicates the place of birth, hence different placeOfBirth values correspond to different idCard formats. If the getter of v.recursive() could accept parameters, then it could be written as follows:

const schema = recursive((input) => {
  if (input.placeOfBirth === 'Taipei') {
    return object({
      placeOfBirth: string(),
      idCard: string([custom((value) => /^A\d{9}$/.test(value), 'Should be A followed by 9 digits')])
    })
  } else if (input.placeOfBirth === 'Taichung') {
    return object({
      placeOfBirth: string(),
      idCard: string([custom((value) => /^B\d{9}$/.test(value), 'Should be B followed by 9 digits')])
    })
  } else {
    return object({
      placeOfBirth: string([minLength(1, 'Place of birth is required')]),
      idCard: string([minLength(1, 'ID card is required')])
    })
  }
})

This approach allows for the idCard format to be determined based on the value of placeOfBirth.

If the validation rules are defined dynamically based on the values being validated, I think the name lazy might be more intuitive in this context, as it suggests a sort of on-demand generation. I have reviewed the examples in the documentation and understand the rationale behind naming it recursive. Is there a possibility to either keep recursive or allow both names to coexist?

I hope this case study is helpful to you.

from valibot.

Mini-ghost avatar Mini-ghost commented on May 23, 2024

Thank you for your reply. I will look further into the method of variant, as it truly seems fantastic.

Additionally, if you decide to rename recursive to lazy, I would be happy to contribute to this change.

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.