Skip to content

Commit d719874

Browse files
Merge branch 'main' into fix_multiupload
2 parents 683d5b8 + d7bd130 commit d719874

File tree

6 files changed

+71
-4
lines changed

6 files changed

+71
-4
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ should change the heading of the (upcoming) version to include a major version b
2121
## @rjsf/core
2222

2323
- Fix an issue where only the first file was uploaded when users selected multiple files for upload.
24+
- Fixed validation regression Form not revalidating after formData change, fixing [#4343](https://github.com/rjsf-team/react-jsonschema-form/issues/4343)
25+
26+
# 5.22.1
27+
28+
## Dev / docs / playground
29+
30+
- Bumped peer dependencies to 5.22.x due to updated type definition and API changes in @rjsf/utils
2431

2532
# 5.22.0
2633

packages/core/src/components/Form.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,9 +441,9 @@ export default class Form<
441441
if (mustValidate) {
442442
const schemaValidation = this.validate(formData, schema, schemaUtils, _retrievedSchema);
443443
errors = schemaValidation.errors;
444-
// If the schema has changed, we do not merge state.errorSchema.
444+
// If retrievedSchema is undefined which means the schema or formData has changed, we do not merge state.
445445
// Else in the case where it hasn't changed, we merge 'state.errorSchema' with 'schemaValidation.errorSchema.' This done to display the raised field error.
446-
if (isSchemaChanged) {
446+
if (retrievedSchema === undefined) {
447447
errorSchema = schemaValidation.errorSchema;
448448
} else {
449449
errorSchema = mergeObjects(

packages/core/test/ArrayField.test.jsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3254,6 +3254,27 @@ describe('ArrayField', () => {
32543254
});
32553255
});
32563256

3257+
it('Check that when formData changes, the form should re-validate', () => {
3258+
const { node, rerender } = createFormComponent({
3259+
schema,
3260+
formData: [
3261+
{
3262+
text: null,
3263+
},
3264+
],
3265+
liveValidate: true,
3266+
});
3267+
3268+
const errorMessages = node.querySelectorAll('#root_0_text__error');
3269+
expect(errorMessages).to.have.length(1);
3270+
const errorMessageContent = node.querySelector('#root_0_text__error .text-danger').textContent;
3271+
expect(errorMessageContent).to.contain('must be string');
3272+
3273+
rerender({ schema, formData: [{ text: 'test' }], liveValidate: true });
3274+
3275+
expect(node.querySelectorAll('#root_0_text__error')).to.have.length(0);
3276+
});
3277+
32573278
it('raise an error and check if the error is displayed', () => {
32583279
const { node } = createFormComponent({
32593280
schema,

packages/core/test/ObjectField.test.jsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,25 @@ describe('ObjectField', () => {
227227
});
228228
});
229229

230+
it('Check that when formData changes, the form should re-validate', () => {
231+
const { node, rerender } = createFormComponent({
232+
schema,
233+
formData: {
234+
foo: null,
235+
},
236+
liveValidate: true,
237+
});
238+
239+
const errorMessages = node.querySelectorAll('#root_foo__error');
240+
expect(errorMessages).to.have.length(1);
241+
const errorMessageContent = node.querySelector('#root_foo__error .text-danger').textContent;
242+
expect(errorMessageContent).to.contain('must be string');
243+
244+
rerender({ schema, formData: { foo: 'test' }, liveValidate: true });
245+
246+
expect(node.querySelectorAll('#root_foo__error')).to.have.length(0);
247+
});
248+
230249
it('raise an error and check if the error is displayed', () => {
231250
const { node } = createFormComponent({
232251
schema,

packages/core/test/StringField.test.jsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,23 @@ describe('StringField', () => {
299299
expect(node.querySelector('input').getAttribute('autocomplete')).eql('family-name');
300300
});
301301

302+
it('Check that when formData changes, the form should re-validate', () => {
303+
const { node, rerender } = createFormComponent({
304+
schema: { type: 'string' },
305+
formData: null,
306+
liveValidate: true,
307+
});
308+
309+
const errorMessages = node.querySelectorAll('#root__error');
310+
expect(errorMessages).to.have.length(1);
311+
const errorMessageContent = node.querySelector('#root__error .text-danger').textContent;
312+
expect(errorMessageContent).to.contain('must be string');
313+
314+
rerender({ schema: { type: 'string' }, formData: 'hello', liveValidate: true });
315+
316+
expect(node.querySelectorAll('#root__error')).to.have.length(0);
317+
});
318+
302319
it('raise an error and check if the error is displayed', () => {
303320
const { node } = createFormComponent({
304321
schema: { type: 'string' },

packages/core/test/test_utils.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@ export function createComponent(Component, props) {
1414
const onError = sinon.spy();
1515
const onSubmit = sinon.spy();
1616
const comp = <Component onSubmit={onSubmit} onError={onError} onChange={onChange} {...props} />;
17-
const { container } = render(comp);
17+
const { container, rerender } = render(comp);
18+
19+
const rerenderFunction = (props) =>
20+
rerender(<Component onSubmit={onSubmit} onError={onError} onChange={onChange} {...props} />);
1821
const node = findDOMNode(container).firstElementChild;
1922

20-
return { comp, node, onChange, onError, onSubmit };
23+
return { comp, node, onChange, onError, onSubmit, rerender: rerenderFunction };
2124
}
2225

2326
export function createFormComponent(props) {

0 commit comments

Comments
 (0)