Skip to content

Commit

Permalink
fix: removed accidental test focusing, added assertion for validate r…
Browse files Browse the repository at this point in the history
…esult value to ensure it returns appropriate success/fail result (#3855)

Co-authored-by: Richard Ainger <rainger@squiz.net>
  • Loading branch information
raing3 and Richard Ainger authored Sep 8, 2023
1 parent b07eda3 commit 2c279de
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ should change the heading of the (upcoming) version to include a major version b
## @rjsf/core

- Updated `MultiSchemaField` to only merge top level required field fixing duplicate field and description.
- Fixed programmatic validation (`validateForm()`) removes previous errors if all data is now valid.

## @rjsf/chakra-ui

Expand Down
15 changes: 11 additions & 4 deletions packages/core/src/components/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -729,13 +729,14 @@ export default class Form<
*/
validateForm() {
const { extraErrors, extraErrorsBlockSubmit, focusOnFirstError, onError } = this.props;
const { formData } = this.state;
const { formData, errors: prevErrors } = this.state;
const schemaValidation = this.validate(formData);
let errors = schemaValidation.errors;
let errorSchema = schemaValidation.errorSchema;
const schemaValidationErrors = errors;
const schemaValidationErrorSchema = errorSchema;
if (errors.length > 0 || (extraErrors && extraErrorsBlockSubmit)) {
const hasError = errors.length > 0 || (extraErrors && extraErrorsBlockSubmit);
if (hasError) {
if (extraErrors) {
const merged = validationDataMerge(schemaValidation, extraErrors);
errorSchema = merged.errorSchema;
Expand Down Expand Up @@ -763,9 +764,15 @@ export default class Form<
}
}
);
return false;
} else if (prevErrors.length > 0) {
this.setState({
errors: [],
errorSchema: {},
schemaValidationErrors: [],
schemaValidationErrorSchema: {},
});
}
return true;
return !hasError;
}

/** Renders the `Form` fields inside the <form> | `tagName` or `_internalFormWrapper`, rendering any errors if
Expand Down
43 changes: 43 additions & 0 deletions packages/core/test/Form.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4182,4 +4182,47 @@ describe('Form omitExtraData and liveOmit', () => {
expect(checkbox.checked).to.eq(true);
});
});

describe('validateForm()', () => {
it('Should update state when data updated from invalid to valid', () => {
const ref = createRef();
const props = {
schema: {
type: 'object',
required: ['input'],
properties: {
input: {
type: 'string',
},
},
},
formData: {},
ref,
};
const { comp, node } = createFormComponent(props);

// trigger programmatic validation and make sure an error appears.
expect(ref.current.validateForm()).to.eql(false);
let errors = node.querySelectorAll('.error-detail');
expect(errors).to.have.lengthOf(1);
expect(errors[0].textContent).to.be.eql("must have required property 'input'");

// populate the input and simulate a re-render from the parent.
const textNode = node.querySelector('#root_input');
Simulate.change(textNode, {
target: { value: 'populated value' },
});
setProps(comp, { ...props, formData: { input: 'populated value' } });

// error should still be present.
errors = node.querySelectorAll('.error-detail');
expect(errors).to.have.lengthOf(1);
expect(errors[0].textContent).to.be.eql("must have required property 'input'");

// trigger programmatic validation again and make sure the error disappears.
expect(ref.current.validateForm()).to.eql(true);
errors = node.querySelectorAll('.error-detail');
expect(errors).to.have.lengthOf(0);
});
});
});

0 comments on commit 2c279de

Please sign in to comment.