Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for dependentSchemas and dependentRequired #3189

Closed
1 task done
lucaswiman opened this issue Oct 7, 2022 · 2 comments · Fixed by #3200 or #3201
Closed
1 task done

Support for dependentSchemas and dependentRequired #3189

lucaswiman opened this issue Oct 7, 2022 · 2 comments · Fixed by #3200 or #3201
Labels
feature Is a feature request help wanted

Comments

@lucaswiman
Copy link

Prerequisites

What theme are you using?

core

Is your feature request related to a problem? Please describe.

Per json-schema.org, the dependencies keyword was split into two keywords: dependentSchemas and dependentRequired:
image

This means that some schemas behave differently between react-jsonschema-form and e.g. python's jsonschema library, which does not support the dependencies keyword. See https://github.com/python-jsonschema/jsonschema/blob/bf92aed44eb317188fba68e558d0429c68259dee/jsonschema/_legacy_validators.py#L56-L67

In this playground, selecting "1" in the a dropdown does not produce the "b" dropdown. With dependencies instead of dependentSchemas, the form works as expected.

Using another jsonschema validator on the backend (python's jsonschema==4.15.0, with the jsonschema.validate method) yields opposite behavior, with dependencies validating {"a": "1"} and dependentSchemas doing the opposite. Using jsonschema.Draft7Validator(schema=schema).validate(...) instead yields similar behavior to RJSF.

Describe the solution you'd like

One of the following:

  1. Implement the new keywords to accord with the latest draft. It may be a good idea to also allow use of the dependencies keyword for backwards compatibility or generate a console warning if dependencies is used.
  2. Update the documentation to mention that dependentSchemas is not supported, and recommend a particular draft validator version to use with RJSF.

Describe alternatives you've considered

Using the dependencies keyword, and using the Draft7 validator in the Python backend. This works, but was confusing until I figured it out, and means that my schemas may be out of date with other tools using current jsonschema versions.

@lucaswiman lucaswiman added feature Is a feature request needs triage Initial label given, to be assigned correct labels and assigned labels Oct 7, 2022
@heath-freenome
Copy link
Member

@lucaswiman This is definitely something that we'll need to support. There is a non-trivial amount of work needed for this, including:

  • Changing all the types that use RJSFSchema as the type for the schema to replace it with a generic that defaults to that type.
  • Updating the validator-ajv8 to allow one to specify during construction which of the three schemas that ajv v8 supports
  • Updating all of the themes to properly allow the specifying of generics (already covered by Fix Typescript generics support in all themes in the beta #3072)

Are the first two things something you'd be willing to work on if the last item is fixed?

@heath-freenome heath-freenome added help wanted and removed needs triage Initial label given, to be assigned correct labels and assigned labels Oct 10, 2022
heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this issue Oct 17, 2022
- Updated `@rjsf/utils` to make the `schema` and `rootSchema` props use a new generic type `S`
  - Added the new `StrictRJSFSchema` type as the alias to `JSON7Schema` changing `RJSFSchema` to be `StrictRJSFSchema & GenericObjectType`
  - Deleted the `RJSFSchemaDefinition` type in favor of accessing it indirectly via the `S["<prop-with-definition>"]` syntax
  - Added the new generic `S extends StrictRJSFSchema = RJSFSchema` to all types that directly or indirectly used `RJSFSchema` after the `T = any` type
  - Updated `SchemaUtilsType` to add the `F = any` generic to the whole interface, removing it from the definition of the two functions that need it
  - Updated all functions that used `RJSFSchema` to take the new generic, replacing `RJSFSchema` with `S`
  - Added missing generics where needed
- Updated `@rjsf/core` to insert the `S extends StrictRJSFSchema = RJSFSchema` to every component that needed it, after the `T = any` generic
  - Updated the `index.ts` for the `ButtonTemplates`, `field`, `templates` and `widgets` to make them functions that take the `T`, `S` and `F` generics
  - Updated `getDefaultRegistry()` and `templates()` to call the appropriate functions
  - Replaced all uses of `RJSFSchema` and `RJSFSchemaDefinition` with `S` and `S["<prop-with-definition>"]`
  - Added missing generics where needed
  - Fixed a few type casts due the to the change in the `RJSFSchema` type
- Updated `@rjsf/validator-ajv6` to fix a few type casts due to the change in the `RJSFSchema` type
- Updated `@rjsf/validator-ajv8` to add the `S extends StrictRJSFSchema = RJSFSchema` generic to the `customizeValidator()` function and the `AJV8Validator` class
  - Replaced all uses of `RJSFSchema` and `RJSFSchemaDefinition` with `S` and `S["<prop-with-definition>"]`
  - Changed `RJSFSchema` to `S` where applicable
  - Fixed a few type casts due the to the change in the `RJSFSchema` type
heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this issue Oct 17, 2022
- Updated `@rjsf/utils` to make the `schema` and `rootSchema` props use a new generic type `S`
  - Added the new `StrictRJSFSchema` type as the alias to `JSON7Schema` changing `RJSFSchema` to be `StrictRJSFSchema & GenericObjectType`
  - Deleted the `RJSFSchemaDefinition` type in favor of accessing it indirectly via the `S["<prop-with-definition>"]` syntax
  - Added the new generic `S extends StrictRJSFSchema = RJSFSchema` to all types that directly or indirectly used `RJSFSchema` after the `T = any` type
  - Updated `SchemaUtilsType` to add the `F = any` generic to the whole interface, removing it from the definition of the two functions that need it
  - Updated all functions that used `RJSFSchema` to take the new generic, replacing `RJSFSchema` with `S`
  - Added missing generics where needed
- Updated `@rjsf/core` to insert the `S extends StrictRJSFSchema = RJSFSchema` to every component that needed it, after the `T = any` generic
  - Updated the `index.ts` for the `ButtonTemplates`, `field`, `templates` and `widgets` to make them functions that take the `T`, `S` and `F` generics
  - Updated `getDefaultRegistry()` and `templates()` to call the appropriate functions
  - Replaced all uses of `RJSFSchema` and `RJSFSchemaDefinition` with `S` and `S["<prop-with-definition>"]`
  - Added missing generics where needed
  - Fixed a few type casts due the to the change in the `RJSFSchema` type
- Updated `@rjsf/validator-ajv6` to fix a few type casts due to the change in the `RJSFSchema` type
- Updated `@rjsf/validator-ajv8` to add the `S extends StrictRJSFSchema = RJSFSchema` generic to the `customizeValidator()` function and the `AJV8Validator` class
  - Replaced all uses of `RJSFSchema` and `RJSFSchemaDefinition` with `S` and `S["<prop-with-definition>"]`
  - Changed `RJSFSchema` to `S` where applicable
  - Fixed a few type casts due the to the change in the `RJSFSchema` type
heath-freenome added a commit that referenced this issue Oct 18, 2022
* fix: Partially fix #3189
- Updated `@rjsf/utils` to make the `schema` and `rootSchema` props use a new generic type `S`
  - Added the new `StrictRJSFSchema` type as the alias to `JSON7Schema` changing `RJSFSchema` to be `StrictRJSFSchema & GenericObjectType`
  - Deleted the `RJSFSchemaDefinition` type in favor of accessing it indirectly via the `S["<prop-with-definition>"]` syntax
  - Added the new generic `S extends StrictRJSFSchema = RJSFSchema` to all types that directly or indirectly used `RJSFSchema` after the `T = any` type
  - Updated `SchemaUtilsType` to add the `F = any` generic to the whole interface, removing it from the definition of the two functions that need it
  - Updated all functions that used `RJSFSchema` to take the new generic, replacing `RJSFSchema` with `S`
  - Added missing generics where needed
- Updated `@rjsf/core` to insert the `S extends StrictRJSFSchema = RJSFSchema` to every component that needed it, after the `T = any` generic
  - Updated the `index.ts` for the `ButtonTemplates`, `field`, `templates` and `widgets` to make them functions that take the `T`, `S` and `F` generics
  - Updated `getDefaultRegistry()` and `templates()` to call the appropriate functions
  - Replaced all uses of `RJSFSchema` and `RJSFSchemaDefinition` with `S` and `S["<prop-with-definition>"]`
  - Added missing generics where needed
  - Fixed a few type casts due the to the change in the `RJSFSchema` type
- Updated `@rjsf/validator-ajv6` to fix a few type casts due to the change in the `RJSFSchema` type
- Updated `@rjsf/validator-ajv8` to add the `S extends StrictRJSFSchema = RJSFSchema` generic to the `customizeValidator()` function and the `AJV8Validator` class
  - Replaced all uses of `RJSFSchema` and `RJSFSchemaDefinition` with `S` and `S["<prop-with-definition>"]`
  - Changed `RJSFSchema` to `S` where applicable
  - Fixed a few type casts due the to the change in the `RJSFSchema` type

* Update packages/validator-ajv8/src/validator.ts

Fix comment

Co-authored-by: Nick Grosenbacher <[email protected]>

Co-authored-by: Nick Grosenbacher <[email protected]>
heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this issue Oct 18, 2022
…ma Ajv classes

Fix rjsf-team#3189 by extending the `@rjsf/validator-ajv8` to support the `Ajv2019` and `Ajv2020` classes
- Updated the `CustomValidatorOptionsType` to add a new `AjvClass` option
- Updated the `createAjvInstance()` function to use the `AjvClass` option is provided to create the `Ajv` class instance, falling back to the default `Ajv`
- Updated the `AJV8Validator` to extract the `AjvClass` from the options and pass it to the `createAjvInstance()` function
- Updated the tests to also test the `Ajv2019` and `Ajv2020` class instances
- Updated the `validation.md` file to document the new `AjvClass` options, switching the examples over to `tsx`
- Updated the `CHANGELOG.md` file accordingly
@lucaswiman
Copy link
Author

lucaswiman commented Oct 18, 2022

Are the first two things something you'd be willing to work on if the last item is fixed?

I'm still a typescript novice, so I don't think I could commit to making those fixes. I'd be happy to help with updating the documentation to indicate that draft 7 is the version that RJSF uses for now, though it looks like you're changing that quickly.

heath-freenome added a commit that referenced this issue Oct 21, 2022
…asses (#3201)

* fix: #3189 by allowing the ajv 8 validator to alternate schema Ajv classes
Fix #3189 by extending the `@rjsf/validator-ajv8` to support the `Ajv2019` and `Ajv2020` classes
- Updated the `CustomValidatorOptionsType` to add a new `AjvClass` option
- Updated the `createAjvInstance()` function to use the `AjvClass` option is provided to create the `Ajv` class instance, falling back to the default `Ajv`
- Updated the `AJV8Validator` to extract the `AjvClass` from the options and pass it to the `createAjvInstance()` function
- Updated the tests to also test the `Ajv2019` and `Ajv2020` class instances
- Updated the `validation.md` file to document the new `AjvClass` options, switching the examples over to `tsx`
- Updated the `CHANGELOG.md` file accordingly

* - Added support for the 2019 and 2020 schemas from AJV 8 to the playground
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Is a feature request help wanted
Projects
None yet
2 participants