From df362f9df355e30fe136768fb11d196c1b4635ff Mon Sep 17 00:00:00 2001 From: Edgar Mueller Date: Mon, 4 Jun 2018 15:41:34 +0200 Subject: [PATCH] Update of JSON schema definitions (#938) --- packages/core/src/generators/schema.ts | 26 +-- packages/core/src/index.ts | 4 +- packages/core/src/models/jsonSchema.ts | 180 +----------------- packages/core/src/models/jsonSchema4.ts | 158 +++++++++++++++ packages/core/src/models/jsonSchema7.ts | 167 ++++++++++++++++ packages/core/src/util/resolvers.ts | 9 +- packages/core/src/util/validator.ts | 2 +- .../core/test/generators/uischema.test.ts | 2 +- packages/core/test/reducers/core.test.ts | 50 +++++ packages/core/test/testers.test.ts | 4 +- packages/editor/src/editor-context.ts | 4 +- packages/editor/src/services/property.util.ts | 29 ++- .../editor/src/services/reference.service.ts | 26 +-- packages/editor/src/tree/ObjectListItem.tsx | 6 +- packages/editor/src/tree/TreeRenderer.tsx | 12 +- packages/editor/src/tree/dnd.util.ts | 4 +- 16 files changed, 443 insertions(+), 240 deletions(-) create mode 100644 packages/core/src/models/jsonSchema4.ts create mode 100644 packages/core/src/models/jsonSchema7.ts create mode 100644 packages/core/test/reducers/core.test.ts diff --git a/packages/core/src/generators/schema.ts b/packages/core/src/generators/schema.ts index 4c041b24f..61e92a50a 100644 --- a/packages/core/src/generators/schema.ts +++ b/packages/core/src/generators/schema.ts @@ -23,12 +23,12 @@ THE SOFTWARE. */ -import { JsonSchema } from '../models/jsonSchema'; +import { JsonSchema4 } from '../models/jsonSchema4'; const ADDITIONAL_PROPERTIES = 'additionalProperties'; const REQUIRED_PROPERTIES = 'required'; -type Properties = {[property: string]: JsonSchema}; +type Properties = {[property: string]: JsonSchema4}; const distinct = (array: any[], discriminator: (item: any) => string): any[] => { const known = {}; @@ -51,12 +51,12 @@ class Gen { } - schemaObject = (data: Object): JsonSchema => { - const props = this.properties(data); - const schema: JsonSchema = { - 'type': 'object', - 'properties': props, - 'additionalProperties': this.findOption(props)(ADDITIONAL_PROPERTIES) + schemaObject = (data: Object): JsonSchema4 => { + const props: Properties = this.properties(data); + const schema: JsonSchema4 = { + type: 'object', + properties: props, + additionalProperties: this.findOption(props)(ADDITIONAL_PROPERTIES) }; const required = this.findOption(props)(REQUIRED_PROPERTIES); if (required.length > 0) { @@ -81,7 +81,7 @@ class Gen { ); } - property = (data: any): JsonSchema => { + property = (data: any): JsonSchema4 => { switch (typeof data) { case 'string': return { 'type': 'string' }; @@ -104,7 +104,7 @@ class Gen { } } - schemaObjectOrArray = (data: any): JsonSchema => { + schemaObjectOrArray = (data: any): JsonSchema4 => { if (data instanceof Array) { return this.schemaArray(data as any[]); } else { @@ -112,7 +112,7 @@ class Gen { } } - schemaArray = (data: any[]): JsonSchema => { + schemaArray = (data: any[]): JsonSchema4 => { if (data.length > 0) { const allProperties = data.map(this.property); const uniqueProperties = distinct(allProperties, prop => JSON.stringify(prop)); @@ -144,9 +144,9 @@ class Gen { * @param {any} options any additional options that may alter the generated JSON schema * @returns {JsonSchema} the generated schema */ -export const generateJsonSchema = (instance: Object, options: any = {}): JsonSchema => { +export const generateJsonSchema = (instance: Object, options: any = {}): JsonSchema4 => { - const findOption = (props: Properties) => (optionName: string) => { + const findOption = (props: Properties) => (optionName: string): boolean | string[] => { switch (optionName) { case ADDITIONAL_PROPERTIES: if (options.hasOwnProperty(ADDITIONAL_PROPERTIES)) { diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 19d93a715..25320106f 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -24,7 +24,9 @@ */ export * from './util'; -export * from './models/jsonSchema'; +export { JsonSchema } from './models/jsonSchema'; +export { JsonSchema4 } from './models/jsonSchema4'; +export { JsonSchema7 } from './models/jsonSchema7'; export * from './store'; export * from './actions'; import * as Actions from './actions'; diff --git a/packages/core/src/models/jsonSchema.ts b/packages/core/src/models/jsonSchema.ts index e502d043f..815170c24 100644 --- a/packages/core/src/models/jsonSchema.ts +++ b/packages/core/src/models/jsonSchema.ts @@ -1,178 +1,4 @@ -/* - The MIT License - - Copyright (c) 2018 EclipseSource Munich - https://github.com/eclipsesource/jsonforms - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. -*/ -/** - * MIT License - * - * Copyright (c) 2016 Richard Adams (https://github.com/enriched) - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ +import { JsonSchema4 } from './jsonSchema4'; +import { JsonSchema7 } from './jsonSchema7'; -export interface JsonSchema { - $ref?: string; - ///////////////////////////////////////////////// - // Schema Metadata - ///////////////////////////////////////////////// - /** - * This is important because it tells refs where - * the root of the document is located - */ - id?: string; - /** - * It is recommended that the meta-schema is - * included in the root of any JSON Schema - */ - $schema?: JsonSchema; - /** - * Title of the schema - */ - title?: string; - /** - * Schema description - */ - description?: string; - /** - * Default json for the object represented by - * this schema - */ - 'default'?: any; - - ///////////////////////////////////////////////// - // Number Validation - ///////////////////////////////////////////////// - /** - * The value must be a multiple of the number - * (e.g. 10 is a multiple of 5) - */ - multipleOf?: number; - maximum?: number; - /** - * If true maximum must be > value, >= otherwise - */ - exclusiveMaximum?: boolean; - minimum?: number; - /** - * If true minimum must be < value, <= otherwise - */ - exclusiveMinimum?: boolean; - - ///////////////////////////////////////////////// - // String Validation - ///////////////////////////////////////////////// - maxLength?: number; - minLength?: number; - /** - * This is a regex string that the value must - * conform to - */ - pattern?: string; - - ///////////////////////////////////////////////// - // Array Validation - ///////////////////////////////////////////////// - additionalItems?: boolean | JsonSchema; - items?: JsonSchema | JsonSchema[]; - maxItems?: number; - minItems?: number; - uniqueItems?: boolean; - - ///////////////////////////////////////////////// - // Object Validation - ///////////////////////////////////////////////// - maxProperties?: number; - minProperties?: number; - required?: string[]; - additionalProperties?: boolean | JsonSchema; - /** - * Holds simple JSON Schema definitions for - * referencing from elsewhere. - */ - definitions?: {[key: string]: JsonSchema}; - /** - * The keys that can exist on the object with the - * json schema that should validate their value - */ - properties?: {[property: string]: JsonSchema}; - /** - * The key of this object is a regex for which - * properties the schema applies to - */ - patternProperties?: {[pattern: string]: JsonSchema}; - /** - * If the key is present as a property then the - * string of properties must also be present. - * If the value is a JSON Schema then it must - * also be valid for the object if the key is - * present. - */ - dependencies?: {[key: string]: JsonSchema | string[]}; - - ///////////////////////////////////////////////// - // Generic - ///////////////////////////////////////////////// - /** - * Enumerates the values that this schema can be - * e.g. - * {"type": "string", - * "enum": ["red", "green", "blue"]} - */ - 'enum'?: any[]; - /** - * The basic type of this schema, can be one of - * [string, number, object, array, boolean, null] - * or an array of the acceptable types - */ - type?: string | string[]; - - ///////////////////////////////////////////////// - // Combining Schemas - ///////////////////////////////////////////////// - allOf?: JsonSchema[]; - anyOf?: JsonSchema[]; - oneOf?: JsonSchema[]; - /** - * The entity being validated must not match this schema - */ - not?: JsonSchema; - - format?: string; - const?: any; -} +export type JsonSchema = JsonSchema4 | JsonSchema7; diff --git a/packages/core/src/models/jsonSchema4.ts b/packages/core/src/models/jsonSchema4.ts new file mode 100644 index 000000000..3a3cc64d6 --- /dev/null +++ b/packages/core/src/models/jsonSchema4.ts @@ -0,0 +1,158 @@ +/* + The MIT License + + Copyright (c) 2016 Richard Adams + https://github.com/enriched + + Modifications by EclipseSource Munich 2018 + https://github.com/eclipsesource/jsonforms + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +export interface JsonSchema4 { + $ref?: string; + ///////////////////////////////////////////////// + // Schema Metadata + ///////////////////////////////////////////////// + /** + * This is important because it tells refs where + * the root of the document is located + */ + id?: string; + /** + * It is recommended that the meta-schema is + * included in the root of any JSON Schema + */ + $schema?: string; + /** + * Title of the schema + */ + title?: string; + /** + * Schema description + */ + description?: string; + /** + * Default json for the object represented by + * this schema + */ + default?: any; + + ///////////////////////////////////////////////// + // Number Validation + ///////////////////////////////////////////////// + /** + * The value must be a multiple of the number + * (e.g. 10 is a multiple of 5) + */ + multipleOf?: number; + maximum?: number; + /** + * If true maximum must be > value, >= otherwise + */ + exclusiveMaximum?: boolean; + minimum?: number; + /** + * If true minimum must be < value, <= otherwise + */ + exclusiveMinimum?: boolean; + + ///////////////////////////////////////////////// + // String Validation + ///////////////////////////////////////////////// + maxLength?: number; + minLength?: number; + /** + * This is a regex string that the value must + * conform to + */ + pattern?: string; + + ///////////////////////////////////////////////// + // Array Validation + ///////////////////////////////////////////////// + additionalItems?: boolean | JsonSchema4; + items?: JsonSchema4 | JsonSchema4[]; + maxItems?: number; + minItems?: number; + uniqueItems?: boolean; + + ///////////////////////////////////////////////// + // Object Validation + ///////////////////////////////////////////////// + maxProperties?: number; + minProperties?: number; + required?: string[]; + additionalProperties?: boolean | JsonSchema4; + /** + * Holds simple JSON Schema definitions for + * referencing from elsewhere. + */ + definitions?: {[key: string]: JsonSchema4}; + /** + * The keys that can exist on the object with the + * json schema that should validate their value + */ + properties?: {[property: string]: JsonSchema4}; + /** + * The key of this object is a regex for which + * properties the schema applies to + */ + patternProperties?: {[pattern: string]: JsonSchema4}; + /** + * If the key is present as a property then the + * string of properties must also be present. + * If the value is a JSON Schema then it must + * also be valid for the object if the key is + * present. + */ + dependencies?: {[key: string]: JsonSchema4 | string[]}; + + ///////////////////////////////////////////////// + // Generic + ///////////////////////////////////////////////// + /** + * Enumerates the values that this schema can be + * e.g. + * {"type": "string", + * "enum": ["red", "green", "blue"]} + */ + 'enum'?: any[]; + /** + * The basic type of this schema, can be one of + * [string, number, object, array, boolean, null] + * or an array of the acceptable types + */ + type?: string | string[]; + + ///////////////////////////////////////////////// + // Combining Schemas + ///////////////////////////////////////////////// + allOf?: JsonSchema4[]; + anyOf?: JsonSchema4[]; + oneOf?: JsonSchema4[]; + /** + * The entity being validated must not match this schema + */ + not?: JsonSchema4; + + format?: string; + const?: any; +} diff --git a/packages/core/src/models/jsonSchema7.ts b/packages/core/src/models/jsonSchema7.ts new file mode 100644 index 000000000..9d6c3f397 --- /dev/null +++ b/packages/core/src/models/jsonSchema7.ts @@ -0,0 +1,167 @@ +/* + The MIT License + + Copyright (c) 2016 Richard Adams + https://github.com/enriched + + Modifications by EclipseSource Munich 2018 + https://github.com/eclipsesource/jsonforms + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +export interface JsonSchema7 { + $ref?: string; + ///////////////////////////////////////////////// + // Schema Metadata + ///////////////////////////////////////////////// + /** + * This is important because it tells refs where + * the root of the document is located + */ + $id?: string; + /** + * It is recommended that the meta-schema is + * included in the root of any JSON Schema + */ + $schema?: string; + /** + * Title of the schema + */ + title?: string; + /** + * Schema description + */ + description?: string; + /** + * Default json for the object represented by + * this schema + */ + default?: any; + + ///////////////////////////////////////////////// + // Number Validation + ///////////////////////////////////////////////// + /** + * The value must be a multiple of the number + * (e.g. 10 is a multiple of 5) + */ + multipleOf?: number; + maximum?: number; + /** + * If true maximum must be > value, >= otherwise + */ + exclusiveMaximum?: number; + minimum?: number; + /** + * If true minimum must be < value, <= otherwise + */ + exclusiveMinimum?: number; + + ///////////////////////////////////////////////// + // String Validation + ///////////////////////////////////////////////// + maxLength?: number; + minLength?: number; + /** + * This is a regex string that the value must + * conform to + */ + pattern?: string; + + ///////////////////////////////////////////////// + // Array Validation + ///////////////////////////////////////////////// + additionalItems?: boolean | JsonSchema7; + items?: JsonSchema7 | JsonSchema7[]; + maxItems?: number; + minItems?: number; + uniqueItems?: boolean; + + ///////////////////////////////////////////////// + // Object Validation + ///////////////////////////////////////////////// + maxProperties?: number; + minProperties?: number; + required?: string[]; + additionalProperties?: boolean | JsonSchema7; + /** + * Holds simple JSON Schema definitions for + * referencing from elsewhere. + */ + definitions?: {[key: string]: JsonSchema7}; + /** + * The keys that can exist on the object with the + * json schema that should validate their value + */ + properties?: {[property: string]: JsonSchema7}; + /** + * The key of this object is a regex for which + * properties the schema applies to + */ + patternProperties?: {[pattern: string]: JsonSchema7}; + /** + * If the key is present as a property then the + * string of properties must also be present. + * If the value is a JSON Schema then it must + * also be valid for the object if the key is + * present. + */ + dependencies?: {[key: string]: JsonSchema7 | string[]}; + + ///////////////////////////////////////////////// + // Generic + ///////////////////////////////////////////////// + /** + * Enumerates the values that this schema can be + * e.g. + * {"type": "string", + * "enum": ["red", "green", "blue"]} + */ + 'enum'?: any[]; + /** + * The basic type of this schema, can be one of + * [string, number, object, array, boolean, null] + * or an array of the acceptable types + */ + type?: string | string[]; + + ///////////////////////////////////////////////// + // Combining Schemas + ///////////////////////////////////////////////// + allOf?: JsonSchema7[]; + anyOf?: JsonSchema7[]; + oneOf?: JsonSchema7[]; + /** + * The entity being validated must not match this schema + */ + not?: JsonSchema7; + + format?: string; + readOnly?: boolean; + writeOnly?: boolean; + examples?: any[]; + contains?: JsonSchema7; + propertyNames?: JsonSchema7; + const?: any; + if?: JsonSchema7; + then?: JsonSchema7; + else?: JsonSchema7; + errorMessage?: any; +} \ No newline at end of file diff --git a/packages/core/src/util/resolvers.ts b/packages/core/src/util/resolvers.ts index 0dd1196c0..7cf28ef8a 100644 --- a/packages/core/src/util/resolvers.ts +++ b/packages/core/src/util/resolvers.ts @@ -23,8 +23,7 @@ THE SOFTWARE. */ import * as _ from 'lodash'; - -import { JsonSchema } from '../models/jsonSchema'; +import { JsonSchema } from '..'; /** * Map for storing refs and the respective schemas they are pointing to. @@ -79,14 +78,16 @@ export const findAllRefs = if (isArray(schema)) { if (Array.isArray(schema.items)) { if (resolveTuples) { - schema.items.forEach(child => findAllRefs(child, result)); + const items: JsonSchema[] = schema.items; + items.forEach(child => findAllRefs(child, result)); } } else { findAllRefs(schema.items, result); } } if (Array.isArray(schema.anyOf)) { - schema.anyOf.forEach(child => findAllRefs(child, result)); + const anyOf: JsonSchema[] = schema.anyOf; + anyOf.forEach(child => findAllRefs(child, result)); } if (schema.$ref !== undefined) { result[schema.$ref] = schema; diff --git a/packages/core/src/util/validator.ts b/packages/core/src/util/validator.ts index b622a0a97..2305cc03c 100644 --- a/packages/core/src/util/validator.ts +++ b/packages/core/src/util/validator.ts @@ -3,7 +3,7 @@ import { Draft4 } from '../models/draft4'; export const createAjv = () => { const ajv = new AJV({ - schemaId: 'id', + schemaId: 'auto', allErrors: true, jsonPointers: true, errorDataPath: 'property' diff --git a/packages/core/test/generators/uischema.test.ts b/packages/core/test/generators/uischema.test.ts index da9559e54..cac8e0d89 100644 --- a/packages/core/test/generators/uischema.test.ts +++ b/packages/core/test/generators/uischema.test.ts @@ -24,8 +24,8 @@ */ import test from 'ava'; import { generateDefaultUISchema } from '../../src/generators/uischema'; -import { JsonSchema } from '../../src/models/jsonSchema'; import { ControlElement, LabelElement, Layout, VerticalLayout } from '../../src/models/uischema'; +import { JsonSchema } from '../../src'; test('generate ui schema for Control element by resolving refs', t => { const schema: JsonSchema = { diff --git a/packages/core/test/reducers/core.test.ts b/packages/core/test/reducers/core.test.ts new file mode 100644 index 000000000..5732152f0 --- /dev/null +++ b/packages/core/test/reducers/core.test.ts @@ -0,0 +1,50 @@ +/* + The MIT License + + Copyright (c) 2018 EclipseSource Munich + https://github.com/eclipsesource/jsonforms + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +import test from 'ava'; +import { coreReducer } from '../../src/reducers'; +import { init } from '../../src/actions'; +import { JsonSchema } from '../../src/models/jsonSchema'; + +test('core reducer should support v7', t => { + const schema: JsonSchema = { + type: 'object', + properties: { + foo: { + type: 'string', + const: 'bar' + } + } + }; + const after = coreReducer( + undefined, + init( + { + foo: 'baz' + }, + schema + ) + ); + t.is(after.errors.length, 1); +}); diff --git a/packages/core/test/testers.test.ts b/packages/core/test/testers.test.ts index fb3f36c13..2d400cac8 100644 --- a/packages/core/test/testers.test.ts +++ b/packages/core/test/testers.test.ts @@ -26,14 +26,14 @@ import test from 'ava'; import { and, formatIs, - isPrimitiveArrayControl, - isObjectArrayControl, isBooleanControl, isDateControl, isEnumControl, isIntegerControl, isMultiLineControl, isNumberControl, + isObjectArrayControl, + isPrimitiveArrayControl, isStringControl, isTimeControl, optionIs, diff --git a/packages/editor/src/editor-context.ts b/packages/editor/src/editor-context.ts index f8a6edcf2..21bca1bda 100644 --- a/packages/editor/src/editor-context.ts +++ b/packages/editor/src/editor-context.ts @@ -1,9 +1,9 @@ -import { JsonSchema } from '@jsonforms/core'; +import { JsonSchema4 } from '@jsonforms/core'; import { ModelMapping } from './helpers/container.util'; // TODO remove when it is no longer needed by the schema service impl: Besides that it is obsolete export interface EditorContext { - dataSchema: JsonSchema; + dataSchema: JsonSchema4; identifyingProperty: string; modelMapping: ModelMapping; } diff --git a/packages/editor/src/services/property.util.ts b/packages/editor/src/services/property.util.ts index 81d9178a2..c40892447 100644 --- a/packages/editor/src/services/property.util.ts +++ b/packages/editor/src/services/property.util.ts @@ -1,12 +1,12 @@ import * as _ from 'lodash'; -import { JsonSchema } from '@jsonforms/core'; +import { JsonSchema4 } from '@jsonforms/core'; import { resolveSchema } from '@jsonforms/core'; import * as JsonRefs from 'json-refs'; -const isObject = (schema: JsonSchema): boolean => { +const isObject = (schema: JsonSchema4): boolean => { return schema.properties !== undefined; }; -const isArray = (schema: JsonSchema): boolean => { +const isArray = (schema: JsonSchema4): boolean => { return schema.items !== undefined; }; const deepCopy = (object: T): T => { @@ -29,7 +29,7 @@ export interface Property { /** * The schema is the JsonSchema this property describes. */ - schema: JsonSchema; + schema: JsonSchema4; } /** @@ -72,7 +72,7 @@ export const findPropertyLabel = (property: Property): string => { * @param extractedReferences Contains the definition attributes for the current schema * @returns SchemaRefs all the required reference paths for subschema */ -const findReferences = (parentSchema: JsonSchema, +const findReferences = (parentSchema: JsonSchema4, allRefs: SchemaRefs, schemaRefs: SchemaRefs, extractedReferences: { [id: string]: string } @@ -102,8 +102,8 @@ const findReferences = (parentSchema: JsonSchema, * @param schema current subschema without resolved references * @returns JsonSchema current subschema with resolved references */ -export const makeSchemaSelfContained = (parentSchema: JsonSchema, - schema: JsonSchema): JsonSchema => { +export const makeSchemaSelfContained = (parentSchema: JsonSchema4, + schema: JsonSchema4): JsonSchema4 => { const schemaRefs = JsonRefs.findRefs(schema, {resolveCirculars: true}); const allRefs = JsonRefs.findRefs(parentSchema, {resolveCirculars: true}); let extractedReferences; @@ -140,8 +140,8 @@ export const makeSchemaSelfContained = (parentSchema: JsonSchema, * @param refPath The path to resolve * @return a JsonSchema that is self-contained */ -export const makeSelfContainedSchema = (parentSchema: JsonSchema, refPath: string): JsonSchema => { - let schema = resolveSchema(parentSchema, refPath); +export const makeSelfContainedSchema = (parentSchema: JsonSchema4, refPath: string): JsonSchema4 => { + let schema: JsonSchema4 = resolveSchema(parentSchema, refPath) as JsonSchema4; schema = deepCopy(schema); if (_.isEmpty(schema.id)) { schema.id = '#' + refPath; @@ -178,8 +178,8 @@ const calculateLabel = (id: string): string => ( */ export const findContainerProperties = (property: string, label: string, - schema: JsonSchema, - rootSchema: JsonSchema, + schema: JsonSchema4, + rootSchema: JsonSchema4, isInContainer: boolean, hasOnlyOwnChildren = false): Property[] => { if (schema.$ref !== undefined) { @@ -261,8 +261,7 @@ export const findContainerProperties = (property: string, * @return The array of {@link Property} or empty if no properties are available * @see Property */ -export const retrieveContainerProperties = (schema: JsonSchema, - rootSchema: JsonSchema) => { +export const retrieveContainerProperties = (schema: JsonSchema4, rootSchema: JsonSchema4) => { return findContainerProperties('root', 'root', schema, rootSchema, false); }; @@ -277,8 +276,8 @@ export const retrieveContainerProperties = (schema: JsonSchema, * @returns {{[p: string]: Property[]}} */ export const findAllContainerProperties = - (schema: JsonSchema, - rootSchema: JsonSchema, + (schema: JsonSchema4, + rootSchema: JsonSchema4, containerProperties: { [schemaId: string]: Property[] } = {}): { [schemaId: string]: Property[] } => { diff --git a/packages/editor/src/services/reference.service.ts b/packages/editor/src/services/reference.service.ts index 54ecc3774..03e86ff4e 100644 --- a/packages/editor/src/services/reference.service.ts +++ b/packages/editor/src/services/reference.service.ts @@ -1,4 +1,4 @@ -import { JsonSchema } from '@jsonforms/core'; +import { JsonSchema4 } from '@jsonforms/core'; import { makeSelfContainedSchema, Property } from './property.util'; import { EditorContext } from '../editor-context'; import { Resources } from '../resources/resources'; @@ -18,7 +18,7 @@ export interface ReferenceProperty extends Property { /** * The schema of the referenced elements. */ - readonly targetSchema: JsonSchema; + readonly targetSchema: JsonSchema4; /** * This allows to set the reference. * @@ -57,8 +57,8 @@ export interface ReferenceProperty extends Property { export class ReferencePropertyImpl implements ReferenceProperty { constructor( - private innerSchema: JsonSchema, - private innerTargetSchema: JsonSchema, + private innerSchema: JsonSchema4, + private innerTargetSchema: JsonSchema4, private key: string, private name: string, private idBased: boolean, @@ -77,13 +77,13 @@ export class ReferencePropertyImpl implements ReferenceProperty { n => !_.isEmpty(n) ); } - get schema(): JsonSchema { + get schema(): JsonSchema4 { return this.innerSchema; } get property(): string { return this.key; } - get targetSchema(): JsonSchema { + get targetSchema(): JsonSchema4 { return this.innerTargetSchema; } addToData(data: object, valueToAdd: Object): void { @@ -104,7 +104,7 @@ export class ReferencePropertyImpl implements ReferenceProperty { * * @return true if the data adheres to the schema, false otherwise */ -const checkData = (data: Object, targetSchema: JsonSchema): boolean => { +const checkData = (data: Object, targetSchema: JsonSchema4): boolean => { // use AJV to validate data against targetSchema and return result const valid = ajv.validate(targetSchema, data); if (_.isBoolean(valid)) { @@ -113,7 +113,7 @@ const checkData = (data: Object, targetSchema: JsonSchema): boolean => { return false; }; -const addReference = (schema: JsonSchema, identifyingProperty: string, propName: string) => +const addReference = (schema: JsonSchema4, identifyingProperty: string, propName: string) => (data: Object, toAdd: object) => { const refValue = toAdd[identifyingProperty]; @@ -169,7 +169,7 @@ const getReferenceTargetData = (data: Object, href: string): Object => { }; // reference resolvement for id based references -export const resolveRef = (schema: JsonSchema, +export const resolveRef = (schema: JsonSchema4, findTargets: (data: Object) => { [key: string]: Object }, propName: string) => (data: Object): { [key: string]: Object } => { if (_.isEmpty(data)) { @@ -235,7 +235,7 @@ export const getFindReferenceTargetsFunction = * The result is an object mapping from the found paths to the target data object * found at the path. */ -const collectionHelperMap = (currentPath: string, data: Object, targetSchema: JsonSchema) +const collectionHelperMap = (currentPath: string, data: Object, targetSchema: JsonSchema4) : { [key: string]: Object } => { const result = {}; if (checkData(data, targetSchema)) { @@ -267,7 +267,7 @@ const collectionHelperMap = (currentPath: string, data: Object, targetSchema: Js /** * Adds the reference path given in toAdd to the data's reference property. */ -const addPathBasedRef = (schema: JsonSchema, propName: string) => +const addPathBasedRef = (schema: JsonSchema4, propName: string) => (data: Object, toAdd: object) => { if (typeof toAdd !== 'string') { console.error(`Path based reference values must be of type string. The given value was of` @@ -304,7 +304,7 @@ const resolvePathBasedRef = (href: string, pathProperty: string) => (data: Objec return result; }; -const getPathBasedRefTargets = (href: string, targetSchema: JsonSchema) => (data: Object) +const getPathBasedRefTargets = (href: string, targetSchema: JsonSchema4) => (data: Object) : { [key: string]: Object } => { const targetData = getReferenceTargetData(data, href); @@ -370,7 +370,7 @@ const getSchemaIdForObject = (object: Object, modelMapping: ModelMapping): strin * @return The array of {@link ReferenceProperty} or empty if no references are available * @see ReferenceProperty */ -export const getReferenceProperties = (schema: JsonSchema, +export const getReferenceProperties = (schema: JsonSchema4, editorContext: EditorContext): ReferenceProperty[] => { if (schema.$ref !== undefined) { return getReferenceProperties( diff --git a/packages/editor/src/tree/ObjectListItem.tsx b/packages/editor/src/tree/ObjectListItem.tsx index 42bd02f73..70f597c73 100644 --- a/packages/editor/src/tree/ObjectListItem.tsx +++ b/packages/editor/src/tree/ObjectListItem.tsx @@ -5,7 +5,7 @@ import { connect } from 'react-redux'; import { getData, getSchema, - JsonSchema, + JsonSchema4, Paths, resolveData, update @@ -39,7 +39,7 @@ import { const RESET_SELECTION_DELAY = 40; const getNamingFunction = - (schema: JsonSchema, labelMapping) => (element: Object): string => { + (schema: JsonSchema4, labelMapping) => (element: Object): string => { if (!_.isEmpty(labelMapping) && labelMapping[schema.id] !== undefined) { @@ -72,7 +72,7 @@ const getNamingFunction = export interface ObjectListItemProps { path: string; - schema: JsonSchema; + schema: JsonSchema4; rootData: any; data: any; selection: any; diff --git a/packages/editor/src/tree/TreeRenderer.tsx b/packages/editor/src/tree/TreeRenderer.tsx index 0f563e2b8..82d6f0a59 100644 --- a/packages/editor/src/tree/TreeRenderer.tsx +++ b/packages/editor/src/tree/TreeRenderer.tsx @@ -8,7 +8,7 @@ import { ControlState, generateDefaultUISchema, getData, - JsonSchema, + JsonSchema4, Paths, Resolve, Runtime, @@ -29,7 +29,7 @@ import { MasterDetailLayout } from '../master-detail-layout'; import { getUiSchemata } from '../reducers'; export interface MasterProps { - schema: JsonSchema; + schema: JsonSchema4; path: string; rootData: any; selection: any; @@ -59,7 +59,7 @@ const Master = (