jquense / yup

Dead simple Object schema validation
MIT License
22.94k stars 935 forks source link

Nested `defined` string not properly ignored #2243

Closed GravlLift closed 2 months ago

GravlLift commented 3 months 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 3 months ago

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