jquense / yup

Dead simple Object schema validation
MIT License
22.72k stars 925 forks source link

Nested `defined` string not properly ignored #2243

Closed GravlLift closed 1 week ago

GravlLift commented 3 weeks ago

Describe the bug If an object's property is listed defined, but the object itself is notRequired, the property will fail validation even when an undefined is passed for the object.

To Reproduce

https://codesandbox.io/p/sandbox/mystifying-resonance-hhy8k6?file=%2Fsrc%2Findex.test.js%3A11%2C1

Expected behavior No validation error should be thrown for child properties when the parent object does not exist.

Platform (please complete the following information):

Additional context Basically I'm looking to validate something with this type:

type ObjectType = {
    property: string;
} | null | undefined

and it's throwing for property if the object is undefined, even though the InferType produces the correct type.

jquense commented 2 weeks ago

See the section "object defaults" in the docs under the object schema.