@rjsf/utils 6.5.2 → 6.5.3

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/lib/types.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import type { ButtonHTMLAttributes, ChangeEvent, ComponentType, FocusEvent, HTMLAttributes, ReactElement, ReactNode, StyleHTMLAttributes } from 'react';
2
2
  import { JSONSchema7 } from 'json-schema';
3
3
  import { TranslatableString } from './enums.js';
4
+ import './jsonSchemaAugmentation.js';
4
5
  /** The representation of any generic object type, usually used as an intersection on other types to make them more
5
6
  * flexible in the properties they support (i.e. anything else)
6
7
  */
@@ -156,6 +157,8 @@ export type InputPropsType = {
156
157
  * `['baz', 1]` represents the second element in the list `baz` of the schema
157
158
  */
158
159
  export type FieldPathList = (string | number)[];
160
+ /** Dot string or segment list for `getFromSchema` / `findFieldInSchema` (same segment rules as {@link FieldPathList}). */
161
+ export type SchemaFieldPath = string | FieldPathList;
159
162
  /** Type describing an id and path used for a field */
160
163
  export type FieldPathId = {
161
164
  /** The id for a field */
@@ -383,6 +386,12 @@ export type GlobalUISchemaOptions = GenericObjectType & {
383
386
  * only affects the DOM-level encoding.
384
387
  */
385
388
  optionValueFormat?: OptionValueFormat;
389
+ /** Controls how a deprecated property is rendered.
390
+ * - `hide`: The field is completely hidden (via the `hidden` prop passed to FieldTemplate).
391
+ * - `disable`: The field is rendered but disabled.
392
+ * - `label` (default): The field is rendered with "(deprecated)" appended to its label.
393
+ */
394
+ deprecatedHandling?: 'hide' | 'disable' | 'label';
386
395
  };
387
396
  /** The set of options from the `Form` that will be available on the `Registry` for use in everywhere the `registry` is
388
397
  * available.
@@ -1073,7 +1082,7 @@ export interface SchemaUtilsType<T = any, S extends StrictRJSFSchema = RJSFSchem
1073
1082
  * @returns - An object that contains the field and its required state. If no field can be found then
1074
1083
  * `{ field: undefined, isRequired: undefined }` is returned.
1075
1084
  */
1076
- findFieldInSchema(schema: S, path: string | string[], formData?: T): FoundFieldType<S>;
1085
+ findFieldInSchema(schema: S, path: SchemaFieldPath, formData?: T): FoundFieldType<S>;
1077
1086
  /** Finds the oneOf option inside the `schema['any/oneOf']` list which has the `properties[selectorField].default` that
1078
1087
  * matches the `formData[selectorField]` value. For the purposes of this function, `selectorField` is either
1079
1088
  * `schema.discriminator.propertyName` or `fallbackField`.
@@ -1138,9 +1147,9 @@ export interface SchemaUtilsType<T = any, S extends StrictRJSFSchema = RJSFSchem
1138
1147
  * @param defaultValue - The value to return if a value is not found for the `pathList` path
1139
1148
  * @returns - The internal schema from the `schema` for the given `path` or the `defaultValue` if not found
1140
1149
  */
1141
- getFromSchema(schema: S, path: string | string[], defaultValue: T): T;
1142
- getFromSchema(schema: S, path: string | string[], defaultValue: S): S;
1143
- getFromSchema(schema: S, path: string | string[], defaultValue: T | S): S | T;
1150
+ getFromSchema(schema: S, path: SchemaFieldPath, defaultValue: T): T;
1151
+ getFromSchema(schema: S, path: SchemaFieldPath, defaultValue: S): S;
1152
+ getFromSchema(schema: S, path: SchemaFieldPath, defaultValue: T | S): S | T;
1144
1153
  /** Checks to see if the `schema` and `uiSchema` combination represents an array of files
1145
1154
  *
1146
1155
  * @param schema - The schema for which check for array of files flag is desired
package/lib/types.js CHANGED
@@ -1,2 +1,2 @@
1
- export {};
1
+ import './jsonSchemaAugmentation.js';
2
2
  //# sourceMappingURL=types.js.map
package/lib/types.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAaA,OAAO,0BAA0B,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rjsf/utils",
3
- "version": "6.5.2",
3
+ "version": "6.5.3",
4
4
  "main": "dist/index.js",
5
5
  "module": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
@@ -67,7 +67,7 @@
67
67
  "dependencies": {
68
68
  "@x0k/json-schema-merge": "^1.0.3",
69
69
  "fast-equals": "^6.0.0",
70
- "fast-uri": "^3.1.0",
70
+ "fast-uri": "^3.1.2",
71
71
  "jsonpointer": "^5.0.1",
72
72
  "lodash": "^4.18.1",
73
73
  "lodash-es": "^4.18.1",
@@ -7,6 +7,7 @@ import {
7
7
  GlobalUISchemaOptions,
8
8
  PathSchema,
9
9
  RJSFSchema,
10
+ SchemaFieldPath,
10
11
  SchemaUtilsType,
11
12
  StrictRJSFSchema,
12
13
  UiSchema,
@@ -127,7 +128,7 @@ class SchemaUtils<
127
128
  * @returns - An object that contains the field and its required state. If no field can be found then
128
129
  * `{ field: undefined, isRequired: undefined }` is returned.
129
130
  */
130
- findFieldInSchema(schema: S, path: string | string[], formData?: T): FoundFieldType<S> {
131
+ findFieldInSchema(schema: S, path: SchemaFieldPath, formData?: T): FoundFieldType<S> {
131
132
  return findFieldInSchema(
132
133
  this.validator,
133
134
  this.rootSchema,
@@ -259,9 +260,9 @@ class SchemaUtils<
259
260
  * @param defaultValue - The value to return if a value is not found for the `pathList` path
260
261
  * @returns - The internal schema from the `schema` for the given `path` or the `defaultValue` if not found
261
262
  */
262
- getFromSchema(schema: S, path: string | string[], defaultValue: T): T;
263
- getFromSchema(schema: S, path: string | string[], defaultValue: S): S;
264
- getFromSchema(schema: S, path: string | string[], defaultValue: T | S): T | S {
263
+ getFromSchema(schema: S, path: SchemaFieldPath, defaultValue: T): T;
264
+ getFromSchema(schema: S, path: SchemaFieldPath, defaultValue: S): S;
265
+ getFromSchema(schema: S, path: SchemaFieldPath, defaultValue: T | S): T | S {
265
266
  return getFromSchema<T, S, F>(
266
267
  this.validator,
267
268
  this.rootSchema,
package/src/enums.ts CHANGED
@@ -68,6 +68,8 @@ export enum TranslatableString {
68
68
  TitleOptionPrefix = '%1 option %2',
69
69
  /** Key label, where %1 will be replaced by the label as provided by WrapIfAdditionalTemplate */
70
70
  KeyLabel = '%1 Key',
71
+ /** Deprecated label, where %1 will be replaced by the label as provided by SchemaField */
72
+ DeprecatedLabel = '%1 (deprecated)',
71
73
  // Strings with replaceable parameters AND/OR that support markdown and html
72
74
  /** Invalid object field configuration as provided by the ObjectField.
73
75
  * NOTE: Use markdown notation rather than html tags.
@@ -0,0 +1,21 @@
1
+ import type { JSONSchema7 } from 'json-schema';
2
+
3
+ /**
4
+ * This file is used to augment the `json-schema` module with the `deprecated` keyword.
5
+ *
6
+ * It is a dedicated file because `json-schema` is a type-only package. Standard augmentation
7
+ * using `import 'json-schema'` in a file with other imports can cause module resolution
8
+ * errors in certain environments (like Jest). Using `import type` in this dedicated file
9
+ * ensures the augmentation is correctly applied by the TypeScript compiler without
10
+ * confusing runtime module resolvers.
11
+ */
12
+ declare module 'json-schema' {
13
+ export interface JSONSchema7 {
14
+ /** The deprecated keyword is a boolean that indicates that the instance value the keyword applies to should not be
15
+ * used and may be removed in the future.
16
+ */
17
+ deprecated?: boolean;
18
+ }
19
+ }
20
+
21
+ export type { JSONSchema7 };
@@ -68,7 +68,8 @@ export default function mergeDefaultsWithFormData<T = any>(
68
68
  const keyExistsInDefaults = isObject(defaults) && key in (defaults as GenericObjectType);
69
69
  const keyExistsInFormData = key in (formData as GenericObjectType);
70
70
  const keyDefault = get(defaults, key) ?? {};
71
- const defaultValueIsNestedObject = keyExistsInDefaults && Object.entries(keyDefault).some(([, v]) => isObject(v));
71
+ const defaultValueIsNestedObject =
72
+ keyExistsInDefaults && isObject(keyDefault) && Object.values(keyDefault).some((v) => isObject(v));
72
73
 
73
74
  const keyDefaultIsObject = keyExistsInDefaults && isObject(get(defaults, key));
74
75
  const keyHasFormDataObject = keyExistsInFormData && isObject(keyValue);
@@ -9,6 +9,7 @@ import {
9
9
  FormContextType,
10
10
  FoundFieldType,
11
11
  RJSFSchema,
12
+ SchemaFieldPath,
12
13
  StrictRJSFSchema,
13
14
  ValidatorType,
14
15
  } from '../types';
@@ -37,7 +38,7 @@ export default function findFieldInSchema<
37
38
  validator: ValidatorType<T, S, F>,
38
39
  rootSchema: S,
39
40
  schema: S,
40
- path: string | string[],
41
+ path: SchemaFieldPath,
41
42
  formData: T = {} as T,
42
43
  experimental_customMergeAllOf?: Experimental_CustomMergeAllOf<S>,
43
44
  ): FoundFieldType<S> {
@@ -46,6 +47,7 @@ export default function findFieldInSchema<
46
47
 
47
48
  // store the desired field into a variable and removing it from the `pathList`
48
49
  const fieldName = pathList.pop()!;
50
+ const fieldNameKey = String(fieldName);
49
51
 
50
52
  if (pathList.length) {
51
53
  // drilling into the schema for each sub-path and taking into account of the any/oneOfs
@@ -64,7 +66,7 @@ export default function findFieldInSchema<
64
66
  validator,
65
67
  rootSchema,
66
68
  parentField,
67
- fieldName,
69
+ fieldNameKey,
68
70
  ONE_OF_KEY,
69
71
  get(formData, subPath),
70
72
  experimental_customMergeAllOf,
@@ -75,7 +77,7 @@ export default function findFieldInSchema<
75
77
  validator,
76
78
  rootSchema,
77
79
  parentField,
78
- fieldName,
80
+ fieldNameKey,
79
81
  ANY_OF_KEY,
80
82
  get(formData, subPath),
81
83
  experimental_customMergeAllOf,
@@ -90,7 +92,7 @@ export default function findFieldInSchema<
90
92
  validator,
91
93
  rootSchema,
92
94
  parentField,
93
- fieldName,
95
+ fieldNameKey,
94
96
  ONE_OF_KEY,
95
97
  formData,
96
98
  experimental_customMergeAllOf,
@@ -101,7 +103,7 @@ export default function findFieldInSchema<
101
103
  validator,
102
104
  rootSchema,
103
105
  parentField,
104
- fieldName,
106
+ fieldNameKey,
105
107
  ANY_OF_KEY,
106
108
  formData,
107
109
  experimental_customMergeAllOf,
@@ -131,7 +133,7 @@ export default function findFieldInSchema<
131
133
  );
132
134
  let isRequired: boolean | undefined;
133
135
  if (field && Array.isArray(requiredArray)) {
134
- isRequired = requiredArray.includes(fieldName);
136
+ isRequired = requiredArray.includes(fieldNameKey);
135
137
  }
136
138
 
137
139
  return { field, isRequired };
@@ -3,7 +3,14 @@ import has from 'lodash/has';
3
3
  import isEmpty from 'lodash/isEmpty';
4
4
 
5
5
  import retrieveSchema from './retrieveSchema';
6
- import { Experimental_CustomMergeAllOf, FormContextType, RJSFSchema, StrictRJSFSchema, ValidatorType } from '../types';
6
+ import {
7
+ Experimental_CustomMergeAllOf,
8
+ FormContextType,
9
+ RJSFSchema,
10
+ SchemaFieldPath,
11
+ StrictRJSFSchema,
12
+ ValidatorType,
13
+ } from '../types';
7
14
  import { REF_KEY } from '../constants';
8
15
 
9
16
  /** Internal helper function that acts like lodash's `get` but additionally retrieves `$ref`s as needed to get the path
@@ -20,7 +27,7 @@ function getFromSchemaInternal<T = any, S extends StrictRJSFSchema = RJSFSchema,
20
27
  validator: ValidatorType<T, S, F>,
21
28
  rootSchema: S,
22
29
  schema: S,
23
- path: string | string[],
30
+ path: SchemaFieldPath,
24
31
  experimental_customMergeAllOf?: Experimental_CustomMergeAllOf<S>,
25
32
  ): T | S | undefined {
26
33
  let fieldSchema = schema;
@@ -30,9 +37,9 @@ function getFromSchemaInternal<T = any, S extends StrictRJSFSchema = RJSFSchema,
30
37
  if (isEmpty(path)) {
31
38
  return fieldSchema;
32
39
  }
33
- const pathList = Array.isArray(path) ? path : path.split('.');
40
+ const pathList = Array.isArray(path) ? [...path] : path.split('.');
34
41
  const [part, ...nestedPath] = pathList;
35
- if (part && has(fieldSchema, part)) {
42
+ if (part !== undefined && part !== '' && has(fieldSchema, part)) {
36
43
  fieldSchema = get(fieldSchema, part) as S;
37
44
  return getFromSchemaInternal<T, S, F>(
38
45
  validator,
@@ -64,7 +71,7 @@ export default function getFromSchema<
64
71
  validator: ValidatorType<T, S, F>,
65
72
  rootSchema: S,
66
73
  schema: S,
67
- path: string | string[],
74
+ path: SchemaFieldPath,
68
75
  defaultValue: T,
69
76
  experimental_customMergeAllOf?: Experimental_CustomMergeAllOf<S>,
70
77
  ): T;
@@ -76,7 +83,7 @@ export default function getFromSchema<
76
83
  validator: ValidatorType<T, S, F>,
77
84
  rootSchema: S,
78
85
  schema: S,
79
- path: string | string[],
86
+ path: SchemaFieldPath,
80
87
  defaultValue: S,
81
88
  experimental_customMergeAllOf?: Experimental_CustomMergeAllOf<S>,
82
89
  ): S;
@@ -88,7 +95,7 @@ export default function getFromSchema<
88
95
  validator: ValidatorType<T, S, F>,
89
96
  rootSchema: S,
90
97
  schema: S,
91
- path: string | string[],
98
+ path: SchemaFieldPath,
92
99
  defaultValue: T | S,
93
100
  experimental_customMergeAllOf?: Experimental_CustomMergeAllOf<S>,
94
101
  ): T | S {
package/src/types.ts CHANGED
@@ -11,6 +11,7 @@ import type {
11
11
  import { JSONSchema7 } from 'json-schema';
12
12
 
13
13
  import { TranslatableString } from './enums';
14
+ import './jsonSchemaAugmentation';
14
15
 
15
16
  /** The representation of any generic object type, usually used as an intersection on other types to make them more
16
17
  * flexible in the properties they support (i.e. anything else)
@@ -185,6 +186,9 @@ export type InputPropsType = {
185
186
  */
186
187
  export type FieldPathList = (string | number)[];
187
188
 
189
+ /** Dot string or segment list for `getFromSchema` / `findFieldInSchema` (same segment rules as {@link FieldPathList}). */
190
+ export type SchemaFieldPath = string | FieldPathList;
191
+
188
192
  /** Type describing an id and path used for a field */
189
193
  export type FieldPathId = {
190
194
  /** The id for a field */
@@ -447,6 +451,12 @@ export type GlobalUISchemaOptions = GenericObjectType & {
447
451
  * only affects the DOM-level encoding.
448
452
  */
449
453
  optionValueFormat?: OptionValueFormat;
454
+ /** Controls how a deprecated property is rendered.
455
+ * - `hide`: The field is completely hidden (via the `hidden` prop passed to FieldTemplate).
456
+ * - `disable`: The field is rendered but disabled.
457
+ * - `label` (default): The field is rendered with "(deprecated)" appended to its label.
458
+ */
459
+ deprecatedHandling?: 'hide' | 'disable' | 'label';
450
460
  };
451
461
 
452
462
  /** The set of options from the `Form` that will be available on the `Registry` for use in everywhere the `registry` is
@@ -1328,7 +1338,7 @@ export interface SchemaUtilsType<T = any, S extends StrictRJSFSchema = RJSFSchem
1328
1338
  * @returns - An object that contains the field and its required state. If no field can be found then
1329
1339
  * `{ field: undefined, isRequired: undefined }` is returned.
1330
1340
  */
1331
- findFieldInSchema(schema: S, path: string | string[], formData?: T): FoundFieldType<S>;
1341
+ findFieldInSchema(schema: S, path: SchemaFieldPath, formData?: T): FoundFieldType<S>;
1332
1342
  /** Finds the oneOf option inside the `schema['any/oneOf']` list which has the `properties[selectorField].default` that
1333
1343
  * matches the `formData[selectorField]` value. For the purposes of this function, `selectorField` is either
1334
1344
  * `schema.discriminator.propertyName` or `fallbackField`.
@@ -1403,9 +1413,9 @@ export interface SchemaUtilsType<T = any, S extends StrictRJSFSchema = RJSFSchem
1403
1413
  * @param defaultValue - The value to return if a value is not found for the `pathList` path
1404
1414
  * @returns - The internal schema from the `schema` for the given `path` or the `defaultValue` if not found
1405
1415
  */
1406
- getFromSchema(schema: S, path: string | string[], defaultValue: T): T;
1407
- getFromSchema(schema: S, path: string | string[], defaultValue: S): S;
1408
- getFromSchema(schema: S, path: string | string[], defaultValue: T | S): S | T;
1416
+ getFromSchema(schema: S, path: SchemaFieldPath, defaultValue: T): T;
1417
+ getFromSchema(schema: S, path: SchemaFieldPath, defaultValue: S): S;
1418
+ getFromSchema(schema: S, path: SchemaFieldPath, defaultValue: T | S): S | T;
1409
1419
  /** Checks to see if the `schema` and `uiSchema` combination represents an array of files
1410
1420
  *
1411
1421
  * @param schema - The schema for which check for array of files flag is desired