Please see this example: https://runkit.com/daddywarbucks/5c8c3cefcb8f3600126ace2f
emptySchema behaves as I would expect and throws a validation error because the Object that is supposed to be at thing is undefined.
fullSchema passes validation, which it should not I believe. The only difference is that thing is no longer an "empty" object schema and instead has a property.
@DaddyWarbucks looks like you have put required on something inside the object...
https://runkit.com/morgs32/5c8fb75d1176b70012580cc6
@morgs32 Thanks for looking at this but I don't think that solves the "problem". I have a general understanding of why this is the case. In the emptySchema example, there is nothing inside the object to cast and it therefore casts to undefined as it should. In the fullSchema, there are properties in the object and those must get cast somewhere...and cast happens before validation. So the cast phase itself creates an object with a property inside of it, which passes a required validation on the object itself that was created, which happens after cast.
Perhaps there is no good away around this and it is probably not a super painful problem for most users. I was doing some very custom validation with nested objects and default fields when I stumbled across this.
I see. Meanwhile, I'm wondering about the case where fullSchema itself should be optional, and only IF we have an object, should its keys be required...
https://runkit.com/morgs32/5c927ac777af19001278460a
Meanwhile, I'm wondering about the case where fullSchema itself should be optional, and only IF we have an object, should its keys be required
I am also wondering about this case. Why required in nested properties makes parent object automatically required? Is there any way to avoid it?
https://runkit.com/felixcatto/5cd2ab120c224d001a6e9bdf
UPD: have found the solution. Need to add .default(null).nullable() so result will be like this
source: object({
name: yup.mixed().required()
}).default(null).nullable()
based on https://github.com/jquense/yup/issues/348#issuecomment-433582869
similar problem. I have such a schema
yup.object({
id: yup.string().required(),
updates: yup
.object({
name: yup.reach(schema, "name"),
desc: yup.reach(schema, "desc"),
logo: yup.reach(schema, "logo"),
enabled: yup.reach(schema, "enabled")
})
.required()
})
updates field is required, but {id: 'xxx'} would pass the validation, why???
I had this same problem too. Thanks @felixcatto for the solution. Another solution would probably be writing this custom validator - https://github.com/jquense/yup/issues/505
Most helpful comment
I am also wondering about this case. Why
requiredin nested properties makes parent object automaticallyrequired? Is there any way to avoid it?https://runkit.com/felixcatto/5cd2ab120c224d001a6e9bdf
UPD: have found the solution. Need to add
.default(null).nullable()so result will be like thisbased on https://github.com/jquense/yup/issues/348#issuecomment-433582869