mirror of
https://github.com/vxcontrol/ncform.git
synced 2026-06-30 22:17:58 -04:00
chore: added combining objects in json schema via allOf keys
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
import _get from "lodash-es/get";
|
||||
import _map from "lodash-es/map";
|
||||
import _uniq from "lodash-es/uniq";
|
||||
import _isNil from "lodash-es/isnil";
|
||||
import _kebabCase from "lodash-es/kebabCase";
|
||||
import _mergeWith from "lodash-es/mergewith";
|
||||
import extend from "extend";
|
||||
|
||||
const ncformUtils = {
|
||||
@@ -169,6 +172,17 @@ const ncformUtils = {
|
||||
return newFieldVal;
|
||||
}
|
||||
|
||||
function mergeSchemas(jsonSchemas) {
|
||||
return _mergeWith({}, ...jsonSchemas, (mergedValue, newValue, key) => {
|
||||
if (_isNil(mergedValue)) {
|
||||
return;
|
||||
}
|
||||
if (Array.isArray(mergedValue)) {
|
||||
return _uniq(mergedValue.concat(newValue)).sort();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function fixReference(root, obj = root) {
|
||||
function getValue(p) {
|
||||
let value = {};
|
||||
@@ -189,18 +203,11 @@ const ncformUtils = {
|
||||
}
|
||||
else {
|
||||
if (property === "$ref" && typeof obj[property] === "string") {
|
||||
Object.entries(getValue(obj[property]))
|
||||
Object.entries(mergeSchemas([getValue(obj[property]), obj]))
|
||||
.forEach(([key, value]) => {
|
||||
if (typeof obj[key] === "object" && !Array.isArray(obj[key]) &&
|
||||
typeof value === "object" && !Array.isArray(value)) {
|
||||
obj[key] = {...obj[key], ...value};
|
||||
} else if (Array.isArray(obj[key]) && Array.isArray(value)) {
|
||||
// obj[key] = obj[key].concat(value).unique();
|
||||
obj[key] = [...new Set([...obj[key], ...value])];
|
||||
} else {
|
||||
obj[key] = value;
|
||||
}
|
||||
});
|
||||
);
|
||||
delete obj["$ref"];
|
||||
fixReference(root, obj);
|
||||
break;
|
||||
@@ -212,7 +219,31 @@ const ncformUtils = {
|
||||
return obj;
|
||||
}
|
||||
|
||||
return completionField("$root", fixReference(returnSchema));
|
||||
function fixCombining(root, obj = root) {
|
||||
for (let property in obj) {
|
||||
if (obj.hasOwnProperty(property)) {
|
||||
if (property !== "allOf" && typeof obj[property] === "object") {
|
||||
fixCombining(root, obj[property]);
|
||||
}
|
||||
else {
|
||||
if (property === "allOf" && Array.isArray(obj[property])) {
|
||||
fixCombining(root, obj[property]);
|
||||
Object.entries(mergeSchemas(obj[property]))
|
||||
.forEach(([key, value]) => {
|
||||
obj[key] = value;
|
||||
}
|
||||
);
|
||||
delete obj["allOf"];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
return completionField("$root", fixCombining(fixReference(returnSchema)));
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
@@ -176,6 +176,58 @@ describe('/src/ncform-utils.js', () => {
|
||||
assert.equal(newFormSchema.properties.arr.ui.showLegend, false);
|
||||
assert.deepEqual(newFormSchema.properties.arr.value, [123, 234, 345, 456]);
|
||||
});
|
||||
it("perfectFormSchema - Combining in schema (allOf)", () => {
|
||||
const formSchema = {
|
||||
type: 'object',
|
||||
properties: {
|
||||
name: {
|
||||
allOf: [{
|
||||
type: 'string',
|
||||
ui: {
|
||||
showLabel: false
|
||||
}
|
||||
}, {
|
||||
ui: {
|
||||
label: 'my_name',
|
||||
noLabelSpace: true
|
||||
}
|
||||
}]
|
||||
},
|
||||
obj: {
|
||||
allOf: [{
|
||||
type: 'object'
|
||||
}, {
|
||||
}]
|
||||
},
|
||||
arr: {
|
||||
allOf: [{
|
||||
type: 'array',
|
||||
value: [123, 234, 345]
|
||||
}, {
|
||||
value: [345, 456]
|
||||
}]
|
||||
}
|
||||
}
|
||||
};
|
||||
const newFormSchema = ncformUtils.perfectFormSchema(formSchema);
|
||||
assert.equal(newFormSchema.ui.showLabel, true);
|
||||
assert.equal(newFormSchema.ui.showIdxLabel, false);
|
||||
assert.equal(newFormSchema.ui.showLegend, true);
|
||||
assert.equal(newFormSchema.ui.noLabelSpace, false);
|
||||
assert.equal(newFormSchema.properties.name.ui.label, 'my_name');
|
||||
assert.equal(newFormSchema.properties.name.ui.showLabel, false);
|
||||
assert.equal(newFormSchema.properties.name.ui.showIdxLabel, false);
|
||||
assert.equal(newFormSchema.properties.name.ui.noLabelSpace, true);
|
||||
assert.equal(newFormSchema.properties.obj.ui.label, 'obj');
|
||||
assert.equal(newFormSchema.properties.obj.ui.showLabel, true);
|
||||
assert.equal(newFormSchema.properties.obj.ui.showIdxLabel, false);
|
||||
assert.equal(newFormSchema.properties.obj.ui.showLegend, false);
|
||||
assert.equal(newFormSchema.properties.arr.ui.label, 'arr');
|
||||
assert.equal(newFormSchema.properties.arr.ui.showLabel, true);
|
||||
assert.equal(newFormSchema.properties.arr.ui.showIdxLabel, true);
|
||||
assert.equal(newFormSchema.properties.arr.ui.showLegend, false);
|
||||
assert.deepEqual(newFormSchema.properties.arr.value, [123, 234, 345, 456]);
|
||||
});
|
||||
|
||||
// --- getModelFromSchema
|
||||
|
||||
@@ -569,7 +621,7 @@ describe('/src/ncform-utils.js', () => {
|
||||
};
|
||||
ncformUtils.setValueToSchema(value, formSchema);
|
||||
assert(formSchema.properties.name.properties.firstname.value === '');
|
||||
assert(formSchema.properties.name.properties.age.value === undefined);
|
||||
assert(formSchema.properties.name.properties.age.value === '');
|
||||
assert(formSchema.properties.name.properties.marry.value === false);
|
||||
assert(
|
||||
JSON.stringify(formSchema.properties.name.properties.info.value) ===
|
||||
@@ -1058,10 +1110,10 @@ describe('/src/ncform-utils.js', () => {
|
||||
assert(stringDefVal === '');
|
||||
|
||||
const numberDefVal = ncformUtils.getDefVal('number');
|
||||
assert(numberDefVal === undefined);
|
||||
assert(numberDefVal === '');
|
||||
|
||||
const integerDefVal = ncformUtils.getDefVal('integer');
|
||||
assert(integerDefVal === undefined);
|
||||
assert(integerDefVal === '');
|
||||
|
||||
const booleanDefVal = ncformUtils.getDefVal('boolean');
|
||||
assert(booleanDefVal === false);
|
||||
|
||||
Reference in New Issue
Block a user