Skip to content

Commit ff46301

Browse files
fix: Switch to using isValid() for oneOf schema detection in retrieveSchema() (#3575)
1 parent 8aeffcb commit ff46301

File tree

5 files changed

+37
-38
lines changed

5 files changed

+37
-38
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ should change the heading of the (upcoming) version to include a major version b
2020

2121
## @rjsf/utils
2222
- Added protections against infinite recursion of `$ref`s for the `toIdSchema()`, `toPathSchema()` and `getDefaultFormState()` functions, fixing [#3560](https://github.com/rjsf-team/react-jsonschema-form/issues/3560)
23-
- Updated `getDefaultFormState()` to handle object-based `additionalProperties` with defaults using `formData` in addition to values contained in a `default` object, fixing [#2593](https://github.com/rjsf-team/react-jsonschema-form/issues/2593)
23+
- Updated `getDefaultFormState()` to handle object-based `additionalProperties` with defaults using `formData` in addition to values contained in a `default` object, fixing [#2593](https://github.com/rjsf-team/react-jsonschema-form/issues/2593)
24+
- Updated internal helper `withExactlyOneSubschema()` inside of `retrieveSchema()` to use the `isValid()` function rather than `validateFormData()` when determining the one-of branch
2425

2526
## Dev / docs / playground
2627

packages/core/test/Form.test.jsx

-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,6 @@ describeRepeated('Form common', (createFormComponent) => {
209209

210210
it('should work with oneOf', function () {
211211
const schema = {
212-
$schema: 'http://json-schema.org/draft-06/schema#',
213212
type: 'object',
214213
properties: {
215214
connector: {

packages/utils/src/schema/retrieveSchema.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,7 @@ export function withExactlyOneSubschema<
375375
[dependencyKey]: conditionPropertySchema,
376376
},
377377
} as S;
378-
const { errors } = validator.validateFormData(formData, conditionSchema);
379-
return errors.length === 0;
378+
return validator.isValid(conditionSchema, formData, rootSchema);
380379
}
381380
return false;
382381
});

packages/utils/test/schema/getDefaultFormStateTest.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1185,13 +1185,13 @@ export default function getDefaultFormStateTest(testValidator: TestValidatorType
11851185
]);
11861186
});
11871187
it('should populate defaults for nested dependencies in arrays when matching enum values in oneOf', () => {
1188-
// Mock errors so that withExactlyOneSubschema works as expected
1188+
// Mock isValid so that withExactlyOneSubschema works as expected
11891189
testValidator.setReturnValues({
1190-
data: [
1191-
{ errors: [], errorSchema: {} }, // First oneOf... first === first
1192-
{ errors: [{ stack: 'error' }], errorSchema: {} }, // Second oneOf... second !== first
1193-
{ errors: [{ stack: 'error' }], errorSchema: {} }, // First oneOf... first !== second
1194-
{ errors: [], errorSchema: {} }, // Second oneOf... second === second
1190+
isValid: [
1191+
true, // First oneOf... first === first
1192+
false, // Second oneOf... second !== first
1193+
false, // First oneOf... first !== second
1194+
true, // Second oneOf... second === second
11951195
],
11961196
});
11971197
const schema: RJSFSchema = {

packages/utils/test/schema/retrieveSchemaTest.ts

+28-28
Original file line numberDiff line numberDiff line change
@@ -482,11 +482,11 @@ export default function retrieveSchemaTest(testValidator: TestValidatorType) {
482482

483483
describe('with $ref in oneOf', () => {
484484
it('should retrieve referenced schemas', () => {
485-
// Mock errors so that withExactlyOneSubschema works as expected
485+
// Mock isValid so that withExactlyOneSubschema works as expected
486486
testValidator.setReturnValues({
487-
data: [
488-
{ errors: [{ stack: 'error' }], errorSchema: {} }, // First oneOf... second !== first
489-
{ errors: [], errorSchema: {} }, // Second oneOf... second === second
487+
isValid: [
488+
false, // First oneOf... second !== first
489+
true, // Second oneOf... second === second
490490
],
491491
});
492492
const schema: RJSFSchema = {
@@ -568,11 +568,11 @@ export default function retrieveSchemaTest(testValidator: TestValidatorType) {
568568

569569
describe('true condition', () => {
570570
it('should add `first` properties given `first` data', () => {
571-
// Mock errors so that withExactlyOneSubschema works as expected
571+
// Mock isValid so that withExactlyOneSubschema works as expected
572572
testValidator.setReturnValues({
573-
data: [
574-
{ errors: [], errorSchema: {} }, // First dependency... first === first
575-
{ errors: [{ stack: 'error' }], errorSchema: {} }, // Second dependency... second !== first
573+
isValid: [
574+
true, // First dependency... first === first
575+
false, // Second dependency... second !== first
576576
],
577577
});
578578
const schema: RJSFSchema = {
@@ -611,11 +611,11 @@ export default function retrieveSchemaTest(testValidator: TestValidatorType) {
611611
});
612612

613613
it('should add `second` properties given `second` data', () => {
614-
// Mock errors so that withExactlyOneSubschema works as expected
614+
// Mock isValid so that withExactlyOneSubschema works as expected
615615
testValidator.setReturnValues({
616-
data: [
617-
{ errors: [{ stack: 'error' }], errorSchema: {} }, // First dependency... first !== second
618-
{ errors: [], errorSchema: {} }, // Second dependency... second === second
616+
isValid: [
617+
false, // First dependency... first !== second
618+
true, // Second dependency... second === second
619619
],
620620
});
621621
const schema: RJSFSchema = {
@@ -730,13 +730,13 @@ export default function retrieveSchemaTest(testValidator: TestValidatorType) {
730730
});
731731

732732
it('should not include nested dependencies that should be hidden', () => {
733-
// Mock errors so that withExactlyOneSubschema works as expected
733+
// Mock isValid so that withExactlyOneSubschema works as expected
734734
testValidator.setReturnValues({
735-
data: [
736-
{ errors: [{ stack: 'error' }], errorSchema: {} }, // employee_accounts oneOf ... - fail
737-
{ errors: [], errorSchema: {} }, // update_absences first oneOf... success
738-
{ errors: [{ stack: 'error' }], errorSchema: {} }, // update_absences second oneOf... fail
739-
{ errors: [{ stack: 'error' }], errorSchema: {} }, // update_absences third oneOf... fail
735+
isValid: [
736+
false, // employee_accounts oneOf ... - fail
737+
true, // update_absences first oneOf... success
738+
false, // update_absences second oneOf... fail
739+
false, // update_absences third oneOf... fail
740740
],
741741
});
742742
const formData = {
@@ -758,13 +758,13 @@ export default function retrieveSchemaTest(testValidator: TestValidatorType) {
758758
});
759759

760760
it('should include nested dependencies that should not be hidden', () => {
761-
// Mock errors so that withExactlyOneSubschema works as expected
761+
// Mock isValid so that withExactlyOneSubschema works as expected
762762
testValidator.setReturnValues({
763-
data: [
764-
{ errors: [], errorSchema: {} }, // employee_accounts oneOf... success
765-
{ errors: [], errorSchema: {} }, // update_absences first oneOf... success
766-
{ errors: [{ stack: 'error' }], errorSchema: {} }, // update_absences second oneOf... fail
767-
{ errors: [{ stack: 'error' }], errorSchema: {} }, // update_absences third oneOf... fail
763+
isValid: [
764+
true, // employee_accounts oneOf... success
765+
true, // update_absences first oneOf... success
766+
false, // update_absences second oneOf... fail
767+
false, // update_absences third oneOf... fail
768768
],
769769
});
770770
const formData = {
@@ -800,11 +800,11 @@ export default function retrieveSchemaTest(testValidator: TestValidatorType) {
800800

801801
describe('with $ref in dependency', () => {
802802
it('should retrieve the referenced schema', () => {
803-
// Mock errors so that withExactlyOneSubschema works as expected
803+
// Mock isValid so that withExactlyOneSubschema works as expected
804804
testValidator.setReturnValues({
805-
data: [
806-
{ errors: [{ stack: 'error' }], errorSchema: {} }, // First oneOf... fail
807-
{ errors: [], errorSchema: {} }, // Second oneOf... success
805+
isValid: [
806+
false, // First oneOf... fail
807+
true, // Second oneOf... success
808808
],
809809
});
810810
const schema: RJSFSchema = {

0 commit comments

Comments
 (0)