@jsonforms/core 3.2.0-alpha.2 → 3.2.0-alpha.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.
@@ -366,13 +366,6 @@ const initState = {
366
366
  validationMode: 'ValidateAndShow',
367
367
  additionalErrors: [],
368
368
  };
369
- const reuseAjvForSchema = (ajv, schema) => {
370
- if (Object.prototype.hasOwnProperty.call(schema, 'id') ||
371
- Object.prototype.hasOwnProperty.call(schema, '$id')) {
372
- ajv.removeSchema(schema);
373
- }
374
- return ajv;
375
- };
376
369
  const getOrCreateAjv = (state, action) => {
377
370
  if (action) {
378
371
  if (hasAjvOption(action.options)) {
@@ -384,12 +377,7 @@ const getOrCreateAjv = (state, action) => {
384
377
  }
385
378
  }
386
379
  }
387
- if (state.ajv) {
388
- return action?.schema
389
- ? reuseAjvForSchema(state.ajv, action.schema)
390
- : state.ajv;
391
- }
392
- return createAjv();
380
+ return state.ajv ? state.ajv : createAjv();
393
381
  };
394
382
  const hasAjvOption = (option) => {
395
383
  if (option) {
@@ -498,7 +486,7 @@ const coreReducer = (state = initState, action) => {
498
486
  case SET_SCHEMA: {
499
487
  const needsNewValidator = action.schema && state.ajv && state.validationMode !== 'NoValidation';
500
488
  const v = needsNewValidator
501
- ? reuseAjvForSchema(state.ajv, action.schema).compile(action.schema)
489
+ ? state.ajv.compile(action.schema)
502
490
  : state.validator;
503
491
  const errors = validate(v, state.data);
504
492
  return {
@@ -564,7 +552,7 @@ const coreReducer = (state = initState, action) => {
564
552
  };
565
553
  }
566
554
  if (state.validationMode === 'NoValidation') {
567
- const validator = reuseAjvForSchema(state.ajv, state.schema).compile(state.schema);
555
+ const validator = state.ajv.compile(state.schema);
568
556
  const errors = validate(validator, state.data);
569
557
  return {
570
558
  ...state,
@@ -1171,7 +1159,7 @@ const findUISchema = (uischemas, schema, schemaPath, path, fallback = 'VerticalL
1171
1159
  if (typeof fallback === 'function') {
1172
1160
  return fallback();
1173
1161
  }
1174
- return Generate.uiSchema(schema, fallback);
1162
+ return Generate.uiSchema(schema, fallback, undefined, rootSchema);
1175
1163
  }
1176
1164
  }
1177
1165
  else if (typeof control.options.detail === 'object') {
@@ -1441,6 +1429,19 @@ const isInherentlyEnabled = (state, ownProps, uischema, schema, rootData, config
1441
1429
  return true;
1442
1430
  };
1443
1431
 
1432
+ const convertDateToString = (date, format) => {
1433
+ const dateString = date.toISOString();
1434
+ if (format === 'date-time') {
1435
+ return dateString;
1436
+ }
1437
+ else if (format === 'date') {
1438
+ return dateString.split('T')[0];
1439
+ }
1440
+ else if (format === 'time') {
1441
+ return dateString.split('T')[1].split('.')[0];
1442
+ }
1443
+ return dateString;
1444
+ };
1444
1445
  const convertToValidClassName = (s) => s.replace('#', 'root').replace(new RegExp('/', 'g'), '_');
1445
1446
  const formatErrorMessage = (errors) => {
1446
1447
  if (errors === undefined || errors === null) {
@@ -1558,27 +1559,54 @@ const computeLabel = (label, required, hideRequiredAsterisk) => {
1558
1559
  const showAsRequired = (required, hideRequiredAsterisk) => {
1559
1560
  return required && !hideRequiredAsterisk;
1560
1561
  };
1561
- const createDefaultValue = (schema) => {
1562
- switch (schema.type) {
1563
- case 'string':
1564
- if (schema.format === 'date-time' ||
1565
- schema.format === 'date' ||
1566
- schema.format === 'time') {
1567
- return new Date();
1562
+ const createDefaultValue = (schema, rootSchema) => {
1563
+ const resolvedSchema = Resolve.schema(schema, schema.$ref, rootSchema);
1564
+ if (resolvedSchema.default !== undefined) {
1565
+ return extractDefaults(resolvedSchema, rootSchema);
1566
+ }
1567
+ if (hasType(resolvedSchema, 'string')) {
1568
+ if (resolvedSchema.format === 'date-time' ||
1569
+ resolvedSchema.format === 'date' ||
1570
+ resolvedSchema.format === 'time') {
1571
+ return convertDateToString(new Date(), resolvedSchema.format);
1572
+ }
1573
+ return '';
1574
+ }
1575
+ else if (hasType(resolvedSchema, 'integer') ||
1576
+ hasType(resolvedSchema, 'number')) {
1577
+ return 0;
1578
+ }
1579
+ else if (hasType(resolvedSchema, 'boolean')) {
1580
+ return false;
1581
+ }
1582
+ else if (hasType(resolvedSchema, 'array')) {
1583
+ return [];
1584
+ }
1585
+ else if (hasType(resolvedSchema, 'object')) {
1586
+ return extractDefaults(resolvedSchema, rootSchema);
1587
+ }
1588
+ else if (hasType(resolvedSchema, 'null')) {
1589
+ return null;
1590
+ }
1591
+ else {
1592
+ return {};
1593
+ }
1594
+ };
1595
+ const extractDefaults = (schema, rootSchema) => {
1596
+ if (hasType(schema, 'object') && schema.default === undefined) {
1597
+ const result = {};
1598
+ for (const key in schema.properties) {
1599
+ const property = schema.properties[key];
1600
+ const resolvedProperty = property.$ref
1601
+ ? Resolve.schema(rootSchema, property.$ref, rootSchema)
1602
+ : property;
1603
+ if (resolvedProperty.default !== undefined) {
1604
+ result[key] = cloneDeep(resolvedProperty.default);
1568
1605
  }
1569
- return '';
1570
- case 'integer':
1571
- case 'number':
1572
- return 0;
1573
- case 'boolean':
1574
- return false;
1575
- case 'array':
1576
- return [];
1577
- case 'null':
1578
- return null;
1579
- default:
1580
- return {};
1606
+ }
1607
+ return result;
1581
1608
  }
1609
+ return cloneDeep(schema.default);
1582
1610
  };
1583
1611
  const isDescriptionHidden = (visible, description, isFocused, showUnfocusedDescription) => {
1584
1612
  return (description === undefined ||
@@ -2107,6 +2135,7 @@ const createAjv = (options) => {
2107
2135
  allErrors: true,
2108
2136
  verbose: true,
2109
2137
  strict: false,
2138
+ addUsedSchema: false,
2110
2139
  ...options,
2111
2140
  });
2112
2141
  addFormats(ajv);
@@ -2382,5 +2411,5 @@ const Helpers = {
2382
2411
  convertToValidClassName,
2383
2412
  };
2384
2413
 
2385
- export { ADD_CELL, ADD_DEFAULT_DATA, ADD_RENDERER, ADD_UI_SCHEMA, index as Actions, ArrayTranslationEnum, CombinatorTranslationEnum, Draft4, Generate, Helpers, INIT, NOT_APPLICABLE, Paths, REMOVE_CELL, REMOVE_DEFAULT_DATA, REMOVE_RENDERER, REMOVE_UI_SCHEMA, Resolve, RuleEffect, Runtime, SET_AJV, SET_CONFIG, SET_LOCALE, SET_SCHEMA, SET_TRANSLATOR, SET_UISCHEMA, SET_VALIDATION_MODE, index$1 as Test, UPDATE_CORE, UPDATE_DATA, UPDATE_ERRORS, UPDATE_I18N, VALIDATE, addI18nKeyToPrefix, and, arrayDefaultTranslations, categorizationHasCategory, cellReducer, clearAllIds, combinatorDefaultTranslations, compose, compose as composePaths, composeWithUi, computeLabel, configReducer, controlDefaultProps, convertToValidClassName, coreReducer, createAjv, createCleanLabel, createCombinatorRenderInfos, createControlElement, createDefaultValue, createId, createLabelDescriptionFrom, decode, defaultDataReducer, defaultErrorTranslator, defaultJsonFormsI18nState, defaultMapDispatchToControlProps, defaultMapStateToEnumCellProps, defaultTranslator, deriveLabelForUISchemaElement, deriveTypes, encode, enumToEnumOptionMapper, errorAt, errorsAt, evalEnablement, evalVisibility, extractAjv, extractData, extractDefaultData, extractSchema, extractUiSchema, fetchErrorTranslator, fetchLocale, fetchTranslator, findAllRefs, findMatchingUISchema, findUISchema, formatErrorMessage, formatIs, generateDefaultUISchema, generateJsonSchema, getAjv, getArrayTranslations, getCells, getCombinatorTranslations, getCombinedErrorMessage, getConfig, getControlPath, getData, getDefaultData, getErrorAt, getErrorTranslator, getFirstPrimitiveProp, getI18nKey, getI18nKeyPrefix, getI18nKeyPrefixBySchema, getLocale, getRenderers, getSchema, getSubErrorsAt, getTranslator, getUISchemas, getUiSchema, hasCategory, hasEnableRule, hasShowRule, hasType, i18nReducer, init, isAllOfControl, isAnyOfControl, isArrayObjectControl, isBooleanControl, isCategorization, isCategory, isControl, isDateControl, isDateTimeControl, isDescriptionHidden, isEnabled, isEnumControl, isGroup, isInherentlyEnabled, isIntegerControl, isInternationalized, isLabelable, isLabeled, isLayout, isMultiLineControl, isNumberControl, isNumberFormatControl, isObjectArray, isObjectArrayControl, isObjectArrayWithNesting, isObjectControl, isOneOfControl, isOneOfEnumControl, isOneOfEnumSchema, isPrimitiveArrayControl, isRangeControl, isScopable, isScoped, isStringControl, isTimeControl, isVisible, iterateSchema, jsonFormsReducerConfig, layoutDefaultProps, mapDispatchToArrayControlProps, mapDispatchToCellProps, mapDispatchToControlProps, mapDispatchToMultiEnumProps, mapStateToAllOfProps, mapStateToAnyOfProps, mapStateToArrayControlProps, mapStateToArrayLayoutProps, mapStateToCellProps, mapStateToCombinatorRendererProps, mapStateToControlProps, mapStateToControlWithDetailProps, mapStateToDispatchCellProps, mapStateToEnumControlProps, mapStateToJsonFormsRendererProps, mapStateToLabelProps, mapStateToLayoutProps, mapStateToMasterListItemProps, mapStateToMultiEnumControlProps, mapStateToOneOfEnumCellProps, mapStateToOneOfEnumControlProps, mapStateToOneOfProps, moveDown, moveUp, not, oneOfToEnumOptionMapper, optionIs, or, rankWith, registerCell, registerDefaultData, registerRenderer, registerUISchema, removeId, rendererReducer, resolveData, resolveSchema, schemaMatches, schemaSubPathMatches, schemaTypeIs, scopeEndIs, scopeEndsWith, setAjv, setConfig, setLocale, setReadonly, setSchema, setTranslator, setUISchema, setValidationMode, showAsRequired, subErrorsAt, toDataPath, toDataPathSegments, transformPathToI18nPrefix, uiTypeIs, uischemaRegistryReducer, unregisterCell, unregisterDefaultData, unregisterRenderer, unregisterUISchema, unsetReadonly, update, updateCore, updateErrors, updateI18n, validate, withIncreasedRank };
2414
+ export { ADD_CELL, ADD_DEFAULT_DATA, ADD_RENDERER, ADD_UI_SCHEMA, index as Actions, ArrayTranslationEnum, CombinatorTranslationEnum, Draft4, Generate, Helpers, INIT, NOT_APPLICABLE, Paths, REMOVE_CELL, REMOVE_DEFAULT_DATA, REMOVE_RENDERER, REMOVE_UI_SCHEMA, Resolve, RuleEffect, Runtime, SET_AJV, SET_CONFIG, SET_LOCALE, SET_SCHEMA, SET_TRANSLATOR, SET_UISCHEMA, SET_VALIDATION_MODE, index$1 as Test, UPDATE_CORE, UPDATE_DATA, UPDATE_ERRORS, UPDATE_I18N, VALIDATE, addI18nKeyToPrefix, and, arrayDefaultTranslations, categorizationHasCategory, cellReducer, clearAllIds, combinatorDefaultTranslations, compose, compose as composePaths, composeWithUi, computeLabel, configReducer, controlDefaultProps, convertDateToString, convertToValidClassName, coreReducer, createAjv, createCleanLabel, createCombinatorRenderInfos, createControlElement, createDefaultValue, createId, createLabelDescriptionFrom, decode, defaultDataReducer, defaultErrorTranslator, defaultJsonFormsI18nState, defaultMapDispatchToControlProps, defaultMapStateToEnumCellProps, defaultTranslator, deriveLabelForUISchemaElement, deriveTypes, encode, enumToEnumOptionMapper, errorAt, errorsAt, evalEnablement, evalVisibility, extractAjv, extractData, extractDefaultData, extractDefaults, extractSchema, extractUiSchema, fetchErrorTranslator, fetchLocale, fetchTranslator, findAllRefs, findMatchingUISchema, findUISchema, formatErrorMessage, formatIs, generateDefaultUISchema, generateJsonSchema, getAjv, getArrayTranslations, getCells, getCombinatorTranslations, getCombinedErrorMessage, getConfig, getControlPath, getData, getDefaultData, getErrorAt, getErrorTranslator, getFirstPrimitiveProp, getI18nKey, getI18nKeyPrefix, getI18nKeyPrefixBySchema, getLocale, getRenderers, getSchema, getSubErrorsAt, getTranslator, getUISchemas, getUiSchema, hasCategory, hasEnableRule, hasShowRule, hasType, i18nReducer, init, isAllOfControl, isAnyOfControl, isArrayObjectControl, isBooleanControl, isCategorization, isCategory, isControl, isDateControl, isDateTimeControl, isDescriptionHidden, isEnabled, isEnumControl, isGroup, isInherentlyEnabled, isIntegerControl, isInternationalized, isLabelable, isLabeled, isLayout, isMultiLineControl, isNumberControl, isNumberFormatControl, isObjectArray, isObjectArrayControl, isObjectArrayWithNesting, isObjectControl, isOneOfControl, isOneOfEnumControl, isOneOfEnumSchema, isPrimitiveArrayControl, isRangeControl, isScopable, isScoped, isStringControl, isTimeControl, isVisible, iterateSchema, jsonFormsReducerConfig, layoutDefaultProps, mapDispatchToArrayControlProps, mapDispatchToCellProps, mapDispatchToControlProps, mapDispatchToMultiEnumProps, mapStateToAllOfProps, mapStateToAnyOfProps, mapStateToArrayControlProps, mapStateToArrayLayoutProps, mapStateToCellProps, mapStateToCombinatorRendererProps, mapStateToControlProps, mapStateToControlWithDetailProps, mapStateToDispatchCellProps, mapStateToEnumControlProps, mapStateToJsonFormsRendererProps, mapStateToLabelProps, mapStateToLayoutProps, mapStateToMasterListItemProps, mapStateToMultiEnumControlProps, mapStateToOneOfEnumCellProps, mapStateToOneOfEnumControlProps, mapStateToOneOfProps, moveDown, moveUp, not, oneOfToEnumOptionMapper, optionIs, or, rankWith, registerCell, registerDefaultData, registerRenderer, registerUISchema, removeId, rendererReducer, resolveData, resolveSchema, schemaMatches, schemaSubPathMatches, schemaTypeIs, scopeEndIs, scopeEndsWith, setAjv, setConfig, setLocale, setReadonly, setSchema, setTranslator, setUISchema, setValidationMode, showAsRequired, subErrorsAt, toDataPath, toDataPathSegments, transformPathToI18nPrefix, uiTypeIs, uischemaRegistryReducer, unregisterCell, unregisterDefaultData, unregisterRenderer, unregisterUISchema, unsetReadonly, update, updateCore, updateErrors, updateI18n, validate, withIncreasedRank };
2386
2415
  //# sourceMappingURL=jsonforms-core.esm.js.map