React-jsonschema-form: Nested schema objects with required properties in dependencies validate incorrectly

Created on 18 Apr 2018  路  1Comment  路  Source: rjsf-team/react-jsonschema-form

Prerequisites

  • [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.

Description

Nested schema objects with required properties in dependencies validate incorrectly

Steps to Reproduce

I'm providing a sample JSFiddle .

Select Address in the sections drop down
Click Submit

Expected behavior

The Form validates correctly and renders errors on Street and Street Number

Actual behavior

The Form validates with no errors and invokes the onSubmit prop

Debug info

The error seems to be with ajv.js Ajv will not validate against nested schemas in dependencies unless the formData for the schema has been touched.

Ie setting formData.address = {} will validate correctly

screen shot 2018-04-19 at 10 41 01 am

It may be worth opening an issue in AJV

Most helpful comment

I can repro this with a much simpler schema. See this JSFiddle.

Note that this capability is specifically called out as being supported, here.

The schema for reference here:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {"enabled": {"type": "boolean", "default": false}},
  "required": ["enabled"],
  "dependencies": {
    "enabled": {
      "oneOf": [
        {
          "properties": {"enabled": {"type": "boolean", "const": false}},
          "additionalProperties": false
        },
        {
          "properties": {
            "enabled": {"type": "boolean", "const": true},
            "dependentBoolean": {
              "type": "boolean",
              "default": true
            }
          },
          "required": [
            "enabled",
            "dependentBoolean"
          ],
          "additionalProperties": false
        }
      ]
    }
  }
}

rjsf fails submission of this valid data:

{
  "enabled": false
}

rjsf allows submission of this invalid data:

{
  "enabled": true
}

ajv command line correctly rejects this invalid data:

[ { keyword: 'const',
    dataPath: '.enabled',
    schemaPath: '#/dependencies/enabled/oneOf/0/properties/enabled/const',
    params: { allowedValue: false },
    message: 'should be equal to constant' },
  { keyword: 'required',
    dataPath: '',
    schemaPath: '#/dependencies/enabled/oneOf/1/required',
    params: { missingProperty: 'dependentBoolean' },
    message: 'should have required property \'dependentBoolean\'' },
  { keyword: 'oneOf',
    dataPath: '',
    schemaPath: '#/dependencies/enabled/oneOf',
    params: { passingSchemas: null },
    message: 'should match exactly one schema in oneOf' } ]

Furthermore, the default value for dependentBoolean does not get filled in the formData when enabled becomes true, as expected.

>All comments

I can repro this with a much simpler schema. See this JSFiddle.

Note that this capability is specifically called out as being supported, here.

The schema for reference here:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {"enabled": {"type": "boolean", "default": false}},
  "required": ["enabled"],
  "dependencies": {
    "enabled": {
      "oneOf": [
        {
          "properties": {"enabled": {"type": "boolean", "const": false}},
          "additionalProperties": false
        },
        {
          "properties": {
            "enabled": {"type": "boolean", "const": true},
            "dependentBoolean": {
              "type": "boolean",
              "default": true
            }
          },
          "required": [
            "enabled",
            "dependentBoolean"
          ],
          "additionalProperties": false
        }
      ]
    }
  }
}

rjsf fails submission of this valid data:

{
  "enabled": false
}

rjsf allows submission of this invalid data:

{
  "enabled": true
}

ajv command line correctly rejects this invalid data:

[ { keyword: 'const',
    dataPath: '.enabled',
    schemaPath: '#/dependencies/enabled/oneOf/0/properties/enabled/const',
    params: { allowedValue: false },
    message: 'should be equal to constant' },
  { keyword: 'required',
    dataPath: '',
    schemaPath: '#/dependencies/enabled/oneOf/1/required',
    params: { missingProperty: 'dependentBoolean' },
    message: 'should have required property \'dependentBoolean\'' },
  { keyword: 'oneOf',
    dataPath: '',
    schemaPath: '#/dependencies/enabled/oneOf',
    params: { passingSchemas: null },
    message: 'should match exactly one schema in oneOf' } ]

Furthermore, the default value for dependentBoolean does not get filled in the formData when enabled becomes true, as expected.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

j-zimnowoda picture j-zimnowoda  路  3Comments

abhishekpdubey picture abhishekpdubey  路  3Comments

norim13 picture norim13  路  3Comments

n1k0 picture n1k0  路  3Comments

ClockerZadq picture ClockerZadq  路  3Comments