@jsonforms/core 3.1.0-alpha.0 → 3.1.0-alpha.2

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.
Files changed (205) hide show
  1. package/README.md +3 -3
  2. package/lib/Helpers.d.ts +1 -1
  3. package/lib/actions/actions.d.ts +27 -26
  4. package/lib/generators/Generate.d.ts +1 -1
  5. package/lib/generators/schema.d.ts +1 -1
  6. package/lib/i18n/arrayTranslations.d.ts +24 -0
  7. package/lib/i18n/i18nTypes.d.ts +3 -2
  8. package/lib/i18n/i18nUtil.d.ts +5 -2
  9. package/lib/i18n/index.d.ts +1 -0
  10. package/lib/jsonforms-core.cjs.js +428 -259
  11. package/lib/jsonforms-core.cjs.js.map +1 -1
  12. package/lib/jsonforms-core.esm.js +313 -200
  13. package/lib/jsonforms-core.esm.js.map +1 -1
  14. package/lib/models/jsonSchema.d.ts +2 -2
  15. package/lib/models/uischema.d.ts +1 -1
  16. package/lib/reducers/cells.d.ts +2 -2
  17. package/lib/reducers/config.d.ts +1 -1
  18. package/lib/reducers/core.d.ts +3 -2
  19. package/lib/reducers/default-data.d.ts +1 -1
  20. package/lib/reducers/i18n.d.ts +1 -1
  21. package/lib/reducers/reducers.d.ts +4 -4
  22. package/lib/reducers/renderers.d.ts +2 -2
  23. package/lib/reducers/selectors.d.ts +6 -6
  24. package/lib/reducers/uischemas.d.ts +2 -2
  25. package/lib/store.d.ts +3 -3
  26. package/lib/testers/testers.d.ts +1 -1
  27. package/lib/util/cell.d.ts +3 -4
  28. package/lib/util/combinators.d.ts +1 -1
  29. package/lib/util/label.d.ts +1 -1
  30. package/lib/util/path.d.ts +6 -3
  31. package/lib/util/renderer.d.ts +11 -7
  32. package/lib/util/resolvers.d.ts +1 -1
  33. package/lib/util/runtime.d.ts +2 -2
  34. package/lib/util/util.d.ts +2 -2
  35. package/lib/util/validator.d.ts +1 -1
  36. package/package.json +16 -5
  37. package/src/Helpers.ts +2 -2
  38. package/src/actions/actions.ts +59 -61
  39. package/src/configDefault.ts +1 -1
  40. package/src/generators/Generate.ts +4 -2
  41. package/src/generators/schema.ts +30 -26
  42. package/src/generators/uischema.ts +7 -6
  43. package/src/i18n/arrayTranslations.ts +54 -0
  44. package/src/i18n/i18nTypes.ts +12 -7
  45. package/src/i18n/i18nUtil.ts +66 -16
  46. package/src/i18n/index.ts +1 -0
  47. package/src/models/draft4.ts +33 -33
  48. package/src/models/jsonSchema.ts +2 -2
  49. package/src/models/uischema.ts +18 -7
  50. package/src/reducers/cells.ts +10 -9
  51. package/src/reducers/config.ts +1 -1
  52. package/src/reducers/core.ts +115 -75
  53. package/src/reducers/default-data.ts +8 -8
  54. package/src/reducers/i18n.ts +22 -10
  55. package/src/reducers/reducers.ts +24 -34
  56. package/src/reducers/renderers.ts +9 -9
  57. package/src/reducers/selectors.ts +10 -11
  58. package/src/reducers/uischemas.ts +27 -26
  59. package/src/store.ts +4 -4
  60. package/src/testers/testers.ts +200 -147
  61. package/src/util/cell.ts +27 -29
  62. package/src/util/combinators.ts +6 -4
  63. package/src/util/label.ts +2 -2
  64. package/src/util/path.ts +15 -8
  65. package/src/util/renderer.ts +123 -72
  66. package/src/util/resolvers.ts +16 -14
  67. package/src/util/runtime.ts +4 -4
  68. package/src/util/schema.ts +1 -1
  69. package/src/util/type.ts +5 -3
  70. package/src/util/uischema.ts +9 -9
  71. package/src/util/util.ts +54 -54
  72. package/src/util/validator.ts +2 -2
  73. package/.nycrc +0 -6
  74. package/docs/assets/css/main.css +0 -1
  75. package/docs/assets/images/icons.png +0 -0
  76. package/docs/assets/images/icons@2x.png +0 -0
  77. package/docs/assets/images/widgets.png +0 -0
  78. package/docs/assets/images/widgets@2x.png +0 -0
  79. package/docs/assets/js/main.js +0 -51
  80. package/docs/assets/js/search.json +0 -1
  81. package/docs/classes/gen.html +0 -406
  82. package/docs/enums/ruleeffect.html +0 -214
  83. package/docs/globals.html +0 -10568
  84. package/docs/index.html +0 -1297
  85. package/docs/interfaces/action.html +0 -166
  86. package/docs/interfaces/addcellrendereraction.html +0 -181
  87. package/docs/interfaces/addrendereraction.html +0 -181
  88. package/docs/interfaces/adduischemaaction.html +0 -181
  89. package/docs/interfaces/andcondition.html +0 -181
  90. package/docs/interfaces/anyaction.html +0 -189
  91. package/docs/interfaces/arraycontrolprops.html +0 -653
  92. package/docs/interfaces/arraylayoutprops.html +0 -648
  93. package/docs/interfaces/categorization.html +0 -281
  94. package/docs/interfaces/category.html +0 -279
  95. package/docs/interfaces/cellprops.html +0 -452
  96. package/docs/interfaces/combinatorrendererprops.html +0 -503
  97. package/docs/interfaces/combinatorsubschemarenderinfo.html +0 -181
  98. package/docs/interfaces/composablecondition.html +0 -193
  99. package/docs/interfaces/condition.html +0 -176
  100. package/docs/interfaces/controlelement.html +0 -280
  101. package/docs/interfaces/controlprops.html +0 -497
  102. package/docs/interfaces/controlstate.html +0 -184
  103. package/docs/interfaces/controlwithdetailprops.html +0 -501
  104. package/docs/interfaces/dispatchcellprops.html +0 -388
  105. package/docs/interfaces/dispatchcellstateprops.html +0 -408
  106. package/docs/interfaces/dispatchpropsofarraycontrol.html +0 -322
  107. package/docs/interfaces/dispatchpropsofcontrol.html +0 -204
  108. package/docs/interfaces/dispatchpropsofmultienumcontrol.html +0 -215
  109. package/docs/interfaces/enumcellprops.html +0 -467
  110. package/docs/interfaces/enumoption.html +0 -167
  111. package/docs/interfaces/formatted.html +0 -226
  112. package/docs/interfaces/grouplayout.html +0 -284
  113. package/docs/interfaces/horizontallayout.html +0 -234
  114. package/docs/interfaces/initaction.html +0 -209
  115. package/docs/interfaces/initactionoptions.html +0 -181
  116. package/docs/interfaces/internationalizable.html +0 -170
  117. package/docs/interfaces/jsonformscellrendererregistryentry.html +0 -167
  118. package/docs/interfaces/jsonformscore.html +0 -251
  119. package/docs/interfaces/jsonformsdefaultdataregistryentry.html +0 -167
  120. package/docs/interfaces/jsonformsextendedstate.html +0 -175
  121. package/docs/interfaces/jsonformsi18nstate.html +0 -181
  122. package/docs/interfaces/jsonformsprops.html +0 -321
  123. package/docs/interfaces/jsonformsrendererregistryentry.html +0 -167
  124. package/docs/interfaces/jsonformsstate.html +0 -170
  125. package/docs/interfaces/jsonformsstore.html +0 -376
  126. package/docs/interfaces/jsonformssubstates.html +0 -276
  127. package/docs/interfaces/jsonformsuischemaregistryentry.html +0 -167
  128. package/docs/interfaces/jsonschema4.html +0 -758
  129. package/docs/interfaces/jsonschema7.html +0 -884
  130. package/docs/interfaces/labelable.html +0 -184
  131. package/docs/interfaces/labeldescription.html +0 -184
  132. package/docs/interfaces/labeled.html +0 -182
  133. package/docs/interfaces/labelelement.html +0 -251
  134. package/docs/interfaces/labelprops.html +0 -339
  135. package/docs/interfaces/layout.html +0 -253
  136. package/docs/interfaces/layoutprops.html +0 -359
  137. package/docs/interfaces/leafcondition.html +0 -209
  138. package/docs/interfaces/orcondition.html +0 -181
  139. package/docs/interfaces/ownpropsofcell.html +0 -321
  140. package/docs/interfaces/ownpropsofcontrol.html +0 -305
  141. package/docs/interfaces/ownpropsofenum.html +0 -161
  142. package/docs/interfaces/ownpropsofenumcell.html +0 -335
  143. package/docs/interfaces/ownpropsofjsonformsrenderer.html +0 -296
  144. package/docs/interfaces/ownpropsoflabel.html +0 -286
  145. package/docs/interfaces/ownpropsoflayout.html +0 -305
  146. package/docs/interfaces/ownpropsofmasterlistitem.html +0 -289
  147. package/docs/interfaces/ownpropsofrenderer.html +0 -292
  148. package/docs/interfaces/referenceschemamap.html +0 -138
  149. package/docs/interfaces/registerdefaultdataaction.html +0 -181
  150. package/docs/interfaces/removecellrendereraction.html +0 -181
  151. package/docs/interfaces/removerendereraction.html +0 -181
  152. package/docs/interfaces/removeuischemaaction.html +0 -167
  153. package/docs/interfaces/rendererprops.html +0 -331
  154. package/docs/interfaces/rule.html +0 -185
  155. package/docs/interfaces/schemabasedcondition.html +0 -202
  156. package/docs/interfaces/scopable.html +0 -171
  157. package/docs/interfaces/scoped.html +0 -183
  158. package/docs/interfaces/setajvaction.html +0 -167
  159. package/docs/interfaces/setconfigaction.html +0 -167
  160. package/docs/interfaces/setlocaleaction.html +0 -167
  161. package/docs/interfaces/setschemaaction.html +0 -167
  162. package/docs/interfaces/settranslatoraction.html +0 -181
  163. package/docs/interfaces/setuischemaaction.html +0 -167
  164. package/docs/interfaces/setvalidationmodeaction.html +0 -167
  165. package/docs/interfaces/statepropsofarraycontrol.html +0 -474
  166. package/docs/interfaces/statepropsofarraylayout.html +0 -461
  167. package/docs/interfaces/statepropsofcell.html +0 -408
  168. package/docs/interfaces/statepropsofcombinator.html +0 -449
  169. package/docs/interfaces/statepropsofcontrol.html +0 -451
  170. package/docs/interfaces/statepropsofcontrolwithdetail.html +0 -463
  171. package/docs/interfaces/statepropsofenumcell.html +0 -422
  172. package/docs/interfaces/statepropsofjsonformsrenderer.html +0 -324
  173. package/docs/interfaces/statepropsoflabel.html +0 -343
  174. package/docs/interfaces/statepropsoflayout.html +0 -369
  175. package/docs/interfaces/statepropsofmasteritem.html +0 -309
  176. package/docs/interfaces/statepropsofrenderer.html +0 -331
  177. package/docs/interfaces/statepropsofscopedrenderer.html +0 -391
  178. package/docs/interfaces/store.html +0 -386
  179. package/docs/interfaces/testercontext.html +0 -184
  180. package/docs/interfaces/uischemaelement.html +0 -225
  181. package/docs/interfaces/unregisterdefaultdataaction.html +0 -167
  182. package/docs/interfaces/updateaction.html +0 -202
  183. package/docs/interfaces/updatecoreaction.html +0 -209
  184. package/docs/interfaces/updateerrorsaction.html +0 -167
  185. package/docs/interfaces/updatei18naction.html +0 -195
  186. package/docs/interfaces/verticallayout.html +0 -234
  187. package/docs/interfaces/withclassname.html +0 -153
  188. package/rollup.config.js +0 -44
  189. package/stats.html +0 -3279
  190. package/test/actions/actions.test.ts +0 -93
  191. package/test/generators/schema.test.ts +0 -224
  192. package/test/generators/uischema.test.ts +0 -576
  193. package/test/i18n/i18nUtil.test.ts +0 -88
  194. package/test/reducers/core.test.ts +0 -1682
  195. package/test/reducers/uischemas.test.ts +0 -173
  196. package/test/testers.test.ts +0 -948
  197. package/test/util/array.test.ts +0 -28
  198. package/test/util/cell.test.ts +0 -354
  199. package/test/util/derivetype.test.ts +0 -101
  200. package/test/util/label.test.ts +0 -364
  201. package/test/util/path.test.ts +0 -259
  202. package/test/util/renderer.test.ts +0 -1830
  203. package/test/util/resolvers.test.ts +0 -157
  204. package/test/util/runtime.test.ts +0 -779
  205. package/tsconfig.json +0 -13
