mirror of
https://github.com/zhigang1992/react-jsonschema-form.git
synced 2026-04-29 04:35:34 +08:00
Removed throwing error when there's an extraneous property in ui:order (#814)
* Removed throwing error when there's an extraneous property in ui:order * Adding tests form orderProperties() * Updated ObjectField tests to test extraneous properties handling in order list * fix: keep order warning
This commit is contained in:
committed by
Ashwin Ramaswami
parent
9104a71eb2
commit
3afb5f9fd3
16
src/utils.js
16
src/utils.js
@@ -290,28 +290,32 @@ export function orderProperties(properties, order) {
|
||||
? `properties '${arr.join("', '")}'`
|
||||
: `property '${arr[0]}'`;
|
||||
const propertyHash = arrayToHash(properties);
|
||||
const orderHash = arrayToHash(order);
|
||||
const extraneous = order.filter(prop => prop !== "*" && !propertyHash[prop]);
|
||||
if (extraneous.length) {
|
||||
throw new Error(
|
||||
console.warn(
|
||||
`uiSchema order list contains extraneous ${errorPropList(extraneous)}`
|
||||
);
|
||||
}
|
||||
const orderFiltered = order.filter(
|
||||
prop => prop === "*" || propertyHash[prop]
|
||||
);
|
||||
const orderHash = arrayToHash(orderFiltered);
|
||||
|
||||
const rest = properties.filter(prop => !orderHash[prop]);
|
||||
const restIndex = order.indexOf("*");
|
||||
const restIndex = orderFiltered.indexOf("*");
|
||||
if (restIndex === -1) {
|
||||
if (rest.length) {
|
||||
throw new Error(
|
||||
`uiSchema order list does not contain ${errorPropList(rest)}`
|
||||
);
|
||||
}
|
||||
return order;
|
||||
return orderFiltered;
|
||||
}
|
||||
if (restIndex !== order.lastIndexOf("*")) {
|
||||
if (restIndex !== orderFiltered.lastIndexOf("*")) {
|
||||
throw new Error("uiSchema order list contains more than one wildcard item");
|
||||
}
|
||||
|
||||
const complete = [...order];
|
||||
const complete = [...orderFiltered];
|
||||
complete.splice(restIndex, 1, ...rest);
|
||||
return complete;
|
||||
}
|
||||
|
||||
@@ -231,7 +231,7 @@ describe("ObjectField", () => {
|
||||
expect(labels).eql(["baz", "bar", "qux", "foo"]);
|
||||
});
|
||||
|
||||
it("should throw when order list contains an extraneous property", () => {
|
||||
it("should use provided order also if order list contains extraneous properties", () => {
|
||||
const { node } = createFormComponent({
|
||||
schema,
|
||||
uiSchema: {
|
||||
@@ -239,9 +239,12 @@ describe("ObjectField", () => {
|
||||
},
|
||||
});
|
||||
|
||||
expect(node.querySelector(".config-error").textContent).to.match(
|
||||
/contains extraneous properties 'wut\?', 'huh\?'/
|
||||
const labels = [].map.call(
|
||||
node.querySelectorAll(".field > label"),
|
||||
l => l.textContent
|
||||
);
|
||||
|
||||
expect(labels).eql(["baz", "qux", "bar", "foo"]);
|
||||
});
|
||||
|
||||
it("should throw when order list misses an existing property", () => {
|
||||
|
||||
@@ -2,6 +2,7 @@ import { expect } from "chai";
|
||||
|
||||
import {
|
||||
asNumber,
|
||||
orderProperties,
|
||||
dataURItoBlob,
|
||||
deepEquals,
|
||||
getDefaultFormState,
|
||||
@@ -343,6 +344,37 @@ describe("utils", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("orderProperties()", () => {
|
||||
it("should remove from order elements that are not in properties", () => {
|
||||
const properties = ["foo", "baz"];
|
||||
const order = ["foo", "bar", "baz", "qux"];
|
||||
expect(orderProperties(properties, order)).eql(["foo", "baz"]);
|
||||
});
|
||||
|
||||
it("should order properties according to the order", () => {
|
||||
const properties = ["bar", "foo"];
|
||||
const order = ["foo", "bar"];
|
||||
expect(orderProperties(properties, order)).eql(["foo", "bar"]);
|
||||
});
|
||||
|
||||
it("should replace * with properties that are absent in order", () => {
|
||||
const properties = ["foo", "bar", "baz"];
|
||||
const order = ["*", "foo"];
|
||||
expect(orderProperties(properties, order)).eql(["bar", "baz", "foo"]);
|
||||
});
|
||||
|
||||
it("should handle more complex ordering case correctly", () => {
|
||||
const properties = ["foo", "baz", "qux", "bar"];
|
||||
const order = ["quux", "foo", "*", "corge", "baz"];
|
||||
expect(orderProperties(properties, order)).eql([
|
||||
"foo",
|
||||
"qux",
|
||||
"bar",
|
||||
"baz",
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("isConstant", () => {
|
||||
it("should return false when neither enum nor const is defined", () => {
|
||||
const schema = {};
|
||||
|
||||
Reference in New Issue
Block a user