@rjsf/utils 5.0.1 → 5.1.0
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/README.md +3 -3
- package/dist/index.d.ts +14 -4
- package/dist/utils.cjs.development.js +60 -21
- package/dist/utils.cjs.development.js.map +1 -1
- package/dist/utils.cjs.production.min.js +1 -1
- package/dist/utils.cjs.production.min.js.map +1 -1
- package/dist/utils.esm.js +60 -21
- package/dist/utils.esm.js.map +1 -1
- package/dist/utils.umd.development.js +60 -21
- package/dist/utils.umd.development.js.map +1 -1
- package/dist/utils.umd.production.min.js +1 -1
- package/dist/utils.umd.production.min.js.map +1 -1
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
<p align="center">
|
|
17
17
|
Utility functions for <a href="https://github.com/rjsf-team/react-jsonschema-form/"><code>react-jsonschema-form</code></a>.
|
|
18
18
|
<br />
|
|
19
|
-
<a href="https://react-jsonschema-form
|
|
19
|
+
<a href="https://rjsf-team.github.io/react-jsonschema-form/docs/"><strong>Explore the docs »</strong></a>
|
|
20
20
|
<br />
|
|
21
21
|
<br />
|
|
22
22
|
<a href="https://rjsf-team.github.io/react-jsonschema-form/">View Playground</a>
|
|
@@ -73,7 +73,7 @@ import * as Utils from '@rjsf/utils';
|
|
|
73
73
|
|
|
74
74
|
## Documentation
|
|
75
75
|
|
|
76
|
-
[Utility function documentation](https://react-jsonschema-form
|
|
76
|
+
[Utility function documentation](https://rjsf-team.github.io/react-jsonschema-form/docs/api-reference/utility-functions/)
|
|
77
77
|
|
|
78
78
|
<!-- ROADMAP -->
|
|
79
79
|
|
|
@@ -85,7 +85,7 @@ See the [open issues](https://github.com/rjsf-team/react-jsonschema-form/issues)
|
|
|
85
85
|
|
|
86
86
|
## Contributing
|
|
87
87
|
|
|
88
|
-
Read our [contributors' guide](https://react-jsonschema-form
|
|
88
|
+
Read our [contributors' guide](https://rjsf-team.github.io/react-jsonschema-form/docs/contributing/) to get started.
|
|
89
89
|
|
|
90
90
|
<!-- CONTACT -->
|
|
91
91
|
|
package/dist/index.d.ts
CHANGED
|
@@ -265,6 +265,16 @@ interface FieldProps<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends
|
|
|
265
265
|
required?: boolean;
|
|
266
266
|
/** The unique name of the field, usually derived from the name of the property in the JSONSchema */
|
|
267
267
|
name: string;
|
|
268
|
+
/** To avoid collisions with existing ids in the DOM, it is possible to change the prefix used for ids;
|
|
269
|
+
* Default is `root`
|
|
270
|
+
*/
|
|
271
|
+
idPrefix?: string;
|
|
272
|
+
/** To avoid using a path separator that is present in field names, it is possible to change the separator used for
|
|
273
|
+
* ids (Default is `_`)
|
|
274
|
+
*/
|
|
275
|
+
idSeparator?: string;
|
|
276
|
+
/** An array of strings listing all generated error messages from encountered errors for this field */
|
|
277
|
+
rawErrors?: string[];
|
|
268
278
|
/** The `registry` object */
|
|
269
279
|
registry: Registry<T, S, F>;
|
|
270
280
|
}
|
|
@@ -766,8 +776,8 @@ interface SchemaUtilsType<T = any, S extends StrictRJSFSchema = RJSFSchema, F ex
|
|
|
766
776
|
* @param schema - The schema for which the default state is desired
|
|
767
777
|
* @param [formData] - The current formData, if any, onto which to provide any missing defaults
|
|
768
778
|
* @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults.
|
|
769
|
-
* If "excludeObjectChildren",
|
|
770
|
-
* object properties.
|
|
779
|
+
* If "excludeObjectChildren", cause undefined values for this object and pass `includeUndefinedValues` as
|
|
780
|
+
* false when computing defaults for any nested object properties.
|
|
771
781
|
* @returns - The resulting `formData` with all the defaults provided
|
|
772
782
|
*/
|
|
773
783
|
getDefaultFormState(schema: S, formData?: T, includeUndefinedValues?: boolean | "excludeObjectChildren"): T | T[] | undefined;
|
|
@@ -1396,8 +1406,8 @@ declare const UI_OPTIONS_KEY = "ui:options";
|
|
|
1396
1406
|
* @param [formData] - The current formData, if any, onto which to provide any missing defaults
|
|
1397
1407
|
* @param [rootSchema] - The root schema, used to primarily to look up `$ref`s
|
|
1398
1408
|
* @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults.
|
|
1399
|
-
* If "excludeObjectChildren",
|
|
1400
|
-
* object properties.
|
|
1409
|
+
* If "excludeObjectChildren", cause undefined values for this object and pass `includeUndefinedValues` as
|
|
1410
|
+
* false when computing defaults for any nested object properties.
|
|
1401
1411
|
* @returns - The resulting `formData` with all the defaults provided
|
|
1402
1412
|
*/
|
|
1403
1413
|
declare function getDefaultFormState<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(validator: ValidatorType<T, S, F>, theSchema: S, formData?: T, rootSchema?: S, includeUndefinedValues?: boolean | "excludeObjectChildren"): T | T[] | undefined;
|
|
@@ -1095,17 +1095,48 @@ function getInnerSchemaForArrayItem(schema, additionalItems, idx) {
|
|
|
1095
1095
|
}
|
|
1096
1096
|
return {};
|
|
1097
1097
|
}
|
|
1098
|
+
/** Either add `computedDefault` at `key` into `obj` or not add it based on its value and the value of
|
|
1099
|
+
* `includeUndefinedValues`. Generally undefined `computedDefault` values are added only when `includeUndefinedValues`
|
|
1100
|
+
* is either true or "excludeObjectChildren". If `includeUndefinedValues` is false, then non-undefined and
|
|
1101
|
+
* non-empty-object values will be added.
|
|
1102
|
+
*
|
|
1103
|
+
* @param obj - The object into which the computed default may be added
|
|
1104
|
+
* @param key - The key into the object at which the computed default may be added
|
|
1105
|
+
* @param computedDefault - The computed default value that maybe should be added to the obj
|
|
1106
|
+
* @param includeUndefinedValues - Optional flag, if true, cause undefined values to be added as defaults.
|
|
1107
|
+
* If "excludeObjectChildren", cause undefined values for this object and pass `includeUndefinedValues` as
|
|
1108
|
+
* false when computing defaults for any nested object properties. If "allowEmptyObject", prevents undefined
|
|
1109
|
+
* values in this object while allow the object itself to be empty and passing `includeUndefinedValues` as
|
|
1110
|
+
* false when computing defaults for any nested object properties.
|
|
1111
|
+
* @param requiredFields - The list of fields that are required
|
|
1112
|
+
*/
|
|
1113
|
+
function maybeAddDefaultToObject(obj, key, computedDefault, includeUndefinedValues, requiredFields) {
|
|
1114
|
+
if (requiredFields === void 0) {
|
|
1115
|
+
requiredFields = [];
|
|
1116
|
+
}
|
|
1117
|
+
if (includeUndefinedValues) {
|
|
1118
|
+
obj[key] = computedDefault;
|
|
1119
|
+
} else if (isObject(computedDefault)) {
|
|
1120
|
+
// Store computedDefault if it's a non-empty object (e.g. not {})
|
|
1121
|
+
if (!isEmpty__default["default"](computedDefault) || requiredFields.includes(key)) {
|
|
1122
|
+
obj[key] = computedDefault;
|
|
1123
|
+
}
|
|
1124
|
+
} else if (computedDefault !== undefined) {
|
|
1125
|
+
// Store computedDefault if it's a defined primitive (e.g. true)
|
|
1126
|
+
obj[key] = computedDefault;
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1098
1129
|
/** Computes the defaults for the current `schema` given the `rawFormData` and `parentDefaults` if any. This drills into
|
|
1099
1130
|
* each level of the schema, recursively, to fill out every level of defaults provided by the schema.
|
|
1100
1131
|
*
|
|
1101
1132
|
* @param validator - an implementation of the `ValidatorType` interface that will be used when necessary
|
|
1102
|
-
* @param
|
|
1133
|
+
* @param rawSchema - The schema for which the default state is desired
|
|
1103
1134
|
* @param [parentDefaults] - Any defaults provided by the parent field in the schema
|
|
1104
1135
|
* @param [rootSchema] - The options root schema, used to primarily to look up `$ref`s
|
|
1105
1136
|
* @param [rawFormData] - The current formData, if any, onto which to provide any missing defaults
|
|
1106
1137
|
* @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults.
|
|
1107
|
-
* If "excludeObjectChildren",
|
|
1108
|
-
* object properties.
|
|
1138
|
+
* If "excludeObjectChildren", cause undefined values for this object and pass `includeUndefinedValues` as
|
|
1139
|
+
* false when computing defaults for any nested object properties.
|
|
1109
1140
|
* @returns - The resulting `formData` with all the defaults provided
|
|
1110
1141
|
*/
|
|
1111
1142
|
function computeDefaults(validator, rawSchema, parentDefaults, rootSchema, rawFormData, includeUndefinedValues) {
|
|
@@ -1137,8 +1168,14 @@ function computeDefaults(validator, rawSchema, parentDefaults, rootSchema, rawFo
|
|
|
1137
1168
|
return computeDefaults(validator, itemSchema, Array.isArray(parentDefaults) ? parentDefaults[idx] : undefined, rootSchema, formData, includeUndefinedValues);
|
|
1138
1169
|
});
|
|
1139
1170
|
} else if (ONE_OF_KEY in schema) {
|
|
1171
|
+
if (schema.oneOf.length === 0) {
|
|
1172
|
+
return undefined;
|
|
1173
|
+
}
|
|
1140
1174
|
schema = schema.oneOf[getClosestMatchingOption(validator, rootSchema, isEmpty__default["default"](formData) ? undefined : formData, schema.oneOf, 0)];
|
|
1141
1175
|
} else if (ANY_OF_KEY in schema) {
|
|
1176
|
+
if (schema.anyOf.length === 0) {
|
|
1177
|
+
return undefined;
|
|
1178
|
+
}
|
|
1142
1179
|
schema = schema.anyOf[getClosestMatchingOption(validator, rootSchema, isEmpty__default["default"](formData) ? undefined : formData, schema.anyOf, 0)];
|
|
1143
1180
|
}
|
|
1144
1181
|
// Not defaults defined for this node, fallback to generic typed ones.
|
|
@@ -1148,23 +1185,25 @@ function computeDefaults(validator, rawSchema, parentDefaults, rootSchema, rawFo
|
|
|
1148
1185
|
switch (getSchemaType(schema)) {
|
|
1149
1186
|
// We need to recur for object schema inner default values.
|
|
1150
1187
|
case "object":
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
acc
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1188
|
+
{
|
|
1189
|
+
var objectDefaults = Object.keys(schema.properties || {}).reduce(function (acc, key) {
|
|
1190
|
+
// Compute the defaults for this node, with the parent defaults we might
|
|
1191
|
+
// have from a previous run: defaults[key].
|
|
1192
|
+
var computedDefault = computeDefaults(validator, get__default["default"](schema, [PROPERTIES_KEY, key]), get__default["default"](defaults, [key]), rootSchema, get__default["default"](formData, [key]), includeUndefinedValues === true);
|
|
1193
|
+
maybeAddDefaultToObject(acc, key, computedDefault, includeUndefinedValues, schema.required);
|
|
1194
|
+
return acc;
|
|
1195
|
+
}, {});
|
|
1196
|
+
if (schema.additionalProperties && isObject(defaults)) {
|
|
1197
|
+
var additionalPropertiesSchema = isObject(schema.additionalProperties) ? schema.additionalProperties : {}; // as per spec additionalProperties may be either schema or boolean
|
|
1198
|
+
Object.keys(defaults).filter(function (key) {
|
|
1199
|
+
return !schema.properties || !schema.properties[key];
|
|
1200
|
+
}).forEach(function (key) {
|
|
1201
|
+
var computedDefault = computeDefaults(validator, additionalPropertiesSchema, get__default["default"](defaults, [key]), rootSchema, get__default["default"](formData, [key]), includeUndefinedValues === true);
|
|
1202
|
+
maybeAddDefaultToObject(objectDefaults, key, computedDefault, includeUndefinedValues);
|
|
1203
|
+
});
|
|
1165
1204
|
}
|
|
1166
|
-
return
|
|
1167
|
-
}
|
|
1205
|
+
return objectDefaults;
|
|
1206
|
+
}
|
|
1168
1207
|
case "array":
|
|
1169
1208
|
// Inject defaults into existing array defaults
|
|
1170
1209
|
if (Array.isArray(defaults)) {
|
|
@@ -1206,8 +1245,8 @@ function computeDefaults(validator, rawSchema, parentDefaults, rootSchema, rawFo
|
|
|
1206
1245
|
* @param [formData] - The current formData, if any, onto which to provide any missing defaults
|
|
1207
1246
|
* @param [rootSchema] - The root schema, used to primarily to look up `$ref`s
|
|
1208
1247
|
* @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults.
|
|
1209
|
-
* If "excludeObjectChildren",
|
|
1210
|
-
* object properties.
|
|
1248
|
+
* If "excludeObjectChildren", cause undefined values for this object and pass `includeUndefinedValues` as
|
|
1249
|
+
* false when computing defaults for any nested object properties.
|
|
1211
1250
|
* @returns - The resulting `formData` with all the defaults provided
|
|
1212
1251
|
*/
|
|
1213
1252
|
function getDefaultFormState(validator, theSchema, formData, rootSchema, includeUndefinedValues) {
|