@@ -23,7 +23,7 @@
23
23
  THE SOFTWARE.
24
24
  */
25
25
 
26
- import { JsonSchema4 } from '../models';
26
+ import type { JsonSchema4 } from '../models';
27
27
 
28
28
  const ADDITIONAL_PROPERTIES = 'additionalProperties';
29
29
  const REQUIRED_PROPERTIES = 'required';
@@ -36,7 +36,7 @@ const distinct = (
36
36
  ): JsonSchema4[] => {
37
37
  const known: { [property: string]: boolean } = {};
38
38
 
39
- return properties.filter(item => {
39
+ return properties.filter((item) => {
40
40
  const discriminatorValue = discriminator(item);
41
41
  if (known.hasOwnProperty(discriminatorValue)) {
42
42
  return false;
@@ -52,12 +52,14 @@ class Gen {
52
52
  private findOption: (props: Properties) => (optionName: string) => any
53
53
  ) {}
54
54
 
55
+ // TODO fix @typescript-eslint/ban-types
56
+ // eslint-disable-next-line @typescript-eslint/ban-types
55
57
  schemaObject = (data: Object): JsonSchema4 => {
56
58
  const props: Properties = this.properties(data);
57
59
  const schema: JsonSchema4 = {
58
60
  type: 'object',
59
61
  properties: props,
60
- additionalProperties: this.findOption(props)(ADDITIONAL_PROPERTIES)
62
+ additionalProperties: this.findOption(props)(ADDITIONAL_PROPERTIES),
61
63
  };
62
64
  const required = this.findOption(props)(REQUIRED_PROPERTIES);
63
65
  if (required.length > 0) {
@@ -111,26 +113,26 @@ class Gen {
111
113
  schemaArray = (data: any[]): JsonSchema4 => {
112
114
  if (data.length > 0) {
113
115
  const allProperties: JsonSchema4[] = data.map(this.property);
114
- const uniqueProperties = distinct(allProperties, prop =>
116
+ const uniqueProperties = distinct(allProperties, (prop) =>
115
117
  JSON.stringify(prop)
116
118
  );
117
119
  if (uniqueProperties.length === 1) {
118
120
  return {
119
121
  type: 'array',
120
- items: uniqueProperties[0]
122
+ items: uniqueProperties[0],
121
123
  };
122
124
  } else {
123
125
  return {
124
126
  type: 'array',
125
127
  items: {
126
- oneOf: uniqueProperties
127
- }
128
+ oneOf: uniqueProperties,
129
+ },
128
130
  };
129
131
  }
130
132
  } else {
131
133
  return {
132
134
  type: 'array',
133
- items: {}
135
+ items: {},
134
136
  };
135
137
  }
136
138
  };
@@ -143,29 +145,31 @@ class Gen {
143
145
  * @returns {JsonSchema} the generated schema
144
146
  */
145
147
  export const generateJsonSchema = (
148
+ // TODO fix @typescript-eslint/ban-types
149
+ // eslint-disable-next-line @typescript-eslint/ban-types
146
150
  instance: Object,
147
151
  options: any = {}
148
152
  ): JsonSchema4 => {
149
- const findOption = (props: Properties) => (
150
- optionName: string
151
- ): boolean | string[] => {
152
- switch (optionName) {
153
- case ADDITIONAL_PROPERTIES:
154
- if (options.hasOwnProperty(ADDITIONAL_PROPERTIES)) {
155
- return options[ADDITIONAL_PROPERTIES];
156
- }
153
+ const findOption =
154
+ (props: Properties) =>
155
+ (optionName: string): boolean | string[] => {
156
+ switch (optionName) {
157
+ case ADDITIONAL_PROPERTIES:
158
+ if (options.hasOwnProperty(ADDITIONAL_PROPERTIES)) {
159
+ return options[ADDITIONAL_PROPERTIES];
160
+ }
157
161
 
158
- return true;
159
- case REQUIRED_PROPERTIES:
160
- if (options.hasOwnProperty(REQUIRED_PROPERTIES)) {
161
- return options[REQUIRED_PROPERTIES](props);
162
- }
162
+ return true;
163
+ case REQUIRED_PROPERTIES:
164
+ if (options.hasOwnProperty(REQUIRED_PROPERTIES)) {
165
+ return options[REQUIRED_PROPERTIES](props);
166
+ }
163
167
 
164
- return Object.keys(props);
165
- default:
166
- return;
167
- }
168
- };
168
+ return Object.keys(props);
169
+ default:
170
+ return;
171
+ }
172
+ };
169
173
 
170
174
  const gen = new Gen(findOption);
171
175
 
@@ -33,7 +33,7 @@ import {
33
33
  JsonSchema,
34
34
  LabelElement,
35
35
  Layout,
36
- UISchemaElement
36
+ UISchemaElement,
37
37
  } from '../models';
38
38
  import { deriveTypes, encode, resolveSchema } from '../util';
39
39
 
@@ -44,7 +44,7 @@ import { deriveTypes, encode, resolveSchema } from '../util';
44
44
  */
45
45
  const createLayout = (layoutType: string): Layout => ({
46
46
  type: layoutType,
47
- elements: []
47
+ elements: [],
48
48
  });
49
49
 
50
50
  /**
@@ -52,7 +52,7 @@ const createLayout = (layoutType: string): Layout => ({
52
52
  */
53
53
  export const createControlElement = (ref: string): ControlElement => ({
54
54
  type: 'Control',
55
- scope: ref
55
+ scope: ref,
56
56
  });
57
57
 
58
58
  /**
@@ -91,7 +91,7 @@ const addLabel = (layout: Layout, labelName: string) => {
91
91
  // add label with name
92
92
  const label: LabelElement = {
93
93
  type: 'Label',
94
- text: fixedLabel
94
+ text: fixedLabel,
95
95
  };
96
96
  layout.elements.push(label);
97
97
  }
@@ -160,7 +160,7 @@ const generateUISchema = (
160
160
  if (!isEmpty(jsonSchema.properties)) {
161
161
  // traverse properties
162
162
  const nextRef: string = currentRef + '/properties';
163
- Object.keys(jsonSchema.properties).map(propName => {
163
+ Object.keys(jsonSchema.properties).map((propName) => {
164
164
  let value = jsonSchema.properties[propName];
165
165
  const ref = `${nextRef}/${encode(propName)}`;
166
166
  if (value.$ref !== undefined) {
@@ -191,11 +191,12 @@ const generateUISchema = (
191
191
  /* falls through */
192
192
  case 'integer':
193
193
  /* falls through */
194
- case 'boolean':
194
+ case 'boolean': {
195
195
  const controlObject: ControlElement = createControlElement(currentRef);
196
196
  schemaElements.push(controlObject);
197
197
 
198
198
  return controlObject;
199
+ }
199
200
  default:
200
201
  throw new Error('Unknown type: ' + JSON.stringify(jsonSchema));
201
202
  }
@@ -0,0 +1,54 @@
1
+ export interface ArrayDefaultTranslation {
2
+ key: ArrayTranslationEnum;
3
+ default: (variable?: string) => string;
4
+ }
5
+
6
+ export enum ArrayTranslationEnum {
7
+ addTooltip = 'addTooltip',
8
+ addAriaLabel = 'addAriaLabel',
9
+ removeTooltip = 'removeTooltip',
10
+ upAriaLabel = 'upAriaLabel',
11
+ downAriaLabel = 'downAriaLabel',
12
+ noSelection = 'noSelection',
13
+ removeAriaLabel = 'removeAriaLabel',
14
+ noDataMessage = 'noDataMessage',
15
+ deleteDialogTitle = 'deleteDialogTitle',
16
+ deleteDialogMessage = 'deleteDialogMessage',
17
+ deleteDialogAccept = 'deleteDialogAccept',
18
+ deleteDialogDecline = 'deleteDialogDecline',
19
+ up = 'up',
20
+ down = 'down',
21
+ }
22
+
23
+ export type ArrayTranslations = {
24
+ [key in ArrayTranslationEnum]?: string;
25
+ };
26
+
27
+ export const arrayDefaultTranslations: ArrayDefaultTranslation[] = [
28
+ {
29
+ key: ArrayTranslationEnum.addTooltip,
30
+ default: (input) => (input ? `Add to ${input}` : 'Add'),
31
+ },
32
+ {
33
+ key: ArrayTranslationEnum.addAriaLabel,
34
+ default: (input) => (input ? `Add to ${input} button` : 'Add button'),
35
+ },
36
+ { key: ArrayTranslationEnum.removeTooltip, default: () => 'Delete' },
37
+ { key: ArrayTranslationEnum.removeAriaLabel, default: () => 'Delete button' },
38
+ { key: ArrayTranslationEnum.upAriaLabel, default: () => `Move item up` },
39
+ { key: ArrayTranslationEnum.up, default: () => 'Up' },
40
+ { key: ArrayTranslationEnum.down, default: () => 'Down' },
41
+ { key: ArrayTranslationEnum.downAriaLabel, default: () => `Move item down` },
42
+ { key: ArrayTranslationEnum.noDataMessage, default: () => 'No data' },
43
+ { key: ArrayTranslationEnum.noSelection, default: () => 'No selection' },
44
+ {
45
+ key: ArrayTranslationEnum.deleteDialogTitle,
46
+ default: () => 'Confirm Deletion',
47
+ },
48
+ {
49
+ key: ArrayTranslationEnum.deleteDialogMessage,
50
+ default: () => 'Are you sure you want to delete the selected entry?',
51
+ },
52
+ { key: ArrayTranslationEnum.deleteDialogAccept, default: () => 'Yes' },
53
+ { key: ArrayTranslationEnum.deleteDialogDecline, default: () => 'No' },
54
+ ];
@@ -1,12 +1,17 @@
1
- import { ErrorObject } from 'ajv';
2
- import { JsonSchema, UISchemaElement } from '../models';
1
+ import type { ErrorObject } from 'ajv';
2
+ import type { JsonSchema, UISchemaElement } from '../models';
3
3
 
4
4
  export type Translator = {
5
- (id: string, defaultMessage: string, values?: any): string;
6
- (id: string, defaultMessage: undefined, values?: any): string | undefined;
7
- }
5
+ (id: string, defaultMessage: string, values?: any): string;
6
+ (id: string, defaultMessage: undefined, values?: any): string | undefined;
7
+ (id: string, defaultMessage?: string, values?: any): string | undefined;
8
+ };
8
9
 
9
- export type ErrorTranslator = (error: ErrorObject, translate: Translator, uischema?: UISchemaElement) => string;
10
+ export type ErrorTranslator = (
11
+ error: ErrorObject,
12
+ translate: Translator,
13
+ uischema?: UISchemaElement
14
+ ) => string;
10
15
 
11
16
  export interface JsonFormsI18nState {
12
17
  locale?: string;
@@ -14,4 +19,4 @@ export interface JsonFormsI18nState {
14
19
  translateError?: ErrorTranslator;
15
20
  }
16
21
 
17
- export type i18nJsonSchema = JsonSchema & {i18n?: string};
22
+ export type i18nJsonSchema = JsonSchema & { i18n?: string };
@@ -1,8 +1,12 @@
1
- import { ErrorObject } from 'ajv';
1
+ import type { ErrorObject } from 'ajv';
2
2
  import { isInternationalized, Labelable, UISchemaElement } from '../models';
3
3
  import { getControlPath } from '../reducers';
4
4
  import { formatErrorMessage } from '../util';
5
- import { i18nJsonSchema, ErrorTranslator, Translator } from './i18nTypes';
5
+ import type { i18nJsonSchema, ErrorTranslator, Translator } from './i18nTypes';
6
+ import {
7
+ ArrayDefaultTranslation,
8
+ ArrayTranslations,
9
+ } from './arrayTranslations';
6
10
 
7
11
  export const getI18nKeyPrefixBySchema = (
8
12
  schema: i18nJsonSchema | undefined,
@@ -22,7 +26,7 @@ export const transformPathToI18nPrefix = (path: string): string => {
22
26
  return (
23
27
  path
24
28
  ?.split('.')
25
- .filter(segment => !/^\d+$/.test(segment))
29
+ .filter((segment) => !/^\d+$/.test(segment))
26
30
  .join('.') || 'root'
27
31
  );
28
32
  };
@@ -47,7 +51,17 @@ export const getI18nKey = (
47
51
  return `${getI18nKeyPrefix(schema, uischema, path)}.${key}`;
48
52
  };
49
53
 
50
- export const defaultTranslator: Translator = (_id: string, defaultMessage: string | undefined) => defaultMessage;
54
+ export const addI18nKeyToPrefix = (
55
+ i18nKeyPrefix: string,
56
+ key: string
57
+ ): string => {
58
+ return `${i18nKeyPrefix}.${key}`;
59
+ };
60
+
61
+ export const defaultTranslator: Translator = (
62
+ _id: string,
63
+ defaultMessage: string | undefined
64
+ ) => defaultMessage;
51
65
 
52
66
  export const defaultErrorTranslator: ErrorTranslator = (error, t, uischema) => {
53
67
  // check whether there is a special keyword message
@@ -57,13 +71,15 @@ export const defaultErrorTranslator: ErrorTranslator = (error, t, uischema) => {
57
71
  getControlPath(error),
58
72
  `error.${error.keyword}`
59
73
  );
60
- const specializedKeywordMessage = t(i18nKey, undefined, { error } );
74
+ const specializedKeywordMessage = t(i18nKey, undefined, { error });
61
75
  if (specializedKeywordMessage !== undefined) {
62
76
  return specializedKeywordMessage;
63
77
  }
64
78
 
65
79
  // check whether there is a generic keyword message
66
- const genericKeywordMessage = t(`error.${error.keyword}`, undefined, { error });
80
+ const genericKeywordMessage = t(`error.${error.keyword}`, undefined, {
81
+ error,
82
+ });
67
83
  if (genericKeywordMessage !== undefined) {
68
84
  return genericKeywordMessage;
69
85
  }
@@ -75,7 +91,10 @@ export const defaultErrorTranslator: ErrorTranslator = (error, t, uischema) => {
75
91
  }
76
92
 
77
93
  // rewrite required property messages (if they were not customized) as we place them next to the respective input
78
- if (error.keyword === 'required' && error.message?.startsWith('must have required property')) {
94
+ if (
95
+ error.keyword === 'required' &&
96
+ error.message?.startsWith('must have required property')
97
+ ) {
79
98
  return t('is a required property', 'is a required property', { error });
80
99
  }
81
100
 
@@ -97,29 +116,60 @@ export const getCombinedErrorMessage = (
97
116
  if (errors.length > 0 && t) {
98
117
  // check whether there is a special message which overwrites all others
99
118
  const customErrorKey = getI18nKey(schema, uischema, path, 'error.custom');
100
- const specializedErrorMessage = t(customErrorKey, undefined, {schema, uischema, path, errors});
119
+ const specializedErrorMessage = t(customErrorKey, undefined, {
120
+ schema,
121
+ uischema,
122
+ path,
123
+ errors,
124
+ });
101
125
  if (specializedErrorMessage !== undefined) {
102
126
  return specializedErrorMessage;
103
127
  }
104
128
  }
105
- return formatErrorMessage(
106
- errors.map(error => et(error, t, uischema))
107
- );
129
+ return formatErrorMessage(errors.map((error) => et(error, t, uischema)));
108
130
  };
109
131
 
110
132
  /**
111
133
  * This can be used to internationalize the label of the given Labelable (e.g. UI Schema elements).
112
134
  * This should not be used for controls as there we have additional context in the form of the JSON Schema available.
113
135
  */
114
- export const deriveLabelForUISchemaElement = (uischema: Labelable<boolean>, t: Translator): string | undefined => {
136
+ export const deriveLabelForUISchemaElement = (
137
+ uischema: Labelable<boolean>,
138
+ t: Translator
139
+ ): string | undefined => {
115
140
  if (uischema.label === false) {
116
141
  return undefined;
117
142
  }
118
- if ((uischema.label === undefined || uischema.label === null || uischema.label === true) && !isInternationalized(uischema)) {
143
+ if (
144
+ (uischema.label === undefined ||
145
+ uischema.label === null ||
146
+ uischema.label === true) &&
147
+ !isInternationalized(uischema)
148
+ ) {
119
149
  return undefined;
120
150
  }
121
- const stringifiedLabel = typeof uischema.label === 'string' ? uischema.label : JSON.stringify(uischema.label);
151
+ const stringifiedLabel =
152
+ typeof uischema.label === 'string'
153
+ ? uischema.label
154
+ : JSON.stringify(uischema.label);
122
155
  const i18nKeyPrefix = getI18nKeyPrefixBySchema(undefined, uischema);
123
- const i18nKey = typeof i18nKeyPrefix === 'string' ? `${i18nKeyPrefix}.label` : stringifiedLabel;
156
+ const i18nKey =
157
+ typeof i18nKeyPrefix === 'string'
158
+ ? `${i18nKeyPrefix}.label`
159
+ : stringifiedLabel;
124
160
  return t(i18nKey, stringifiedLabel, { uischema: uischema });
125
- }
161
+ };
162
+
163
+ export const getArrayTranslations = (
164
+ t: Translator,
165
+ defaultTranslations: ArrayDefaultTranslation[],
166
+ i18nKeyPrefix: string,
167
+ label: string
168
+ ): ArrayTranslations => {
169
+ const translations: ArrayTranslations = {};
170
+ defaultTranslations.forEach((controlElement) => {
171
+ const key = addI18nKeyToPrefix(i18nKeyPrefix, controlElement.key);
172
+ translations[controlElement.key] = t(key, controlElement.default(label));
173
+ });
174
+ return translations;
175
+ };
package/src/i18n/index.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './i18nTypes';
2
2
  export * from './i18nUtil';
3
+ export * from './arrayTranslations';
@@ -31,14 +31,14 @@ export const Draft4 = {
31
31
  schemaArray: {
32
32
  type: 'array',
33
33
  minItems: 1,
34
- items: { $ref: '#' }
34
+ items: { $ref: '#' },
35
35
  },
36
36
  positiveInteger: {
37
37
  type: 'integer',
38
- minimum: 0
38
+ minimum: 0,
39
39
  },
40
40
  positiveIntegerDefault0: {
41
- allOf: [{ $ref: '#/definitions/positiveInteger' }, { default: 0 }]
41
+ allOf: [{ $ref: '#/definitions/positiveInteger' }, { default: 0 }],
42
42
  },
43
43
  simpleTypes: {
44
44
  enum: [
@@ -48,104 +48,104 @@ export const Draft4 = {
48
48
  'null',
49
49
  'number',
50
50
  'object',
51
- 'string'
52
- ]
51
+ 'string',
52
+ ],
53
53
  },
54
54
  stringArray: {
55
55
  type: 'array',
56
56
  items: { type: 'string' },
57
57
  minItems: 1,
58
- uniqueItems: true
59
- }
58
+ uniqueItems: true,
59
+ },
60
60
  },
61
61
  type: 'object',
62
62
  properties: {
63
63
  id: {
64
64
  type: 'string',
65
- format: 'uri'
65
+ format: 'uri',
66
66
  },
67
67
  $schema: {
68
68
  type: 'string',
69
- format: 'uri'
69
+ format: 'uri',
70
70
  },
71
71
  title: {
72
- type: 'string'
72
+ type: 'string',
73
73
  },
74
74
  description: {
75
- type: 'string'
75
+ type: 'string',
76
76
  },
77
77
  default: {},
78
78
  multipleOf: {
79
79
  type: 'number',
80
80
  minimum: 0,
81
- exclusiveMinimum: true
81
+ exclusiveMinimum: true,
82
82
  },
83
83
  maximum: {
84
- type: 'number'
84
+ type: 'number',
85
85
  },
86
86
  exclusiveMaximum: {
87
87
  type: 'boolean',
88
- default: false
88
+ default: false,
89
89
  },
90
90
  minimum: {
91
- type: 'number'
91
+ type: 'number',
92
92
  },
93
93
  exclusiveMinimum: {
94
94
  type: 'boolean',
95
- default: false
95
+ default: false,
96
96
  },
97
97
  maxLength: { $ref: '#/definitions/positiveInteger' },
98
98
  minLength: { $ref: '#/definitions/positiveIntegerDefault0' },
99
99
  pattern: {
100
100
  type: 'string',
101
- format: 'regex'
101
+ format: 'regex',
102
102
  },
103
103
  additionalItems: {
104
104
  anyOf: [{ type: 'boolean' }, { $ref: '#' }],
105
- default: {}
105
+ default: {},
106
106
  },
107
107
  items: {
108
108
  anyOf: [{ $ref: '#' }, { $ref: '#/definitions/schemaArray' }],
109
- default: {}
109
+ default: {},
110
110
  },
111
111
  maxItems: { $ref: '#/definitions/positiveInteger' },
112
112
  minItems: { $ref: '#/definitions/positiveIntegerDefault0' },
113
113
  uniqueItems: {
114
114
  type: 'boolean',
115
- default: false
115
+ default: false,
116
116
  },
117
117
  maxProperties: { $ref: '#/definitions/positiveInteger' },
118
118
  minProperties: { $ref: '#/definitions/positiveIntegerDefault0' },
119
119
  required: { $ref: '#/definitions/stringArray' },
120
120
  additionalProperties: {
121
121
  anyOf: [{ type: 'boolean' }, { $ref: '#' }],
122
- default: {}
122
+ default: {},
123
123
  },
124
124
  definitions: {
125
125
  type: 'object',
126
126
  additionalProperties: { $ref: '#' },
127
- default: {}
127
+ default: {},
128
128
  },
129
129
  properties: {
130
130
  type: 'object',
131
131
  additionalProperties: { $ref: '#' },
132
- default: {}
132
+ default: {},
133
133
  },
134
134
  patternProperties: {
135
135
  type: 'object',
136
136
  additionalProperties: { $ref: '#' },
137
- default: {}
137
+ default: {},
138
138
  },
139
139
  dependencies: {
140
140
  type: 'object',
141
141
  additionalProperties: {
142
- anyOf: [{ $ref: '#' }, { $ref: '#/definitions/stringArray' }]
143
- }
142
+ anyOf: [{ $ref: '#' }, { $ref: '#/definitions/stringArray' }],
143
+ },
144
144
  },
145
145
  enum: {
146
146
  type: 'array',
147
147
  minItems: 1,
148
- uniqueItems: true
148
+ uniqueItems: true,
149
149
  },
150
150
  type: {
151
151
  anyOf: [
@@ -154,18 +154,18 @@ export const Draft4 = {
154
154
  type: 'array',
155
155
  items: { $ref: '#/definitions/simpleTypes' },
156
156
  minItems: 1,
157
- uniqueItems: true
158
- }
159
- ]
157
+ uniqueItems: true,
158
+ },
159
+ ],
160
160
  },
161
161
  allOf: { $ref: '#/definitions/schemaArray' },
162
162
  anyOf: { $ref: '#/definitions/schemaArray' },
163
163
  oneOf: { $ref: '#/definitions/schemaArray' },
164
- not: { $ref: '#' }
164
+ not: { $ref: '#' },
165
165
  },
166
166
  dependencies: {
167
167
  exclusiveMaximum: ['maximum'],
168
- exclusiveMinimum: ['minimum']
168
+ exclusiveMinimum: ['minimum'],
169
169
  },
170
- default: {}
170
+ default: {},
171
171
  };
@@ -23,7 +23,7 @@
23
23
  THE SOFTWARE.
24
24
  */
25
25
 
26
- import { JsonSchema4 } from './jsonSchema4';
27
- import { JsonSchema7 } from './jsonSchema7';
26
+ import type { JsonSchema4 } from './jsonSchema4';
27
+ import type { JsonSchema7 } from './jsonSchema7';
28
28
 
29
29
  export type JsonSchema = JsonSchema4 | JsonSchema7;
@@ -23,7 +23,7 @@
23
23
  THE SOFTWARE.
24
24
  */
25
25
 
26
- import { JsonSchema } from './jsonSchema';
26
+ import type { JsonSchema } from './jsonSchema';
27
27
 
28
28
  /**
29
29
  * Interface for describing an UI schema element that is referencing
@@ -54,7 +54,7 @@ export interface Labelable<T = string> {
54
54
  /**
55
55
  * Label for UI schema element.
56
56
  */
57
- label?: string|T;
57
+ label?: string | T;
58
58
  }
59
59
 
60
60
  /**
@@ -108,7 +108,7 @@ export enum RuleEffect {
108
108
  /**
109
109
  * Effect that disables the associated element.
110
110
  */
111
- DISABLE = 'DISABLE'
111
+ DISABLE = 'DISABLE',
112
112
  }
113
113
 
114
114
  /**
@@ -240,7 +240,11 @@ export interface LabelElement extends UISchemaElement, Internationalizable {
240
240
  * A control element. The scope property of the control determines
241
241
  * to which part of the schema the control should be bound.
242
242
  */
243
- export interface ControlElement extends UISchemaElement, Scoped, Labelable<string | boolean | LabelDescription>, Internationalizable {
243
+ export interface ControlElement
244
+ extends UISchemaElement,
245
+ Scoped,
246
+ Labelable<string | boolean | LabelDescription>,
247
+ Internationalizable {
244
248
  type: 'Control';
245
249
  }
246
250
 
@@ -256,7 +260,10 @@ export interface Category extends Layout, Labeled, Internationalizable {
256
260
  * A child element may either be itself a Categorization or a Category, hence
257
261
  * the categorization element can be used to represent recursive structures like trees.
258
262
  */
259
- export interface Categorization extends UISchemaElement, Labeled, Internationalizable {
263
+ export interface Categorization
264
+ extends UISchemaElement,
265
+ Labeled,
266
+ Internationalizable {
260
267
  type: 'Categorization';
261
268
  /**
262
269
  * The child elements of this categorization which are either of type
@@ -265,8 +272,12 @@ export interface Categorization extends UISchemaElement, Labeled, Internationali
265
272
  elements: (Category | Categorization)[];
266
273
  }
267
274
 
268
- export const isInternationalized = (element: unknown): element is Required<Internationalizable> =>
269
- typeof element === 'object' && element !== null && typeof (element as Internationalizable).i18n === 'string';
275
+ export const isInternationalized = (
276
+ element: unknown
277
+ ): element is Required<Internationalizable> =>
278
+ typeof element === 'object' &&
279
+ element !== null &&
280
+ typeof (element as Internationalizable).i18n === 'string';
270
281
 
271
282
  export const isGroup = (layout: Layout): layout is GroupLayout =>
272
283
  layout.type === 'Group';