From 251a274fe48e3421fa22d0ed67179c2d5cc82901 Mon Sep 17 00:00:00 2001 From: Lucian Buzzo Date: Mon, 21 Jan 2019 15:37:16 +0000 Subject: [PATCH] Add test and update documentation for using anyOf inside array items (#1131) * Add test and update documentation for using anyOf inside array items Signed-off-by: Lucian --- README.md | 5 +++-- docs/index.md | 2 +- playground/samples/anyOf.js | 22 +++++++++++++++++++ test/anyOf_test.js | 44 +++++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 13b28085f9..e5cc458b2a 100644 --- a/README.md +++ b/README.md @@ -1845,9 +1845,9 @@ This component follows [JSON Schema](http://json-schema.org/documentation.html) This keyword works when `items` is an array. `additionalItems: true` is not supported because there's no widget to represent an item of any type. In this case it will be treated as no additional items allowed. `additionalItems` being a valid schema is supported. -* `anyOf`, `allOf`, and `oneOf`, or multiple `types` (i.e. `"type": ["string", "array"]` +* `anyOf`, `allOf`, and `oneOf`, or multiple `types` (i.e. `"type": ["string", "array"]`) - The `anyOf` and `oneOf` keywords are supported, however, properties declared inside the `anyOf/oneOf` should not overlap with properties "outside" of the `anyOf/oneOf`. + The `anyOf` and `oneOf` keywords are supported, however, properties declared inside the `anyOf/oneOf` should not overlap with properties "outside" of the `anyOf/oneOf`. You can also use `oneOf` with [schema dependencies](#schema-dependencies) to dynamically add schema properties based on input data. @@ -1922,6 +1922,7 @@ $ git push --tags origin master ### Q: Does rjsf support `oneOf`, `anyOf`, multiple types in an array, etc.? A: The `anyOf` and `oneOf` keywords are supported, however, properties declared inside the `anyOf/oneOf` should not overlap with properties "outside" of the `anyOf/oneOf`. + There is also special cased where you can use `oneOf` in [schema dependencies](#schema-dependencies), If you'd like to help improve support for these keywords, see the following issues for inspiration [#329](https://github.com/mozilla-services/react-jsonschema-form/pull/329) or [#417](https://github.com/mozilla-services/react-jsonschema-form/pull/417). See also: [#52](https://github.com/mozilla-services/react-jsonschema-form/issues/52), [#151](https://github.com/mozilla-services/react-jsonschema-form/issues/151), [#171](https://github.com/mozilla-services/react-jsonschema-form/issues/171), [#200](https://github.com/mozilla-services/react-jsonschema-form/issues/200), [#282](https://github.com/mozilla-services/react-jsonschema-form/issues/282), [#302](https://github.com/mozilla-services/react-jsonschema-form/pull/302), [#330](https://github.com/mozilla-services/react-jsonschema-form/issues/330), [#430](https://github.com/mozilla-services/react-jsonschema-form/issues/430), [#522](https://github.com/mozilla-services/react-jsonschema-form/issues/522), [#538](https://github.com/mozilla-services/react-jsonschema-form/issues/538), [#551](https://github.com/mozilla-services/react-jsonschema-form/issues/551), [#552](https://github.com/mozilla-services/react-jsonschema-form/issues/552), or [#648](https://github.com/mozilla-services/react-jsonschema-form/issues/648). ### Q: Will react-jsonschema-form support Material, Ant-Design, Foundation, or [some other specific widget library or frontend style]? diff --git a/docs/index.md b/docs/index.md index 91d6c61de2..6f8e489a13 100644 --- a/docs/index.md +++ b/docs/index.md @@ -1743,7 +1743,7 @@ This component follows [JSON Schema](http://json-schema.org/documentation.html) This keyword works when `items` is an array. `additionalItems: true` is not supported because there's no widget to represent an item of any type. In this case it will be treated as no additional items allowed. `additionalItems` being a valid schema is supported. -* `anyOf`, `allOf`, and `oneOf`, or multiple `types` (i.e. `"type": ["string", "array"]` +* `anyOf`, `allOf`, and `oneOf`, or multiple `types` (i.e. `"type": ["string", "array"]`) The `anyOf` and `oneOf` keywords are supported, however, properties declared inside the `anyOf/oneOf` should not overlap with properties "outside" of the `anyOf/oneOf`. diff --git a/playground/samples/anyOf.js b/playground/samples/anyOf.js index 432b0e0be1..8ff87f8523 100644 --- a/playground/samples/anyOf.js +++ b/playground/samples/anyOf.js @@ -6,6 +6,28 @@ module.exports = { type: "integer", title: "Age", }, + items: { + type: "array", + items: { + type: "object", + anyOf: [ + { + properties: { + foo: { + type: "string", + }, + }, + }, + { + properties: { + bar: { + type: "string", + }, + }, + }, + ], + }, + }, }, anyOf: [ { diff --git a/test/anyOf_test.js b/test/anyOf_test.js index ed5e67475b..cc845af6f7 100644 --- a/test/anyOf_test.js +++ b/test/anyOf_test.js @@ -299,4 +299,48 @@ describe("anyOf", () => { expect(node.querySelector("select").value).eql("1"); }); + + describe("Arrays", () => { + it("should correctly render form inputs for anyOf inside array items", () => { + const schema = { + type: "object", + properties: { + items: { + type: "array", + items: { + type: "object", + anyOf: [ + { + properties: { + foo: { + type: "string", + }, + }, + }, + { + properties: { + bar: { + type: "string", + }, + }, + }, + ], + }, + }, + }, + }; + + const { node } = createFormComponent({ + schema, + }); + + expect(node.querySelector(".array-item-add button")).not.eql(null); + + Simulate.click(node.querySelector(".array-item-add button")); + + expect(node.querySelectorAll("select")).to.have.length.of(1); + + expect(node.querySelectorAll("input#root_foo")).to.have.length.of(1); + }); + }); });