Skip to main content
Version: Current (v7.0.0-beta.3)

dependentRequired and dependentSchemas are read as dependencies

Detect​

  • In your schema: dependentRequired or dependentSchemas, the two keywords JSON Schema 2019-09 split dependencies into.
  • On the rendered page: once the object holds an entry's key, the properties a dependentSchemas entry declares render, and the fields either keyword requires are marked required.
  • On a submit: the browser's own validation stops one that went through before, at a field that now carries the required attribute.
  • In your tests or stored values: the form data such a form starts with or submits, or a retrieveSchema(), getDefaultFormState() or omitExtraData() result for such a schema.

Change​

The form read neither keyword, whichever validator it was given (#2241): a field that a dependentSchemas entry declares never rendered and took no default, and a field that a dependentRequired entry lists was not marked required. The form now reads a dependentRequired entry as a dependencies entry that lists keys, and a dependentSchemas entry as one that holds a schema. Once the object holds the entry's key, the listed fields are marked required, the properties of the entry's schema render, and their defaults are filled into the form data.

With the schema below, typing a value for a rendered no other field and left the form data at { "a": "x" }. It now renders b, marked required, and gives { "a": "x", "b": "B" }:

{
"type": "object",
"properties": { "a": { "type": "string" } },
"dependentSchemas": {
"a": { "properties": { "b": { "type": "string", "default": "B" } }, "required": ["b"] }
}
}
  • retrieveSchema() returns the schema with the entries that apply merged in and left out of the two keywords, where it returned both keywords as written. getDefaultFormState() fills those defaults, and omitExtraData() keeps the data of the properties such an entry declares.
  • An entry keyed by a property that the properties of the schema holding it don't declare, where that schema has any, is still not read, as under dependencies. The schema holding it is the object, or an allOf entry, a then/else, an option or another entry merged into it, and a property that an entry applied before it declares counts as declared. retrieveSchema() leaves it under its keyword while the data holds its key, so a 2019-09 or 2020-12 validator applies it as before.
  • An anyOf or oneOf that the object holds beside either keyword is unchanged. It still renders its option selector, where one beside dependencies is merged for the data with no selector.
  • The default @rjsf/validator-ajv8 validator is a draft-07 Ajv, which ignores both keywords, so a submit still reports no error for a dependent field that is left out. Live validation checks the schema with the root's entries merged in, so it reports a field such an entry requires.

Update the tests and stored values that expect the earlier fields and starting data. To have validation apply the keywords as the form does, import the validator from @rjsf/validator-ajv8/2019 or @rjsf/validator-ajv8/2020, as JSON Schema versions describes:

- import validator from '@rjsf/validator-ajv8';
+ import validator from '@rjsf/validator-ajv8/2020';

To keep the form as it was, leave the two keywords out of the schema that Form is given. Form validates with that same schema, so under a 2019-09 or 2020-12 validator this stops validation from applying them too, and a submit of { "a": "x" }, which such a validator rejected with must have required property 'b', then goes through:

{
"type": "object",
"properties": { "a": { "type": "string" } },
- "dependentSchemas": {
- "a": { "properties": { "b": { "type": "string", "default": "B" } }, "required": ["b"] }
- }
}

See Dependencies for how the form reads the two keywords.

Verify​

  • Once the object holds an entry's key, the dependent fields render with their defaults and are marked required.
  • A submit that leaves a required dependent field empty is stopped by the browser's own validation, and under a 2019-09 or 2020-12 validator by the validator too.