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

fix: rollback isValid() in retrieveSchema() to validateFormData() #4418

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ should change the heading of the (upcoming) version to include a major version b
## @rjsf/utils

- Fixed documentation for `getChangedFields()`
- Rollback `isValid()` in `retrieveSchema()` to `validateFormData()` to prevent a bug with empty enums blocking dependencies ([#4418](https://github.com/rjsf-team/react-jsonschema-form/pull/4418)). Fixes [#4357](https://github.com/rjsf-team/react-jsonschema-form/issues/4357)

## Dev / docs / playground

Expand Down
3 changes: 2 additions & 1 deletion packages/utils/src/schema/retrieveSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,8 @@ export function withExactlyOneSubschema<
[dependencyKey]: conditionPropertySchema,
},
} as S;
return validator.isValid(conditionSchema, formData, rootSchema) || expandAllBranches;
const { errors } = validator.validateFormData(formData, conditionSchema);
return errors.length === 0 || expandAllBranches;
}
return false;
});
Expand Down
12 changes: 6 additions & 6 deletions packages/utils/test/schema/getDefaultFormStateTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4431,13 +4431,13 @@ export default function getDefaultFormStateTest(testValidator: TestValidatorType
]);
});
it('should populate defaults for nested dependencies in arrays when matching enum values in oneOf', () => {
// Mock isValid so that withExactlyOneSubschema works as expected
// Mock errors so that withExactlyOneSubschema works as expected
testValidator.setReturnValues({
isValid: [
true, // First oneOf... first === first
false, // Second oneOf... second !== first
false, // First oneOf... first !== second
true, // Second oneOf... second === second
data: [
{ errors: [], errorSchema: {} }, // First oneOf... first === first
{ errors: [{ stack: 'error' }], errorSchema: {} }, // Second oneOf... second !== first
{ errors: [{ stack: 'error' }], errorSchema: {} }, // First oneOf... first !== second
{ errors: [], errorSchema: {} }, // Second oneOf... second === second
],
});
const schema: RJSFSchema = {
Expand Down
56 changes: 28 additions & 28 deletions packages/utils/test/schema/retrieveSchemaTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,11 +508,11 @@ export default function retrieveSchemaTest(testValidator: TestValidatorType) {

describe('with $ref in oneOf', () => {
it('should retrieve referenced schemas', () => {
// Mock isValid so that withExactlyOneSubschema works as expected
// Mock errors so that withExactlyOneSubschema works as expected
testValidator.setReturnValues({
isValid: [
false, // First oneOf... second !== first
true, // Second oneOf... second === second
data: [
{ errors: [{ stack: 'error' }], errorSchema: {} }, // First oneOf... second !== first
{ errors: [], errorSchema: {} }, // Second oneOf... second === second
],
});
const schema: RJSFSchema = {
Expand Down Expand Up @@ -576,11 +576,11 @@ export default function retrieveSchemaTest(testValidator: TestValidatorType) {

describe('true condition', () => {
it('should add `first` properties given `first` data', () => {
// Mock isValid so that withExactlyOneSubschema works as expected
// Mock errors so that withExactlyOneSubschema works as expected
testValidator.setReturnValues({
isValid: [
true, // First dependency... first === first
false, // Second dependency... second !== first
data: [
{ errors: [], errorSchema: {} }, // First dependency... first === first
{ errors: [{ stack: 'error' }], errorSchema: {} }, // Second dependency... second !== first
],
});
const schema: RJSFSchema = {
Expand All @@ -598,11 +598,11 @@ export default function retrieveSchemaTest(testValidator: TestValidatorType) {
});

it('should add `second` properties given `second` data', () => {
// Mock isValid so that withExactlyOneSubschema works as expected
// Mock errors so that withExactlyOneSubschema works as expected
testValidator.setReturnValues({
isValid: [
false, // First dependency... first !== second
true, // Second dependency... second === second
data: [
{ errors: [{ stack: 'error' }], errorSchema: {} }, // First dependency... first !== second
{ errors: [], errorSchema: {} }, // Second dependency... second === second
],
});
const schema: RJSFSchema = {
Expand All @@ -628,13 +628,13 @@ export default function retrieveSchemaTest(testValidator: TestValidatorType) {
});

it('should not include nested dependencies that should be hidden', () => {
// Mock isValid so that withExactlyOneSubschema works as expected
// Mock errors so that withExactlyOneSubschema works as expected
testValidator.setReturnValues({
isValid: [
false, // employee_accounts oneOf ... - fail
true, // update_absences first oneOf... success
false, // update_absences second oneOf... fail
false, // update_absences third oneOf... fail
data: [
{ errors: [{ stack: 'error' }], errorSchema: {} }, // employee_accounts oneOf ... - fail
{ errors: [], errorSchema: {} }, // update_absences first oneOf... success
{ errors: [{ stack: 'error' }], errorSchema: {} }, // update_absences second oneOf... fail
{ errors: [{ stack: 'error' }], errorSchema: {} }, // update_absences third oneOf... fail
],
});
const formData = {
Expand All @@ -656,13 +656,13 @@ export default function retrieveSchemaTest(testValidator: TestValidatorType) {
});

it('should include nested dependencies that should not be hidden', () => {
// Mock isValid so that withExactlyOneSubschema works as expected
// Mock errors so that withExactlyOneSubschema works as expected
testValidator.setReturnValues({
isValid: [
true, // employee_accounts oneOf... success
true, // update_absences first oneOf... success
false, // update_absences second oneOf... fail
false, // update_absences third oneOf... fail
data: [
{ errors: [], errorSchema: {} }, // employee_accounts oneOf... success
{ errors: [], errorSchema: {} }, // update_absences first oneOf... success
{ errors: [{ stack: 'error' }], errorSchema: {} }, // update_absences second oneOf... fail
{ errors: [{ stack: 'error' }], errorSchema: {} }, // update_absences third oneOf... fail
],
});
const formData = {
Expand Down Expand Up @@ -698,11 +698,11 @@ export default function retrieveSchemaTest(testValidator: TestValidatorType) {

describe('with $ref in dependency', () => {
it('should retrieve the referenced schema', () => {
// Mock isValid so that withExactlyOneSubschema works as expected
// Mock errors so that withExactlyOneSubschema works as expected
testValidator.setReturnValues({
isValid: [
false, // First oneOf... fail
true, // Second oneOf... success
data: [
{ errors: [{ stack: 'error' }], errorSchema: {} }, // First oneOf... fail
{ errors: [], errorSchema: {} }, // Second oneOf... success
],
});
const schema: RJSFSchema = {
Expand Down
Loading