Skip to content

Add allOf support #1380

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

Merged
merged 20 commits into from
Dec 7, 2019
Merged
Show file tree
Hide file tree
Changes from 17 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
4 changes: 3 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,12 @@ This component follows [JSON Schema](http://json-schema.org/documentation.html)

* `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](dependencies.md#schema-dependencies) to dynamically add schema properties based on input data.

The `allOf` keyword is supported; it uses [json-schema-merge-allof](https://github.com/mokkabonna/json-schema-merge-allof) to merge subschemas to render the final combined schema in the form. When these subschemas are incompatible, though (or if the library has an error merging it), the `allOf` keyword is dropped from the schema.

* `"additionalProperties":false` produces incorrect schemas when used with [schema dependencies](#schema-dependencies). This library does not remove extra properties, which causes validation to fail. It is recommended to avoid setting `"additionalProperties":false` when you use schema dependencies. See [#848](https://github.com/mozilla-services/react-jsonschema-form/issues/848) [#902](https://github.com/mozilla-services/react-jsonschema-form/issues/902) [#992](https://github.com/mozilla-services/react-jsonschema-form/issues/992)

## Handling of schema defaults
Expand Down
76 changes: 74 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"@babel/runtime-corejs2": "^7.4.5",
"ajv": "^6.7.0",
"core-js": "^2.5.7",
"json-schema-merge-allof": "0.6.0",
"lodash": "^4.17.15",
"prop-types": "^15.7.2",
"react-is": "^16.9.0",
Expand Down
26 changes: 26 additions & 0 deletions playground/samples/allOf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module.exports = {
schema: {
type: "object",
allOf: [
{
properties: {
lorem: {
type: ["string", "number"],
},
},
},
{
properties: {
lorem: {
type: "boolean",
minLength: 5,
},
ipsum: {
type: "string",
},
},
},
],
},
formData: {},
};
2 changes: 2 additions & 0 deletions playground/samples/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import arrays from "./arrays";
import anyOf from "./anyOf";
import oneOf from "./oneOf";
import allOf from "./allOf";
import nested from "./nested";
import numbers from "./numbers";
import simple from "./simple";
Expand Down Expand Up @@ -48,6 +49,7 @@ export const samples = {
"Additional Properties": additionalProperties,
"Any Of": anyOf,
"One Of": oneOf,
"All Of": allOf,
"Null fields": nullField,
Nullable: nullable,
ErrorSchema: errorSchema,
Expand Down
25 changes: 22 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from "react";
import * as ReactIs from "react-is";
import mergeAllOf from "json-schema-merge-allof";
import fill from "core-js/library/fn/array/fill";
import validateFormData, { isValid } from "./validate";
import { union } from "lodash";
Expand Down Expand Up @@ -633,6 +634,13 @@ export function resolveSchema(schema, definitions = {}, formData = {}) {
} else if (schema.hasOwnProperty("dependencies")) {
const resolvedSchema = resolveDependencies(schema, definitions, formData);
return retrieveSchema(resolvedSchema, definitions, formData);
} else if (schema.hasOwnProperty("allOf")) {
return {
...schema,
allOf: schema.allOf.map(allOfSubschema =>
retrieveSchema(allOfSubschema, definitions, formData)
),
};
} else {
// No $ref or dependencies attribute found, returning the original schema.
return schema;
Expand All @@ -653,7 +661,18 @@ function resolveReference(schema, definitions, formData) {
}

export function retrieveSchema(schema, definitions = {}, formData = {}) {
const resolvedSchema = resolveSchema(schema, definitions, formData);
let resolvedSchema = resolveSchema(schema, definitions, formData);
if ("allOf" in schema) {
try {
resolvedSchema = mergeAllOf(
{ ...resolvedSchema, allOf: resolvedSchema.allOf }
);
} catch (e) {
console.warn("could not merge subschemas in allOf:\n" + e);
const { allOf, ...resolvedSchemaWithoutAllOf } = resolvedSchema;
return resolvedSchemaWithoutAllOf;
}
}
const hasAdditionalProperties =
resolvedSchema.hasOwnProperty("additionalProperties") &&
resolvedSchema.additionalProperties !== false;
Expand Down Expand Up @@ -943,7 +962,7 @@ export function toIdSchema(
const idSchema = {
$id: id || idPrefix,
};
if ("$ref" in schema || "dependencies" in schema) {
if ("$ref" in schema || "dependencies" in schema || "allOf" in schema) {
const _schema = retrieveSchema(schema, definitions, formData);
return toIdSchema(_schema, id, definitions, formData, idPrefix);
}
Expand Down Expand Up @@ -973,7 +992,7 @@ export function toPathSchema(schema, name = "", definitions, formData = {}) {
const pathSchema = {
$name: name.replace(/^\./, ""),
};
if ("$ref" in schema || "dependencies" in schema) {
if ("$ref" in schema || "dependencies" in schema || "allOf" in schema) {
const _schema = retrieveSchema(schema, definitions, formData);
return toPathSchema(_schema, name, definitions, formData);
}
Expand Down
49 changes: 49 additions & 0 deletions test/allOf_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { expect } from "chai";

import { createFormComponent, createSandbox } from "./test_utils";

describe("allOf", () => {
let sandbox;

beforeEach(() => {
sandbox = createSandbox();
});

afterEach(() => {
sandbox.restore();
});

it("should render a regular input element with a single type, when multiple types specified", () => {
const schema = {
type: "object",
properties: {
foo: {
allOf: [{ type: ["string", "number", "null"] }, { type: "string" }],
},
},
};

const { node } = createFormComponent({
schema,
});

expect(node.querySelectorAll("input")).to.have.length.of(1);
});

it("should be able to handle incompatible types and not crash", () => {
const schema = {
type: "object",
properties: {
foo: {
allOf: [{ type: "string" }, { type: "boolean" }],
},
},
};

const { node } = createFormComponent({
schema,
});

expect(node.querySelectorAll("input")).to.have.length.of(0);
});
});
Loading