Comments (10)
v0.29.0 is available
from valibot.
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.
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.
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.
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.
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.
@Mini-ghost do you have an opinion on renaming recursive
to lazy
? Do you have any other alternative ideas in mind?
from valibot.
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.
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.
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)
- Function to support for json type HOT 2
- Number literal don't trigger variant validation HOT 7
- `_addIssue` blocks `every`, `some` & `check` actions rewrite due to a possible type issue HOT 2
- Email regex pattern does not allow all valid characters HOT 1
- Are there any similarities about zod `.superRefine`? HOT 1
- Error when building HOT 6
- How to access other field? HOT 2
- Allow null default value while enforcing required validation HOT 2
- Force typeing on object schema HOT 2
- Investigate memory, performance and bundle size impact when caching basic schema objects
- Conditionally validate HOT 2
- Not correct lang after set globally lang HOT 3
- `forward` not working when `transform` field throwing error HOT 3
- [i18n] add Polish(pl) language
- Record with params (key, value, pipe) return error TS2769 HOT 2
- Invalid type error while trying to parse process.env object HOT 4
- Unable to use `lazy()` in `variant()` HOT 1
- New fields in existing schema HOT 4
- Type instantiation is excessively deep and possively Infinite HOT 2
- [i18n] Add Spanish language
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.