Skip to content

Commit

Permalink
Add test and update documentation for using anyOf inside array items (#…
Browse files Browse the repository at this point in the history
…1131)

* Add test and update documentation for using anyOf inside array items

Signed-off-by: Lucian <lucian.buzzo@gmail.com>
  • Loading branch information
LucianBuzzo authored and epicfaace committed Jan 21, 2019
1 parent ba833fa commit 251a274
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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]?
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
22 changes: 22 additions & 0 deletions playground/samples/anyOf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand Down
44 changes: 44 additions & 0 deletions test/anyOf_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});

0 comments on commit 251a274

Please sign in to comment.