Skip to content

Commit

Permalink
Reimplemented fix in rjsf-team#2198 in @rjsf/utils package
Browse files Browse the repository at this point in the history
- The fix in rjsf-team#2198 needed to be refactored into the new `@rjsf/utils` package
- Replicated the fix into the `getDefaultFormState.ts` file
- Replicated the additional tests into `getDefaultFormStateTest.ts` and `oneOf_test.js`
- Updated the `CHANGELOG.md` with the note for this fix along with the notes for PR rjsf-team#3094
  • Loading branch information
heath-freenome committed Sep 8, 2022
1 parent 71688ab commit c8e5389
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 8 deletions.
14 changes: 9 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,25 @@ it according to semantic versioning. For example, if your PR adds a breaking cha
should change the heading of the (upcoming) version to include a major version bump.
-->
# v5.0.0-beta.7

## @rjsf/core
- Enhancement for color widget allows users to look and feel interaction.

# v5.0.0-beta.6

## @rjsf/core
- Added tests for the new `@rjsf/validator-ajv8` to the `validate_test.js` file to ensure the validation works with both validator implementations

## @rjsf/mui
- Fixed the `README.md` to correct the package name in several places to match the actual package

## @rjsf/utils
- Fixed the `README.md` to remove references to ajv6 validator, adding link to the `utility-functions.md` in the docs
- Fixed the `README.md` to correct the package name in several places to match the actual package
- Updated `getDefaultFormState()` so that oneOf and anyOf default values do not always use the first option when formData contains a better option, fixing (https://github.com/rjsf-team/react-jsonschema-form/issues/2183)

## @rjsf/validator-ajv6
- Fixed the `README.md` to correct the package name in several places to match the actual package

## @rjsf/validator-ajv8
- Support for localization (L12n) on a customized validator using a `Localizer` function passed as a second parameter to `customizeValidator()`, fixing (https://github.com/rjsf-team/react-jsonschema-form/pull/846, and https://github.com/rjsf-team/react-jsonschema-form/issues/1195)
- Fixed the `README.md` to correct the package name in several places to match the actual package

## Dev / docs / playground
- Added two new validator selections, `AJV8` and `AJV8_es` to the list of available validators for the playground; Using the second one will translate error messages to spanish.
Expand Down
37 changes: 37 additions & 0 deletions packages/core/test/oneOf_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,43 @@ describe("oneOf", () => {
expect(node.querySelector("input#root").value).eql("");
});

it("should use only the selected option when generating default values", () => {
const schema = {
type: "object",
oneOf: [
{
additionalProperties: false,
properties: { lorem: { type: "object" } },
},
{
additionalProperties: false,
properties: { ipsum: { type: "object" } },
},
{
additionalProperties: false,
properties: { pyot: { type: "object" } },
},
],
};

const { node, onChange } = createFormComponent({
schema,
formData: { lorem: {} },
});

const $select = node.querySelector("select");

Simulate.change($select, {
target: { value: $select.options[1].value },
});

expect($select.value).eql("1");

sinon.assert.calledWithMatch(onChange.lastCall, {
formData: { lorem: undefined, ipsum: {} },
});
});

describe("Arrays", () => {
it("should correctly render mixed types for oneOf inside array items", () => {
const schema = {
Expand Down
7 changes: 4 additions & 3 deletions packages/utils/src/schema/getDefaultFormState.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import get from "lodash/get";
import isEmpty from "lodash/isEmpty";

import {
ANY_OF_KEY,
Expand Down Expand Up @@ -71,7 +72,7 @@ export function getInnerSchemaForArrayItem(
}

/** Computes the defaults for the current `schema` given the `rawFormData` and `parentDefaults` if any. This drills into
* the each level of the schema, recursively, to fill out every level of defaults provided by the schema.
* each level of the schema, recursively, to fill out every level of defaults provided by the schema.
*
* @param validator - an implementation of the `ValidatorType` interface that will be used when necessary
* @param schema - The schema for which the default state is desired
Expand Down Expand Up @@ -143,7 +144,7 @@ export function computeDefaults<T = any>(
schema = schema.oneOf![
getMatchingOption(
validator,
undefined,
isEmpty(formData) ? undefined : formData,
schema.oneOf as RJSFSchema[],
rootSchema
)
Expand All @@ -152,7 +153,7 @@ export function computeDefaults<T = any>(
schema = schema.anyOf![
getMatchingOption(
validator,
undefined,
isEmpty(formData) ? undefined : formData,
schema.anyOf as RJSFSchema[],
rootSchema
)
Expand Down
40 changes: 40 additions & 0 deletions packages/utils/test/schema/getDefaultFormStateTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,26 @@ export default function getDefaultFormStateTest(
grade: "A",
});
});
it("should populate defaults for oneOf second option", () => {
const schema: RJSFSchema = {
type: "object",
properties: {
test: {
oneOf: [
{ properties: { a: { type: "string", default: "a" } } },
{ properties: { b: { type: "string", default: "b" } } },
],
},
},
};
// Mock errors so that getMatchingOption works as expected
testValidator.setReturnValues({ isValid: [false, true] });
expect(
getDefaultFormState(testValidator, schema, { test: { b: "b" } })
).toEqual({
test: { b: "b" },
});
});
});
describe("defaults with anyOf", () => {
it("should populate defaults for anyOf", () => {
Expand Down Expand Up @@ -837,6 +857,26 @@ export default function getDefaultFormStateTest(
grade: "A",
});
});
it("should populate defaults for anyOf second option", () => {
const schema: RJSFSchema = {
type: "object",
properties: {
test: {
anyOf: [
{ properties: { a: { type: "string", default: "a" } } },
{ properties: { b: { type: "string", default: "b" } } },
],
},
},
};
// Mock errors so that getMatchingOption works as expected
testValidator.setReturnValues({ isValid: [false, true] });
expect(
getDefaultFormState(testValidator, schema, { test: { b: "b" } })
).toEqual({
test: { b: "b" },
});
});
});
describe("with dependencies", () => {
it("should populate defaults for dependencies", () => {
Expand Down

0 comments on commit c8e5389

Please sign in to comment.