rjsf-team / react-jsonschema-form

A React component for building Web forms from JSON Schema.
https://rjsf-team.github.io/react-jsonschema-form/
Apache License 2.0
14.3k stars 2.19k forks source link

Possible Bug - Validation uses schema not passed in to component #3451

Closed davkap92 closed 1 year ago

davkap92 commented 1 year ago

Prerequisites

What theme are you using?

antd

Version

5.x

Current Behavior

I created two

instances both using different schemas, call them schema1 and schema2

Form 1 - enter invalid data, error displayed (expected)

Form 2 - ent invalid data, error displayed but also displays error from schema1(unexpected)

Expected Behavior

Both Forms should only validate against the Schema passed into the Form component

Steps To Reproduce

  1. Don't fill out any data, hit submit on form 1 -> employee id error shown
  2. Don't fill out any data, hit submit on form 2 -> employee id error shown (there is no employee id field)

After lots of debugging it seems $id is used to fetch the schema, As per the below sandbox, i had the same $id: example.com/schema for both schema1 and schema2

This seems like unexpected behaviour to me that it fetches the schema by id rather than what's being passed in? https://codesandbox.io/s/dark-tree-3hommm

Environment

- OS:
- Node:
- npm:

Anything else?

No response

nickgros commented 1 year ago

@davkap92 in @rjsf/validator-ajv8 we are using Ajv's instance cache, which uses the $id as the cache key: https://ajv.js.org/guide/managing-schemas.html#using-ajv-instance-cache

We could possibly expose a way to disable or clear the cache, but is there a compelling reason you need to use the same $id for different schemas?

davkap92 commented 1 year ago

@nickgros ok I see, it just feels a bit counterintuitive that the schema passed into <Form> gets ignored in this scenario, maybe at least there could be a console warning using a hash of the schema or something indicating that the one it's planning to use isn't what you passed in? I didn't know that the validator being imported is essentially a singleton with one instance which is why it was also confusing

Not really a compelling reason, just that in the app being developed, our customer dictates this so we can't ensure it will be unique per schema. For now as workaround I'm just deleting the $id field from the schema which forces it to use the correct schema, not the cached one

So yeah i guess more just about some sort of visibility of this behavior could be helpful. thanks

heath-freenome commented 1 year ago

@nickgros will be updating the docs with a bit more information that addresses this issue.