@remoteoss/json-schema-form 0.10.1-beta.0 → 0.11.0-dev.20240725074901
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +201 -14
- package/dist/index.js +201 -14
- package/dist/standalone.js +371 -22
- package/package.json +1 -1
- package/src/tests/modify.test.js +477 -11
package/dist/index.cjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
|
|
2
2
|
/*!
|
|
3
3
|
Copyright (c) 2024 Remote Technology, Inc.
|
|
4
|
-
NPM Package: @remoteoss/json-schema-form@0.
|
|
5
|
-
Generated:
|
|
4
|
+
NPM Package: @remoteoss/json-schema-form@0.11.0-dev.20240725074901
|
|
5
|
+
Generated: Thu, 25 Jul 2024 07:49:16 GMT
|
|
6
6
|
|
|
7
7
|
MIT License
|
|
8
8
|
|
|
@@ -1834,11 +1834,24 @@ function createHeadlessForm(jsonSchema, customConfig = {}) {
|
|
|
1834
1834
|
}
|
|
1835
1835
|
|
|
1836
1836
|
// src/modify.js
|
|
1837
|
+
var import_difference = __toESM(require("lodash/difference"));
|
|
1837
1838
|
var import_get4 = __toESM(require("lodash/get"));
|
|
1839
|
+
var import_intersection = __toESM(require("lodash/intersection"));
|
|
1838
1840
|
var import_merge3 = __toESM(require("lodash/merge"));
|
|
1841
|
+
var import_mergeWith = __toESM(require("lodash/mergeWith"));
|
|
1842
|
+
var import_set2 = __toESM(require("lodash/set"));
|
|
1843
|
+
var WARNING_TYPES = {
|
|
1844
|
+
FIELD_TO_CHANGE_NOT_FOUND: "FIELD_TO_CHANGE_NOT_FOUND",
|
|
1845
|
+
ORDER_MISSING_FIELDS: "ORDER_MISSING_FIELDS",
|
|
1846
|
+
FIELD_TO_CREATE_EXISTS: "FIELD_TO_CREATE_EXISTS",
|
|
1847
|
+
PICK_MISSED_FIELD: "PICK_MISSED_FIELD"
|
|
1848
|
+
};
|
|
1839
1849
|
function shortToFullPath(path) {
|
|
1840
1850
|
return path.replace(".", ".properties.");
|
|
1841
1851
|
}
|
|
1852
|
+
function mergeReplaceArray(_, newVal) {
|
|
1853
|
+
return Array.isArray(newVal) ? newVal : void 0;
|
|
1854
|
+
}
|
|
1842
1855
|
function standardizeAttrs(attrs) {
|
|
1843
1856
|
const { errorMessage, properties, ...rest } = attrs;
|
|
1844
1857
|
return {
|
|
@@ -1846,25 +1859,52 @@ function standardizeAttrs(attrs) {
|
|
|
1846
1859
|
...errorMessage ? { "x-jsf-errorMessage": errorMessage } : {}
|
|
1847
1860
|
};
|
|
1848
1861
|
}
|
|
1862
|
+
function isConditionalReferencingAnyPickedField(condition, fieldsToPick) {
|
|
1863
|
+
const { if: ifCondition, then: thenCondition, else: elseCondition } = condition;
|
|
1864
|
+
const inIf = (0, import_intersection.default)(ifCondition.required, fieldsToPick);
|
|
1865
|
+
if (inIf.length > 0) {
|
|
1866
|
+
return true;
|
|
1867
|
+
}
|
|
1868
|
+
const inThen = (0, import_intersection.default)(thenCondition.required, fieldsToPick) || (0, import_intersection.default)(Object.keys(thenCondition.properties), fieldsToPick);
|
|
1869
|
+
if (inThen.length > 0) {
|
|
1870
|
+
return true;
|
|
1871
|
+
}
|
|
1872
|
+
const inElse = (0, import_intersection.default)(elseCondition.required, fieldsToPick) || (0, import_intersection.default)(Object.keys(elseCondition.properties), fieldsToPick);
|
|
1873
|
+
if (inElse.length > 0) {
|
|
1874
|
+
return true;
|
|
1875
|
+
}
|
|
1876
|
+
return false;
|
|
1877
|
+
}
|
|
1849
1878
|
function rewriteFields(schema, fieldsConfig) {
|
|
1850
1879
|
if (!fieldsConfig)
|
|
1851
|
-
return null;
|
|
1880
|
+
return { warnings: null };
|
|
1881
|
+
const warnings = [];
|
|
1852
1882
|
const fieldsToModify = Object.entries(fieldsConfig);
|
|
1853
1883
|
fieldsToModify.forEach(([shortPath, mutation]) => {
|
|
1854
1884
|
const fieldPath = shortToFullPath(shortPath);
|
|
1855
1885
|
if (!(0, import_get4.default)(schema.properties, fieldPath)) {
|
|
1886
|
+
warnings.push({
|
|
1887
|
+
type: WARNING_TYPES.FIELD_TO_CHANGE_NOT_FOUND,
|
|
1888
|
+
message: `Changing field "${shortPath}" was ignored because it does not exist.`
|
|
1889
|
+
});
|
|
1856
1890
|
return;
|
|
1857
1891
|
}
|
|
1858
1892
|
const fieldAttrs = (0, import_get4.default)(schema.properties, fieldPath);
|
|
1859
1893
|
const fieldChanges = typeof mutation === "function" ? mutation(fieldAttrs) : mutation;
|
|
1860
|
-
(0,
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1894
|
+
(0, import_mergeWith.default)(
|
|
1895
|
+
(0, import_get4.default)(schema.properties, fieldPath),
|
|
1896
|
+
{
|
|
1897
|
+
...fieldAttrs,
|
|
1898
|
+
...standardizeAttrs(fieldChanges)
|
|
1899
|
+
},
|
|
1900
|
+
mergeReplaceArray
|
|
1901
|
+
);
|
|
1864
1902
|
if (fieldChanges.properties) {
|
|
1865
|
-
rewriteFields((0, import_get4.default)(schema.properties, fieldPath), fieldChanges.properties);
|
|
1903
|
+
const result = rewriteFields((0, import_get4.default)(schema.properties, fieldPath), fieldChanges.properties);
|
|
1904
|
+
warnings.push(result.warnings);
|
|
1866
1905
|
}
|
|
1867
1906
|
});
|
|
1907
|
+
return { warnings: warnings.flat() };
|
|
1868
1908
|
}
|
|
1869
1909
|
function rewriteAllFields(schema, configCallback, context) {
|
|
1870
1910
|
if (!configCallback)
|
|
@@ -1872,10 +1912,14 @@ function rewriteAllFields(schema, configCallback, context) {
|
|
|
1872
1912
|
const parentName = context?.parent;
|
|
1873
1913
|
Object.entries(schema.properties).forEach(([fieldName, fieldAttrs]) => {
|
|
1874
1914
|
const fullName = parentName ? `${parentName}.${fieldName}` : fieldName;
|
|
1875
|
-
(0,
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1915
|
+
(0, import_mergeWith.default)(
|
|
1916
|
+
(0, import_get4.default)(schema.properties, fieldName),
|
|
1917
|
+
{
|
|
1918
|
+
...fieldAttrs,
|
|
1919
|
+
...configCallback(fullName, fieldAttrs)
|
|
1920
|
+
},
|
|
1921
|
+
mergeReplaceArray
|
|
1922
|
+
);
|
|
1879
1923
|
if (fieldAttrs.properties) {
|
|
1880
1924
|
rewriteAllFields(fieldAttrs, configCallback, {
|
|
1881
1925
|
parent: fieldName
|
|
@@ -1883,9 +1927,152 @@ function rewriteAllFields(schema, configCallback, context) {
|
|
|
1883
1927
|
}
|
|
1884
1928
|
});
|
|
1885
1929
|
}
|
|
1930
|
+
function reorderFields(schema, configOrder) {
|
|
1931
|
+
if (!configOrder)
|
|
1932
|
+
return { warnings: null };
|
|
1933
|
+
const warnings = [];
|
|
1934
|
+
const originalOrder = schema["x-jsf-order"] || [];
|
|
1935
|
+
const orderConfig = typeof configOrder === "function" ? configOrder(originalOrder) : configOrder;
|
|
1936
|
+
const remaining = (0, import_difference.default)(originalOrder, orderConfig);
|
|
1937
|
+
if (remaining.length > 0) {
|
|
1938
|
+
warnings.push({
|
|
1939
|
+
type: WARNING_TYPES.ORDER_MISSING_FIELDS,
|
|
1940
|
+
message: `Some fields got forgotten in the new order. They were automatically appended: ${remaining.join(
|
|
1941
|
+
", "
|
|
1942
|
+
)}`
|
|
1943
|
+
});
|
|
1944
|
+
}
|
|
1945
|
+
schema["x-jsf-order"] = [...orderConfig, ...remaining];
|
|
1946
|
+
return { warnings };
|
|
1947
|
+
}
|
|
1948
|
+
function createFields(schema, fieldsConfig) {
|
|
1949
|
+
if (!fieldsConfig)
|
|
1950
|
+
return { warnings: null };
|
|
1951
|
+
const warnings = [];
|
|
1952
|
+
const fieldsToCreate = Object.entries(fieldsConfig);
|
|
1953
|
+
fieldsToCreate.forEach(([shortPath, fieldAttrs]) => {
|
|
1954
|
+
const fieldPath = shortToFullPath(shortPath);
|
|
1955
|
+
if (fieldAttrs.properties) {
|
|
1956
|
+
const result = createFields((0, import_get4.default)(schema.properties, fieldPath), fieldAttrs.properties);
|
|
1957
|
+
warnings.push(result.warnings);
|
|
1958
|
+
}
|
|
1959
|
+
const fieldInSchema = (0, import_get4.default)(schema.properties, fieldPath);
|
|
1960
|
+
if (fieldInSchema) {
|
|
1961
|
+
warnings.push({
|
|
1962
|
+
type: WARNING_TYPES.FIELD_TO_CREATE_EXISTS,
|
|
1963
|
+
message: `Creating field "${shortPath}" was ignored because it already exists.`
|
|
1964
|
+
});
|
|
1965
|
+
return;
|
|
1966
|
+
}
|
|
1967
|
+
const fieldInObjectPath = (0, import_set2.default)({}, fieldPath, standardizeAttrs(fieldAttrs));
|
|
1968
|
+
(0, import_merge3.default)(schema.properties, fieldInObjectPath);
|
|
1969
|
+
});
|
|
1970
|
+
return { warnings: warnings.flat() };
|
|
1971
|
+
}
|
|
1972
|
+
function pickFields(originalSchema, fieldsToPick) {
|
|
1973
|
+
if (!fieldsToPick) {
|
|
1974
|
+
return { schema: originalSchema, warnings: null };
|
|
1975
|
+
}
|
|
1976
|
+
const newSchema = {
|
|
1977
|
+
properties: {}
|
|
1978
|
+
};
|
|
1979
|
+
Object.entries(originalSchema).forEach(([attrKey, attrValue]) => {
|
|
1980
|
+
switch (attrKey) {
|
|
1981
|
+
case "properties":
|
|
1982
|
+
fieldsToPick.forEach((fieldPath) => {
|
|
1983
|
+
(0, import_set2.default)(newSchema.properties, fieldPath, attrValue[fieldPath]);
|
|
1984
|
+
});
|
|
1985
|
+
break;
|
|
1986
|
+
case "x-jsf-order":
|
|
1987
|
+
case "required":
|
|
1988
|
+
newSchema[attrKey] = attrValue.filter((fieldName) => fieldsToPick.includes(fieldName));
|
|
1989
|
+
break;
|
|
1990
|
+
case "allOf": {
|
|
1991
|
+
const newAllOf = originalSchema.allOf.filter(
|
|
1992
|
+
(condition) => isConditionalReferencingAnyPickedField(condition, fieldsToPick)
|
|
1993
|
+
);
|
|
1994
|
+
newSchema[attrKey] = newAllOf;
|
|
1995
|
+
break;
|
|
1996
|
+
}
|
|
1997
|
+
default:
|
|
1998
|
+
newSchema[attrKey] = attrValue;
|
|
1999
|
+
}
|
|
2000
|
+
});
|
|
2001
|
+
let missingFields = {};
|
|
2002
|
+
newSchema.allOf.forEach((condition) => {
|
|
2003
|
+
const { if: ifCondition, then: thenCondition, else: elseCondition } = condition;
|
|
2004
|
+
const index = originalSchema.allOf.indexOf(condition);
|
|
2005
|
+
missingFields = {
|
|
2006
|
+
...missingFields,
|
|
2007
|
+
...findMissingFields(ifCondition, {
|
|
2008
|
+
fields: fieldsToPick,
|
|
2009
|
+
path: `allOf[${index}].if`
|
|
2010
|
+
}),
|
|
2011
|
+
...findMissingFields(thenCondition, {
|
|
2012
|
+
fields: fieldsToPick,
|
|
2013
|
+
path: `allOf[${index}].then`
|
|
2014
|
+
}),
|
|
2015
|
+
...findMissingFields(elseCondition, {
|
|
2016
|
+
fields: fieldsToPick,
|
|
2017
|
+
path: `allOf[${index}].else`
|
|
2018
|
+
})
|
|
2019
|
+
};
|
|
2020
|
+
});
|
|
2021
|
+
const warnings = [];
|
|
2022
|
+
if (Object.keys(missingFields).length > 0) {
|
|
2023
|
+
Object.entries(missingFields).forEach(([fieldName]) => {
|
|
2024
|
+
(0, import_set2.default)(newSchema.properties, fieldName, originalSchema.properties[fieldName]);
|
|
2025
|
+
});
|
|
2026
|
+
warnings.push({
|
|
2027
|
+
type: WARNING_TYPES.PICK_MISSED_FIELD,
|
|
2028
|
+
message: `The picked fields have related conditional fields that got added automatically. ${Object.keys(
|
|
2029
|
+
missingFields
|
|
2030
|
+
).join(", ")}. Check "meta" for more details.`,
|
|
2031
|
+
meta: missingFields
|
|
2032
|
+
});
|
|
2033
|
+
}
|
|
2034
|
+
return { schema: newSchema, warnings };
|
|
2035
|
+
}
|
|
2036
|
+
function findMissingFields(conditional, { fields, path }) {
|
|
2037
|
+
if (!conditional) {
|
|
2038
|
+
return null;
|
|
2039
|
+
}
|
|
2040
|
+
let missingFields = {};
|
|
2041
|
+
conditional.required?.forEach((fieldName) => {
|
|
2042
|
+
if (!fields.includes(fieldName)) {
|
|
2043
|
+
missingFields[fieldName] = {
|
|
2044
|
+
path
|
|
2045
|
+
};
|
|
2046
|
+
}
|
|
2047
|
+
});
|
|
2048
|
+
Object.entries(conditional.properties || []).forEach(([fieldName]) => {
|
|
2049
|
+
if (!fields.includes(fieldName)) {
|
|
2050
|
+
missingFields[fieldName] = { path };
|
|
2051
|
+
}
|
|
2052
|
+
});
|
|
2053
|
+
return missingFields;
|
|
2054
|
+
}
|
|
1886
2055
|
function modify(originalSchema, config) {
|
|
1887
2056
|
const schema = JSON.parse(JSON.stringify(originalSchema));
|
|
1888
|
-
rewriteFields(schema, config.fields);
|
|
2057
|
+
const resultRewrite = rewriteFields(schema, config.fields);
|
|
1889
2058
|
rewriteAllFields(schema, config.allFields);
|
|
1890
|
-
|
|
2059
|
+
const resultCreate = createFields(schema, config.create);
|
|
2060
|
+
const resultPick = pickFields(schema, config.pick);
|
|
2061
|
+
const finalSchema = resultPick.schema;
|
|
2062
|
+
const resultReorder = reorderFields(finalSchema, config.orderRoot);
|
|
2063
|
+
if (!config.muteLogging) {
|
|
2064
|
+
console.warn(
|
|
2065
|
+
"json-schema-form modify(): We highly recommend you to handle/report the returned `warnings` as they highlight possible bugs in your modifications. To mute this log, pass `muteLogging: true` to the config."
|
|
2066
|
+
);
|
|
2067
|
+
}
|
|
2068
|
+
const warnings = [
|
|
2069
|
+
resultRewrite.warnings,
|
|
2070
|
+
resultCreate.warnings,
|
|
2071
|
+
resultPick.warnings,
|
|
2072
|
+
resultReorder.warnings
|
|
2073
|
+
].flat().filter(Boolean);
|
|
2074
|
+
return {
|
|
2075
|
+
schema: finalSchema,
|
|
2076
|
+
warnings
|
|
2077
|
+
};
|
|
1891
2078
|
}
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
|
|
2
2
|
/*!
|
|
3
3
|
Copyright (c) 2024 Remote Technology, Inc.
|
|
4
|
-
NPM Package: @remoteoss/json-schema-form@0.
|
|
5
|
-
Generated:
|
|
4
|
+
NPM Package: @remoteoss/json-schema-form@0.11.0-dev.20240725074901
|
|
5
|
+
Generated: Thu, 25 Jul 2024 07:49:16 GMT
|
|
6
6
|
|
|
7
7
|
MIT License
|
|
8
8
|
|
|
@@ -1797,11 +1797,24 @@ function createHeadlessForm(jsonSchema, customConfig = {}) {
|
|
|
1797
1797
|
}
|
|
1798
1798
|
|
|
1799
1799
|
// src/modify.js
|
|
1800
|
+
import difference from "lodash/difference";
|
|
1800
1801
|
import get4 from "lodash/get";
|
|
1802
|
+
import intersection from "lodash/intersection";
|
|
1801
1803
|
import merge3 from "lodash/merge";
|
|
1804
|
+
import mergeWith from "lodash/mergeWith";
|
|
1805
|
+
import set2 from "lodash/set";
|
|
1806
|
+
var WARNING_TYPES = {
|
|
1807
|
+
FIELD_TO_CHANGE_NOT_FOUND: "FIELD_TO_CHANGE_NOT_FOUND",
|
|
1808
|
+
ORDER_MISSING_FIELDS: "ORDER_MISSING_FIELDS",
|
|
1809
|
+
FIELD_TO_CREATE_EXISTS: "FIELD_TO_CREATE_EXISTS",
|
|
1810
|
+
PICK_MISSED_FIELD: "PICK_MISSED_FIELD"
|
|
1811
|
+
};
|
|
1802
1812
|
function shortToFullPath(path) {
|
|
1803
1813
|
return path.replace(".", ".properties.");
|
|
1804
1814
|
}
|
|
1815
|
+
function mergeReplaceArray(_, newVal) {
|
|
1816
|
+
return Array.isArray(newVal) ? newVal : void 0;
|
|
1817
|
+
}
|
|
1805
1818
|
function standardizeAttrs(attrs) {
|
|
1806
1819
|
const { errorMessage, properties, ...rest } = attrs;
|
|
1807
1820
|
return {
|
|
@@ -1809,25 +1822,52 @@ function standardizeAttrs(attrs) {
|
|
|
1809
1822
|
...errorMessage ? { "x-jsf-errorMessage": errorMessage } : {}
|
|
1810
1823
|
};
|
|
1811
1824
|
}
|
|
1825
|
+
function isConditionalReferencingAnyPickedField(condition, fieldsToPick) {
|
|
1826
|
+
const { if: ifCondition, then: thenCondition, else: elseCondition } = condition;
|
|
1827
|
+
const inIf = intersection(ifCondition.required, fieldsToPick);
|
|
1828
|
+
if (inIf.length > 0) {
|
|
1829
|
+
return true;
|
|
1830
|
+
}
|
|
1831
|
+
const inThen = intersection(thenCondition.required, fieldsToPick) || intersection(Object.keys(thenCondition.properties), fieldsToPick);
|
|
1832
|
+
if (inThen.length > 0) {
|
|
1833
|
+
return true;
|
|
1834
|
+
}
|
|
1835
|
+
const inElse = intersection(elseCondition.required, fieldsToPick) || intersection(Object.keys(elseCondition.properties), fieldsToPick);
|
|
1836
|
+
if (inElse.length > 0) {
|
|
1837
|
+
return true;
|
|
1838
|
+
}
|
|
1839
|
+
return false;
|
|
1840
|
+
}
|
|
1812
1841
|
function rewriteFields(schema, fieldsConfig) {
|
|
1813
1842
|
if (!fieldsConfig)
|
|
1814
|
-
return null;
|
|
1843
|
+
return { warnings: null };
|
|
1844
|
+
const warnings = [];
|
|
1815
1845
|
const fieldsToModify = Object.entries(fieldsConfig);
|
|
1816
1846
|
fieldsToModify.forEach(([shortPath, mutation]) => {
|
|
1817
1847
|
const fieldPath = shortToFullPath(shortPath);
|
|
1818
1848
|
if (!get4(schema.properties, fieldPath)) {
|
|
1849
|
+
warnings.push({
|
|
1850
|
+
type: WARNING_TYPES.FIELD_TO_CHANGE_NOT_FOUND,
|
|
1851
|
+
message: `Changing field "${shortPath}" was ignored because it does not exist.`
|
|
1852
|
+
});
|
|
1819
1853
|
return;
|
|
1820
1854
|
}
|
|
1821
1855
|
const fieldAttrs = get4(schema.properties, fieldPath);
|
|
1822
1856
|
const fieldChanges = typeof mutation === "function" ? mutation(fieldAttrs) : mutation;
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1857
|
+
mergeWith(
|
|
1858
|
+
get4(schema.properties, fieldPath),
|
|
1859
|
+
{
|
|
1860
|
+
...fieldAttrs,
|
|
1861
|
+
...standardizeAttrs(fieldChanges)
|
|
1862
|
+
},
|
|
1863
|
+
mergeReplaceArray
|
|
1864
|
+
);
|
|
1827
1865
|
if (fieldChanges.properties) {
|
|
1828
|
-
rewriteFields(get4(schema.properties, fieldPath), fieldChanges.properties);
|
|
1866
|
+
const result = rewriteFields(get4(schema.properties, fieldPath), fieldChanges.properties);
|
|
1867
|
+
warnings.push(result.warnings);
|
|
1829
1868
|
}
|
|
1830
1869
|
});
|
|
1870
|
+
return { warnings: warnings.flat() };
|
|
1831
1871
|
}
|
|
1832
1872
|
function rewriteAllFields(schema, configCallback, context) {
|
|
1833
1873
|
if (!configCallback)
|
|
@@ -1835,10 +1875,14 @@ function rewriteAllFields(schema, configCallback, context) {
|
|
|
1835
1875
|
const parentName = context?.parent;
|
|
1836
1876
|
Object.entries(schema.properties).forEach(([fieldName, fieldAttrs]) => {
|
|
1837
1877
|
const fullName = parentName ? `${parentName}.${fieldName}` : fieldName;
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1878
|
+
mergeWith(
|
|
1879
|
+
get4(schema.properties, fieldName),
|
|
1880
|
+
{
|
|
1881
|
+
...fieldAttrs,
|
|
1882
|
+
...configCallback(fullName, fieldAttrs)
|
|
1883
|
+
},
|
|
1884
|
+
mergeReplaceArray
|
|
1885
|
+
);
|
|
1842
1886
|
if (fieldAttrs.properties) {
|
|
1843
1887
|
rewriteAllFields(fieldAttrs, configCallback, {
|
|
1844
1888
|
parent: fieldName
|
|
@@ -1846,11 +1890,154 @@ function rewriteAllFields(schema, configCallback, context) {
|
|
|
1846
1890
|
}
|
|
1847
1891
|
});
|
|
1848
1892
|
}
|
|
1893
|
+
function reorderFields(schema, configOrder) {
|
|
1894
|
+
if (!configOrder)
|
|
1895
|
+
return { warnings: null };
|
|
1896
|
+
const warnings = [];
|
|
1897
|
+
const originalOrder = schema["x-jsf-order"] || [];
|
|
1898
|
+
const orderConfig = typeof configOrder === "function" ? configOrder(originalOrder) : configOrder;
|
|
1899
|
+
const remaining = difference(originalOrder, orderConfig);
|
|
1900
|
+
if (remaining.length > 0) {
|
|
1901
|
+
warnings.push({
|
|
1902
|
+
type: WARNING_TYPES.ORDER_MISSING_FIELDS,
|
|
1903
|
+
message: `Some fields got forgotten in the new order. They were automatically appended: ${remaining.join(
|
|
1904
|
+
", "
|
|
1905
|
+
)}`
|
|
1906
|
+
});
|
|
1907
|
+
}
|
|
1908
|
+
schema["x-jsf-order"] = [...orderConfig, ...remaining];
|
|
1909
|
+
return { warnings };
|
|
1910
|
+
}
|
|
1911
|
+
function createFields(schema, fieldsConfig) {
|
|
1912
|
+
if (!fieldsConfig)
|
|
1913
|
+
return { warnings: null };
|
|
1914
|
+
const warnings = [];
|
|
1915
|
+
const fieldsToCreate = Object.entries(fieldsConfig);
|
|
1916
|
+
fieldsToCreate.forEach(([shortPath, fieldAttrs]) => {
|
|
1917
|
+
const fieldPath = shortToFullPath(shortPath);
|
|
1918
|
+
if (fieldAttrs.properties) {
|
|
1919
|
+
const result = createFields(get4(schema.properties, fieldPath), fieldAttrs.properties);
|
|
1920
|
+
warnings.push(result.warnings);
|
|
1921
|
+
}
|
|
1922
|
+
const fieldInSchema = get4(schema.properties, fieldPath);
|
|
1923
|
+
if (fieldInSchema) {
|
|
1924
|
+
warnings.push({
|
|
1925
|
+
type: WARNING_TYPES.FIELD_TO_CREATE_EXISTS,
|
|
1926
|
+
message: `Creating field "${shortPath}" was ignored because it already exists.`
|
|
1927
|
+
});
|
|
1928
|
+
return;
|
|
1929
|
+
}
|
|
1930
|
+
const fieldInObjectPath = set2({}, fieldPath, standardizeAttrs(fieldAttrs));
|
|
1931
|
+
merge3(schema.properties, fieldInObjectPath);
|
|
1932
|
+
});
|
|
1933
|
+
return { warnings: warnings.flat() };
|
|
1934
|
+
}
|
|
1935
|
+
function pickFields(originalSchema, fieldsToPick) {
|
|
1936
|
+
if (!fieldsToPick) {
|
|
1937
|
+
return { schema: originalSchema, warnings: null };
|
|
1938
|
+
}
|
|
1939
|
+
const newSchema = {
|
|
1940
|
+
properties: {}
|
|
1941
|
+
};
|
|
1942
|
+
Object.entries(originalSchema).forEach(([attrKey, attrValue]) => {
|
|
1943
|
+
switch (attrKey) {
|
|
1944
|
+
case "properties":
|
|
1945
|
+
fieldsToPick.forEach((fieldPath) => {
|
|
1946
|
+
set2(newSchema.properties, fieldPath, attrValue[fieldPath]);
|
|
1947
|
+
});
|
|
1948
|
+
break;
|
|
1949
|
+
case "x-jsf-order":
|
|
1950
|
+
case "required":
|
|
1951
|
+
newSchema[attrKey] = attrValue.filter((fieldName) => fieldsToPick.includes(fieldName));
|
|
1952
|
+
break;
|
|
1953
|
+
case "allOf": {
|
|
1954
|
+
const newAllOf = originalSchema.allOf.filter(
|
|
1955
|
+
(condition) => isConditionalReferencingAnyPickedField(condition, fieldsToPick)
|
|
1956
|
+
);
|
|
1957
|
+
newSchema[attrKey] = newAllOf;
|
|
1958
|
+
break;
|
|
1959
|
+
}
|
|
1960
|
+
default:
|
|
1961
|
+
newSchema[attrKey] = attrValue;
|
|
1962
|
+
}
|
|
1963
|
+
});
|
|
1964
|
+
let missingFields = {};
|
|
1965
|
+
newSchema.allOf.forEach((condition) => {
|
|
1966
|
+
const { if: ifCondition, then: thenCondition, else: elseCondition } = condition;
|
|
1967
|
+
const index = originalSchema.allOf.indexOf(condition);
|
|
1968
|
+
missingFields = {
|
|
1969
|
+
...missingFields,
|
|
1970
|
+
...findMissingFields(ifCondition, {
|
|
1971
|
+
fields: fieldsToPick,
|
|
1972
|
+
path: `allOf[${index}].if`
|
|
1973
|
+
}),
|
|
1974
|
+
...findMissingFields(thenCondition, {
|
|
1975
|
+
fields: fieldsToPick,
|
|
1976
|
+
path: `allOf[${index}].then`
|
|
1977
|
+
}),
|
|
1978
|
+
...findMissingFields(elseCondition, {
|
|
1979
|
+
fields: fieldsToPick,
|
|
1980
|
+
path: `allOf[${index}].else`
|
|
1981
|
+
})
|
|
1982
|
+
};
|
|
1983
|
+
});
|
|
1984
|
+
const warnings = [];
|
|
1985
|
+
if (Object.keys(missingFields).length > 0) {
|
|
1986
|
+
Object.entries(missingFields).forEach(([fieldName]) => {
|
|
1987
|
+
set2(newSchema.properties, fieldName, originalSchema.properties[fieldName]);
|
|
1988
|
+
});
|
|
1989
|
+
warnings.push({
|
|
1990
|
+
type: WARNING_TYPES.PICK_MISSED_FIELD,
|
|
1991
|
+
message: `The picked fields have related conditional fields that got added automatically. ${Object.keys(
|
|
1992
|
+
missingFields
|
|
1993
|
+
).join(", ")}. Check "meta" for more details.`,
|
|
1994
|
+
meta: missingFields
|
|
1995
|
+
});
|
|
1996
|
+
}
|
|
1997
|
+
return { schema: newSchema, warnings };
|
|
1998
|
+
}
|
|
1999
|
+
function findMissingFields(conditional, { fields, path }) {
|
|
2000
|
+
if (!conditional) {
|
|
2001
|
+
return null;
|
|
2002
|
+
}
|
|
2003
|
+
let missingFields = {};
|
|
2004
|
+
conditional.required?.forEach((fieldName) => {
|
|
2005
|
+
if (!fields.includes(fieldName)) {
|
|
2006
|
+
missingFields[fieldName] = {
|
|
2007
|
+
path
|
|
2008
|
+
};
|
|
2009
|
+
}
|
|
2010
|
+
});
|
|
2011
|
+
Object.entries(conditional.properties || []).forEach(([fieldName]) => {
|
|
2012
|
+
if (!fields.includes(fieldName)) {
|
|
2013
|
+
missingFields[fieldName] = { path };
|
|
2014
|
+
}
|
|
2015
|
+
});
|
|
2016
|
+
return missingFields;
|
|
2017
|
+
}
|
|
1849
2018
|
function modify(originalSchema, config) {
|
|
1850
2019
|
const schema = JSON.parse(JSON.stringify(originalSchema));
|
|
1851
|
-
rewriteFields(schema, config.fields);
|
|
2020
|
+
const resultRewrite = rewriteFields(schema, config.fields);
|
|
1852
2021
|
rewriteAllFields(schema, config.allFields);
|
|
1853
|
-
|
|
2022
|
+
const resultCreate = createFields(schema, config.create);
|
|
2023
|
+
const resultPick = pickFields(schema, config.pick);
|
|
2024
|
+
const finalSchema = resultPick.schema;
|
|
2025
|
+
const resultReorder = reorderFields(finalSchema, config.orderRoot);
|
|
2026
|
+
if (!config.muteLogging) {
|
|
2027
|
+
console.warn(
|
|
2028
|
+
"json-schema-form modify(): We highly recommend you to handle/report the returned `warnings` as they highlight possible bugs in your modifications. To mute this log, pass `muteLogging: true` to the config."
|
|
2029
|
+
);
|
|
2030
|
+
}
|
|
2031
|
+
const warnings = [
|
|
2032
|
+
resultRewrite.warnings,
|
|
2033
|
+
resultCreate.warnings,
|
|
2034
|
+
resultPick.warnings,
|
|
2035
|
+
resultReorder.warnings
|
|
2036
|
+
].flat().filter(Boolean);
|
|
2037
|
+
return {
|
|
2038
|
+
schema: finalSchema,
|
|
2039
|
+
warnings
|
|
2040
|
+
};
|
|
1854
2041
|
}
|
|
1855
2042
|
export {
|
|
1856
2043
|
buildCompleteYupSchema,
|