-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Optional object with required fields in react-jsonschema-form #675
Comments
I'm surprised with this behavior too, and the jsonschema lib is not at fault here as validating raw data works just as expected. We may be altering the schema in some way, which is bad. |
Has this fix been merged? If not, is there a target for merging it? My org has started using the plugin but are getting lots of invalid data due to turning off validation due to required fields within non-required objects. |
This has not been merged. There are a few outstanding issues that need to be worked through. You can see the discussion at the merge request (#682). Unfortunately, I do not have time to cleanup the issues that need to be worked through in the immediate future. I am sure that if these issues were addressed the fix would be quickly merged. |
Any news on this one? |
If someone else comes across this issue and can not wait for a real fix, you can extend the Form component and override the validate function. Here's an example in Typescript: class ReactJsonSchemaForm<T> extends Form<T> {
validate(formData: any, schema: any) {
formData = removeOptionalEmptyValues(formData, this.props.schema);
//@ts-ignore
return super.validate(formData, schema);
}
}
|
Part of the problem here is that 1) when all the shipping address fields are set to be blank, shipping_address is set to
The following schema is not valid:
Whereas only the following schema is valid:
And the second problem is the same as mentioned above; even when the key |
Any updates with this? I am running into the same issue. |
Hello, any updates with this? I am running into the same issue. And i can't fix with this: #675 (comment) |
A workaround I went with: const formatInitialFormData = (
initialFormData: Record<string, unknown>,
schema: JSONSchema7,
): Record<string, unknown> => {
const properties = schema.properties!
const formData = { ...initialFormData }
Object.entries(properties).forEach((property) => {
const key = property[0]
const value = property[1] as JSONSchema7
formData[key] = initialFormData[key] ?? value?.default ?? undefined
})
return formData
} Consumed by: const [formData, setFormData] = useState(formatInitialFormData(initialFormData ?? {}, schema))
...
<Form
formData={formData}
...
/> It is not perfect for all use cases but does the trick for me. |
Try with the following : export class ReactJSONSchemaForm<T> extends Form<T> {
constructor(props: any){
super(props)
let oldValidate = this.validate;
this.validate = (
formData: T,
schema?: FormProps<T>['schema'],
additionalMetaSchemas?: FormProps<T>['additionalMetaSchemas'],
customFormats?: FormProps<T>['customFormats'],
): {errors: AjvError[], errorSchema: ErrorSchema} => {
let fixedFormData = trimEmptyValues(formData)
return oldValidate.call(this, fixedFormData, schema)
}
}
} |
Running into this issue as well |
Any updates with this? I am running into the same issue |
Till this gets fixed from the code side you can try making some changes to your schema if it fits your requirement. You can remove the
|
fixes: rjsf-team#675 by reverting the `allowEmptyObject` change on Form - In `@rjsf/utils`, reverted the `allowEmptyObject` change as follows: - Removed the `allowEmptyObject` changes made to `getDefaultFormState`, `computeDefault` and `maybeAddDefaultToObject` - Updated `maybeAddDefaultToObject` to take the list of required fields and allow adding an empty object if the field is required - Updated the `SchemaUtilsType` to revert the `allowEmptyObject` change - Updated the tests to remove testing `allowEmptyObject` and to add testing the `required` field state - In `@rjsf/core, removed the passing of `allowEmptyObject` to `getDefaultFormState` - Updated the tests for form to include the test case from rjsf-team#675 - In `utility-functions` documentation, removed the documentation of `allowEmptyObject` - Updated the `CHANGELOG.md` accordingly
fixes: #675 by reverting the `allowEmptyObject` change on Form - In `@rjsf/utils`, reverted the `allowEmptyObject` change as follows: - Removed the `allowEmptyObject` changes made to `getDefaultFormState`, `computeDefault` and `maybeAddDefaultToObject` - Updated `maybeAddDefaultToObject` to take the list of required fields and allow adding an empty object if the field is required - Updated the `SchemaUtilsType` to revert the `allowEmptyObject` change - Updated the tests to remove testing `allowEmptyObject` and to add testing the `required` field state - In `@rjsf/core, removed the passing of `allowEmptyObject` to `getDefaultFormState` - Updated the tests for form to include the test case from #675 - In `utility-functions` documentation, removed the documentation of `allowEmptyObject` - Updated the `CHANGELOG.md` accordingly
6 years later and it looks like this bug still exists. I have a large amount of complex schemas that can't be modified using "depencies" or other conditionals. It is expected behavior of a schema that objects properties are not required if the object itself isn't required. I am testing this with the following simply schema. Is there any fix for this that I am missing that doesn't involve changing the actual schemas?
|
@bluisana can you explain what is not working properly in detailed steps within this playground. As far as I can tell this issue has been fixed in 5.x |
Hello @heath-freenome, This works perfectly in the playground example but doesn't work in my application. After tons of experimenting I finally figured out what is going on. This functionality only works when you have noHtml5Validate = false. If you have noHtml5Validate set to true all inputs are required regardless of the required state of their parents. |
@bluisana So it seems we probably need to add a switch to enable/disable the noHtml5Validate in the playground and then we would see this issue show up? Please create a feature request for this. And also, maybe a bug for your use case |
I am facing the same issue |
@fdecollibus Are you using the latest 5.x release and set |
Prerequisites
Description
Given a json schema like the one below, the react-jsonschema-form validator essentially requires both shipping_address and billing_address even though the billing_address is not listed as required. This is because the address type requires all three of its properties. How can I make the billing_address optional?
Steps to Reproduce
Here is a link to the react-jsonschema-form playground.
Expected behavior
It seems that react-jsonschema-form should simply no submit billing_address if not all of its address properties are filled in.
Actual behavior
The form validator prevents the form from being submitted.
Version
0.49.0
The text was updated successfully, but these errors were encountered: