@jsonforms/core
Advanced tools
Comparing version 3.2.0-alpha.2 to 3.2.0-alpha.3
@@ -30,8 +30,14 @@ import { ControlElement, JsonSchema, LabelElement, UISchemaElement } from '../models'; | ||
/** | ||
* Create a default value based on the given scheam. | ||
* Create a default value based on the given schema. | ||
* @param schema the schema for which to create a default value. | ||
* @returns {any} | ||
*/ | ||
export declare const createDefaultValue: (schema: JsonSchema) => {}; | ||
export declare const createDefaultValue: (schema: JsonSchema, rootSchema: JsonSchema) => any; | ||
/** | ||
* Returns the default value defined in the given schema. | ||
* @param schema the schema for which to create a default value. | ||
* @returns {any} | ||
*/ | ||
export declare const extractDefaults: (schema: JsonSchema, rootSchema: JsonSchema) => any; | ||
/** | ||
* Whether an element's description should be hidden. | ||
@@ -38,0 +44,0 @@ * |
import type { JsonSchema, Scoped, UISchemaElement } from '..'; | ||
import type Ajv from 'ajv'; | ||
/** | ||
* Returns the string representation of the given date. The format of the output string can be specified: | ||
* - 'date' for a date-only string (YYYY-MM-DD), | ||
* - 'time' for a time-only string (HH:mm:ss), or | ||
* - 'date-time' for a full date-time string in ISO format (YYYY-MM-DDTHH:mm:ss.sssZ). | ||
* If no format is specified, the full date-time ISO string is returned by default. | ||
* | ||
* @returns {string} A string representation of the date in the specified format. | ||
* | ||
* @example | ||
* // returns '2023-11-09' | ||
* convertDateToString(new Date('2023-11-09T14:22:54.131Z'), 'date'); | ||
* | ||
* @example | ||
* // returns '14:22:54' | ||
* convertDateToString(new Date('2023-11-09T14:22:54.131Z'), 'time'); | ||
* | ||
* @example | ||
* // returns '2023-11-09T14:22:54.131Z' | ||
* convertDateToString(new Date('2023-11-09T14:22:54.131Z'), 'date-time'); | ||
* | ||
* @example | ||
* // returns '2023-11-09T14:22:54.131Z' | ||
* convertDateToString(new Date('2023-11-09T14:22:54.131Z')); | ||
*/ | ||
export declare const convertDateToString: (date: Date, format?: 'date' | 'time' | 'date-time') => string; | ||
/** | ||
* Escape the given string such that it can be used as a class name, | ||
@@ -5,0 +31,0 @@ * i.e. hashes and slashes will be replaced. |
{ | ||
"name": "@jsonforms/core", | ||
"version": "3.2.0-alpha.2", | ||
"version": "3.2.0-alpha.3", | ||
"description": "Core module of JSON Forms", | ||
@@ -102,3 +102,3 @@ "repository": "https://github.com/eclipsesource/jsonforms", | ||
}, | ||
"gitHead": "f922f48cb7548cefd8f1acd939a9afbd14b36f9b" | ||
"gitHead": "77624c6ab6c0f3db98abe3175547bbd51f7510bb" | ||
} |
@@ -93,12 +93,2 @@ /* | ||
const reuseAjvForSchema = (ajv: Ajv, schema: JsonSchema): Ajv => { | ||
if ( | ||
Object.prototype.hasOwnProperty.call(schema, 'id') || | ||
Object.prototype.hasOwnProperty.call(schema, '$id') | ||
) { | ||
ajv.removeSchema(schema); | ||
} | ||
return ajv; | ||
}; | ||
const getOrCreateAjv = ( | ||
@@ -119,8 +109,3 @@ state: JsonFormsCore, | ||
} | ||
if (state.ajv) { | ||
return action?.schema | ||
? reuseAjvForSchema(state.ajv, action.schema) | ||
: state.ajv; | ||
} | ||
return createAjv(); | ||
return state.ajv ? state.ajv : createAjv(); | ||
}; | ||
@@ -260,3 +245,3 @@ | ||
const v = needsNewValidator | ||
? reuseAjvForSchema(state.ajv, action.schema).compile(action.schema) | ||
? state.ajv.compile(action.schema) | ||
: state.validator; | ||
@@ -332,5 +317,3 @@ const errors = validate(v, state.data); | ||
if (state.validationMode === 'NoValidation') { | ||
const validator = reuseAjvForSchema(state.ajv, state.schema).compile( | ||
state.schema | ||
); | ||
const validator = state.ajv.compile(state.schema); | ||
const errors = validate(validator, state.data); | ||
@@ -337,0 +320,0 @@ return { |
@@ -80,3 +80,3 @@ /* | ||
// force generation of uischema | ||
return Generate.uiSchema(schema, fallback); | ||
return Generate.uiSchema(schema, fallback, undefined, rootSchema); | ||
} | ||
@@ -83,0 +83,0 @@ } else if (typeof control.options.detail === 'object') { |
@@ -60,3 +60,3 @@ /* | ||
import type { AnyAction, Dispatch } from './type'; | ||
import { Resolve } from './util'; | ||
import { Resolve, convertDateToString, hasType } from './util'; | ||
import { composePaths, composeWithUi } from './path'; | ||
@@ -83,2 +83,3 @@ import { CoreActions, update } from '../actions'; | ||
import { resolveSchema } from './resolvers'; | ||
import cloneDeep from 'lodash/cloneDeep'; | ||
@@ -143,29 +144,61 @@ const isRequired = ( | ||
/** | ||
* Create a default value based on the given scheam. | ||
* Create a default value based on the given schema. | ||
* @param schema the schema for which to create a default value. | ||
* @returns {any} | ||
*/ | ||
export const createDefaultValue = (schema: JsonSchema) => { | ||
switch (schema.type) { | ||
case 'string': | ||
if ( | ||
schema.format === 'date-time' || | ||
schema.format === 'date' || | ||
schema.format === 'time' | ||
) { | ||
return new Date(); | ||
export const createDefaultValue = ( | ||
schema: JsonSchema, | ||
rootSchema: JsonSchema | ||
) => { | ||
const resolvedSchema = Resolve.schema(schema, schema.$ref, rootSchema); | ||
if (resolvedSchema.default !== undefined) { | ||
return extractDefaults(resolvedSchema, rootSchema); | ||
} | ||
if (hasType(resolvedSchema, 'string')) { | ||
if ( | ||
resolvedSchema.format === 'date-time' || | ||
resolvedSchema.format === 'date' || | ||
resolvedSchema.format === 'time' | ||
) { | ||
return convertDateToString(new Date(), resolvedSchema.format); | ||
} | ||
return ''; | ||
} else if ( | ||
hasType(resolvedSchema, 'integer') || | ||
hasType(resolvedSchema, 'number') | ||
) { | ||
return 0; | ||
} else if (hasType(resolvedSchema, 'boolean')) { | ||
return false; | ||
} else if (hasType(resolvedSchema, 'array')) { | ||
return []; | ||
} else if (hasType(resolvedSchema, 'object')) { | ||
return extractDefaults(resolvedSchema, rootSchema); | ||
} else if (hasType(resolvedSchema, 'null')) { | ||
return null; | ||
} else { | ||
return {}; | ||
} | ||
}; | ||
/** | ||
* Returns the default value defined in the given schema. | ||
* @param schema the schema for which to create a default value. | ||
* @returns {any} | ||
*/ | ||
export const extractDefaults = (schema: JsonSchema, rootSchema: JsonSchema) => { | ||
if (hasType(schema, 'object') && schema.default === undefined) { | ||
const result: { [key: string]: any } = {}; | ||
for (const key in schema.properties) { | ||
const property = schema.properties[key]; | ||
const resolvedProperty = property.$ref | ||
? Resolve.schema(rootSchema, property.$ref, rootSchema) | ||
: property; | ||
if (resolvedProperty.default !== undefined) { | ||
result[key] = cloneDeep(resolvedProperty.default); | ||
} | ||
return ''; | ||
case 'integer': | ||
case 'number': | ||
return 0; | ||
case 'boolean': | ||
return false; | ||
case 'array': | ||
return []; | ||
case 'null': | ||
return null; | ||
default: | ||
return {}; | ||
} | ||
return result; | ||
} | ||
return cloneDeep(schema.default); | ||
}; | ||
@@ -172,0 +205,0 @@ |
@@ -37,2 +37,45 @@ /* | ||
/** | ||
* Returns the string representation of the given date. The format of the output string can be specified: | ||
* - 'date' for a date-only string (YYYY-MM-DD), | ||
* - 'time' for a time-only string (HH:mm:ss), or | ||
* - 'date-time' for a full date-time string in ISO format (YYYY-MM-DDTHH:mm:ss.sssZ). | ||
* If no format is specified, the full date-time ISO string is returned by default. | ||
* | ||
* @returns {string} A string representation of the date in the specified format. | ||
* | ||
* @example | ||
* // returns '2023-11-09' | ||
* convertDateToString(new Date('2023-11-09T14:22:54.131Z'), 'date'); | ||
* | ||
* @example | ||
* // returns '14:22:54' | ||
* convertDateToString(new Date('2023-11-09T14:22:54.131Z'), 'time'); | ||
* | ||
* @example | ||
* // returns '2023-11-09T14:22:54.131Z' | ||
* convertDateToString(new Date('2023-11-09T14:22:54.131Z'), 'date-time'); | ||
* | ||
* @example | ||
* // returns '2023-11-09T14:22:54.131Z' | ||
* convertDateToString(new Date('2023-11-09T14:22:54.131Z')); | ||
*/ | ||
export const convertDateToString = ( | ||
date: Date, | ||
format?: 'date' | 'time' | 'date-time' | ||
): string => { | ||
//e.g. '2023-11-09T14:22:54.131Z' | ||
const dateString = date.toISOString(); | ||
if (format === 'date-time') { | ||
return dateString; | ||
} else if (format === 'date') { | ||
// e.g. '2023-11-09' | ||
return dateString.split('T')[0]; | ||
} else if (format === 'time') { | ||
//e.g. '14:22:54' | ||
return dateString.split('T')[1].split('.')[0]; | ||
} | ||
return dateString; | ||
}; | ||
/** | ||
* Escape the given string such that it can be used as a class name, | ||
@@ -39,0 +82,0 @@ * i.e. hashes and slashes will be replaced. |
@@ -34,2 +34,3 @@ /* | ||
strict: false, | ||
addUsedSchema: false, | ||
...options, | ||
@@ -36,0 +37,0 @@ }); |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1042276
13542