question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Incorrect validation of required child properties of optional properties?

See original GitHub issue

Prerequisites

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:open
  • Created 2 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
dieseldjangocommented, Feb 28, 2022

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.

0reactions
pkcpkccommented, Mar 9, 2022

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"} ] }

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found