Hello, I've been digging through documentation and it's either that I'm missing something or I've stumbled on a bug.
I have the following scenario
const CurrencySchema = Yup.object().shape({
value: Yup.string().required(),
currency: Yup.string().oneOf(['EUR', 'USD']).required(),
});
Now I want to use this schema on some other schema but make it nullable, allowing nulls and undefineds.
const WhateverSchema = Yup.object().shape({
// other properties
price: CurrencySchema.notRequired().nullable(true)
});
Running WhateverSchema against a JSON like { "price": null } will tell me that both price.value and price.currency are required. Am I missing something?
I have the same problem. I think as a default, if a prop of type yup object is undefined, it should completely ignore its validation.
If the value is undefined it uses the default value for the schema, which in objects builds the object with defaults of it's fields. If you don't want that behavior you can use null it explicitly set the objectd default to undefined or nu
@jquense thank you! will try that.
@emanuelmd for me the case with null works. (only undefined doesn't work)
please try
const WhateverSchema = Yup.object().shape({
// other properties
price: CurrencySchema.nullable(true)
});
and make sure that the value of price is really null and not undefined. Works for me.
Yeah, my point was to allow both undefined AND null. I ended up using lazy, to only validate the object when it's different from null and undefined.
Yeah, my point was to allow both undefined AND null. I ended up using lazy, to only validate the object when it's different from null and undefined.
Yes, you dan easily do that with @jquense suggestion:
const WhateverSchema = Yup.object().shape({
// other properties
price: CurrencySchema.nullable(true).default(null)
});
Most helpful comment
Yes, you dan easily do that with @jquense suggestion:
const WhateverSchema = Yup.object().shape({
// other properties
price: CurrencySchema.nullable(true).default(null)
});