Incorrect validation of required child properties of optional properties?
See original GitHub issuePrerequisites
- [x ] I have read the documentation;
- [x ] In the case of a bug report, I understand that providing a SSCCE example is tremendously useful to the maintainers.
- Ideally, I’m providing a sample JSFiddle or a shared playground link demonstrating the issue.
Description
Form validation fails if a required child property of an optional object property is missing.
Steps to Reproduce
Add the following property schema to the “Simple” example in the playground:
"address": {
"type": "object",
"properties": {
"street": {
"type": "string"
},
"zip": {
"type": "string"
}
},
"required": ["zip"]
}
Submit the form without entering any address data
Expected behavior
Because address is optional and I did not enter any address, validation should succeed and the produced form should have no address property.
Actual behavior
Validation fails because the required zip property is missing.
Version
3.2.1
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
model not validating correctly in controller. Optional property ...
Basically I have RegisterModel2 that has a Person class. My requirement is that Phone Number and Address are not required for registration. But...
Read more >JSONSchemaType incorrectly requires optional properties to ...
This is a deliberate decision to require that optional properties are allowed to be null values, as it is how APIs are commonly...
Read more >node.js - OpenAPI: Mandatory properties of an Optional Property
I have an optional object in the request body. However, if that object is given, it should mandatorily contain a few child properties....
Read more >TypeScript: Transforming optional properties to required ...
This works by mapping over Required<T> , a version of our original type where all the optional properties have been replaced by required...
Read more >Validating Optional Objects with Yup - Object Partners
transform(function(value) { // If false is passed, skip the transform if (!isOptional) return value; // If any child property has a value, skip ......
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 Free
Top 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
It also looks like the whole way ‘required’ is handled needs reworking. The isRequired() method in ObjectField simply looks at the required property on the schema. The problem is that a ‘required’ property on an optional nested schema is not necessarily required. It would be better to simply rely on ajv validation so this code isn’t basically repeating validation logic that’s correctly implemented in ajv.
The “required” attribute is misinterpreted, when an NOT required element contains required element itself.
Checking out this example: An object with “name” (obligatory) and “link” (mandatory); the editor does NOT accept leaving “link” empty, because its inner fields are obligatory.
{ "title": "My Test", "description": "My description", "type": "object", "properties": { "name": { "description": "The name of the menu item", "type": "string" }, "link": { "$ref": "#/definitions/link" } }, "additionalProperties": false, "required": [ "name" ], "definitions": { "target": { "enum": [ "article", "push", "link", "dataprivacy", "licenses", "stage" ] }, "link": { "type": "object", "properties": { "url": { "type": "string" }, "target": { "$ref": "#/definitions/target" } }, "required": [ "url", "target" ] } } }
Possible but ugly workaround:
"link": { "oneOf": [ {"type": "null"}, {"$ref": "#/definitions/link"} ] }