Nullable object property fails to validate `null`
See original GitHub issueDescribe the bug
I’m working with an API that requires differentiating between properties that are T | undefined
or T | null
.
In OpenAPI 3.0, T | null
properties seem to be modelled like:
schemas:
TestObject:
type: object
properties:
otherObjectMaybeNullA:
$ref: "#/components/schemas/OtherObject"
nullable: true
otherObjectMaybeNullB:
anyOf:
- $ref: "#/components/schemas/OtherObject"
nullable: true
required:
- otherObjectMaybeNullA
- otherObjectMaybeNullB
OtherObject:
type: object
In both cases the validator does not accept null
as a valid value. Errors are 'should be object'
and 'should match some schema in anyOf'
.
To Reproduce
Validate above against { otherObjectMaybeNullA: null, otherObjectMaybeNullB: null }
.
Actual behavior
Validation fails.
Expected behavior
Validation should pass.
Examples and context
See above.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Yup how to validate mandatory fields on null object
The solution is to include both nullable() and required() to the shape. Example const requiredOption = Yup.object() .shape({ value: ...
Read more >Solved: Parse JSON - validation failed for null values - h...
I am attempting to parse the JSON, which mostly works... except when null values appear in the string, at which point the validation...
Read more >Avoid Check for Null Statement in Java - Baeldung
2. What Is NullPointerException? · Calling an instance method of a null object · Accessing or modifying a field of a null object...
Read more >Handling JSON null and empty arrays and objects - IBM
When serializing to JSON, if a value of a property in the data object is null, then it will be serialized as a...
Read more >How to allow None (null) for fields in JSON Schema?
however if my fb_id is set to None, the validation fails. ... If you also want to allow null values for fb_id, you...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@cdimascio
v4.5.0
Nullable objects can’t be validated if properties are defined in the openapi.json.
This one works (nestedObject can be null or object)
This one can’t be null (error: .response.ObjectOne.nestedObject should be object)
Thanks for looking into this! Yes, that seems to be a bit of a mess in OpenAPI 3.0. My interpretation of Clarify Semantics of nullable in OpenAPI 3.0 was that at least the second form is valid. Other indicators:
Looks like I’m a bit stuck in terms of work-arounds because:
nullable: true
toOtherObject
. This other object is intended to appear in non-nullable form in other places. I naively thought I can wrap it intoOtherObjectNullable
, but that again results in having to combine$ref
withnullable: true
. This maybe leaves duplicating all fields inOtherObject
andOtherObjectNullable
as an option, which calls for trouble when changing the fields and forgetting to sync them.