From 90b9683d31ec2eeb0b2e3822a0bb7250f94a2549 Mon Sep 17 00:00:00 2001 From: Daniel Agoston Date: Fri, 6 Apr 2018 10:28:23 +0200 Subject: [PATCH 1/2] update from draft6 to draft 7 --- README.md | 2 +- src/lib/src/shared/convert-schema-to-draft6.function.ts | 6 +++--- src/lib/src/shared/json-schema.functions.ts | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index d7f19ce1..bc1921d9 100755 --- a/README.md +++ b/README.md @@ -255,7 +255,7 @@ Combining inputs is useful if you have many unique forms and store each form's d #### Compatibility modes -If you have previously used another JSON form creation library—Angular Schema Form (for AngularJS), React JSON Schema Form, or JSON Form (for jQuery)—in order to make the transition easier, Angular JSON Schema Form will recognize the input names and custom input objects used by those libraries. It should automatically work with JSON Schemas in [version 6](http://json-schema.org/draft-06/schema), [version 4](http://json-schema.org/draft-04/schema), [version 3](http://json-schema.org/draft-03/schema), or the [truncated version 3 format supported by JSON Form](https://github.com/joshfire/jsonform/wiki#schema-shortcut). So the following will all work: +If you have previously used another JSON form creation library—Angular Schema Form (for AngularJS), React JSON Schema Form, or JSON Form (for jQuery)—in order to make the transition easier, Angular JSON Schema Form will recognize the input names and custom input objects used by those libraries. It should automatically work with JSON Schemas in [version 7](http://json-schema.org/draft-07/schema), [version 6](http://json-schema.org/draft-06/schema), [version 4](http://json-schema.org/draft-04/schema), [version 3](http://json-schema.org/draft-03/schema), or the [truncated version 3 format supported by JSON Form](https://github.com/joshfire/jsonform/wiki#schema-shortcut). So the following will all work: Angular Schema Form (AngularJS) compatibility: ```html diff --git a/src/lib/src/shared/convert-schema-to-draft6.function.ts b/src/lib/src/shared/convert-schema-to-draft6.function.ts index 0083ac55..4dfa42b8 100755 --- a/src/lib/src/shared/convert-schema-to-draft6.function.ts +++ b/src/lib/src/shared/convert-schema-to-draft6.function.ts @@ -204,7 +204,7 @@ export function convertSchemaToDraft6(schema, options: OptionObject = {}) { if (newSchema.id.slice(-1) === '#') { newSchema.id = newSchema.id.slice(0, -1); } - newSchema.$id = newSchema.id + '-CONVERTED-TO-DRAFT-06#'; + newSchema.$id = newSchema.id + '-CONVERTED-TO-DRAFT-07#'; delete newSchema.id; changed = true; } @@ -221,10 +221,10 @@ export function convertSchemaToDraft6(schema, options: OptionObject = {}) { if (typeof newSchema.$schema === 'string' && /http\:\/\/json\-schema\.org\/draft\-0[1-4]\/schema\#/.test(newSchema.$schema) ) { - newSchema.$schema = 'http://json-schema.org/draft-06/schema#'; + newSchema.$schema = 'http://json-schema.org/draft-07/schema#'; changed = true; } else if (changed && typeof newSchema.$schema === 'string') { - const addToDescription = 'Converted to draft 6 from ' + newSchema.$schema; + const addToDescription = 'Converted to draft 7 from ' + newSchema.$schema; if (typeof newSchema.description === 'string' && newSchema.description.length) { newSchema.description += '\n' + addToDescription; } else { diff --git a/src/lib/src/shared/json-schema.functions.ts b/src/lib/src/shared/json-schema.functions.ts index 7ec27d9c..17a93f39 100755 --- a/src/lib/src/shared/json-schema.functions.ts +++ b/src/lib/src/shared/json-schema.functions.ts @@ -97,7 +97,7 @@ export function buildSchemaFromData( }; const buildSubSchema = (value) => buildSchemaFromData(value, requireAllFields, false); - if (isRoot) { newSchema.$schema = 'http://json-schema.org/draft-06/schema#'; } + if (isRoot) { newSchema.$schema = 'http://json-schema.org/draft-07/schema#'; } newSchema.type = getFieldType(data); if (newSchema.type === 'object') { newSchema.properties = {}; From 67282a91ae33db94fd98c97988b6b3966d9bfe07 Mon Sep 17 00:00:00 2001 From: Daniel Agoston Date: Thu, 7 Jun 2018 08:18:25 +0200 Subject: [PATCH 2/2] rename convert-schema-to-draft6 --- src/lib/index.ts | 2 +- src/lib/src/json-schema-form.component.ts | 4 ++-- ...o-draft6.function.ts => convert-schema.ts} | 22 +++++++++---------- src/lib/src/shared/index.ts | 2 +- 4 files changed, 15 insertions(+), 15 deletions(-) rename src/lib/src/shared/{convert-schema-to-draft6.function.ts => convert-schema.ts} (95%) mode change 100755 => 100644 diff --git a/src/lib/index.ts b/src/lib/index.ts index 718e59e3..06708383 100755 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -18,7 +18,7 @@ export { updateInputOptions, getTitleMapFromOneOf, getControlValidators, resolveSchemaReferences, getSubSchema, combineAllOf, fixRequiredArrayProperties } from './src/shared/json-schema.functions'; -export { convertSchemaToDraft6 } from './src/shared/convert-schema-to-draft6.function'; +export { convertSchemaToDraft7 } from './src/shared/convert-schema.function'; export { mergeSchemas } from './src/shared/merge-schemas.function'; export { buildFormGroupTemplate, buildFormGroup, formatFormData, diff --git a/src/lib/src/json-schema-form.component.ts b/src/lib/src/json-schema-form.component.ts index e6f99d6f..3f4a49d2 100755 --- a/src/lib/src/json-schema-form.component.ts +++ b/src/lib/src/json-schema-form.component.ts @@ -10,7 +10,7 @@ import * as _ from 'lodash'; import { FrameworkLibraryService } from './framework-library/framework-library.service'; import { WidgetLibraryService } from './widget-library/widget-library.service'; import { JsonSchemaFormService } from './json-schema-form.service'; -import { convertSchemaToDraft6 } from './shared/convert-schema-to-draft6.function'; +import { convertSchemaToDraft7 } from './shared/convert-schema.function'; import { resolveSchemaReferences } from './shared/json-schema.functions'; import { hasValue, inArray, isArray, isEmpty, isNumber, isObject @@ -460,7 +460,7 @@ export class JsonSchemaFormComponent implements ControlValueAccessor, OnChanges, // If needed, update JSON Schema to draft 6 format, including // draft 3 (JSON Form style) and draft 4 (Angular Schema Form style) - this.jsf.schema = convertSchemaToDraft6(this.jsf.schema); + this.jsf.schema = convertSchemaToDraft7(this.jsf.schema); // Initialize ajv and compile schema this.jsf.compileAjvSchema(); diff --git a/src/lib/src/shared/convert-schema-to-draft6.function.ts b/src/lib/src/shared/convert-schema.ts old mode 100755 new mode 100644 similarity index 95% rename from src/lib/src/shared/convert-schema-to-draft6.function.ts rename to src/lib/src/shared/convert-schema.ts index 4dfa42b8..382ea34b --- a/src/lib/src/shared/convert-schema-to-draft6.function.ts +++ b/src/lib/src/shared/convert-schema.ts @@ -1,29 +1,29 @@ import * as _ from 'lodash'; /** - * 'convertSchemaToDraft6' function + * 'convertSchemaToDraft7' function * - * Converts a JSON Schema from draft 1 through 4 format to draft 6 format + * Converts a JSON Schema from draft 1 through 4 format to draft 7 format * * Inspired by on geraintluff's JSON Schema 3 to 4 compatibility function: * https://github.com/geraintluff/json-schema-compatibility - * Also uses suggestions from AJV's JSON Schema 4 to 6 migration guide: + * Also uses suggestions from AJV's JSON Schema 4 to 7 migration guide: * https://github.com/epoberezkin/ajv/releases/tag/5.0.0 * And additional details from the official JSON Schema documentation: * http://json-schema.org * - * @param { object } originalSchema - JSON schema (draft 1, 2, 3, 4, or 6) + * @param { object } originalSchema - JSON schema (draft 1, 2, 3, 4, 6, or 7) * @param { OptionObject = {} } options - options: parent schema changed?, schema draft number? - * @return { object } - JSON schema (draft 6) + * @return { object } - JSON schema (draft 7) */ export interface OptionObject { changed?: boolean, draft?: number }; -export function convertSchemaToDraft6(schema, options: OptionObject = {}) { +export function convertSchemaToDraft7(schema, options: OptionObject = {}) { let draft: number = options.draft || null; let changed: boolean = options.changed || false; if (typeof schema !== 'object') { return schema; } if (typeof schema.map === 'function') { - return [ ...schema.map(subSchema => convertSchemaToDraft6(subSchema, { changed, draft })) ]; + return [ ...schema.map(subSchema => convertSchemaToDraft7(subSchema, { changed, draft })) ]; } let newSchema = { ...schema }; const simpleTypes = ['array', 'boolean', 'integer', 'null', 'number', 'object', 'string']; @@ -45,8 +45,8 @@ export function convertSchemaToDraft6(schema, options: OptionObject = {}) { // Convert v1-v3 'extends' to 'allOf' if (typeof newSchema.extends === 'object') { newSchema.allOf = typeof newSchema.extends.map === 'function' ? - newSchema.extends.map(subSchema => convertSchemaToDraft6(subSchema, { changed, draft })) : - [ convertSchemaToDraft6(newSchema.extends, { changed, draft }) ]; + newSchema.extends.map(subSchema => convertSchemaToDraft7(subSchema, { changed, draft })) : + [ convertSchemaToDraft7(newSchema.extends, { changed, draft }) ]; delete newSchema.extends; changed = true; } @@ -304,14 +304,14 @@ export function convertSchemaToDraft6(schema, options: OptionObject = {}) { ) { const newKey = {}; Object.keys(newSchema[key]).forEach(subKey => newKey[subKey] = - convertSchemaToDraft6(newSchema[key][subKey], { changed, draft }) + convertSchemaToDraft7(newSchema[key][subKey], { changed, draft }) ); newSchema[key] = newKey; } else if ( [ 'items', 'additionalItems', 'additionalProperties', 'allOf', 'anyOf', 'oneOf', 'not' ].includes(key) ) { - newSchema[key] = convertSchemaToDraft6(newSchema[key], { changed, draft }); + newSchema[key] = convertSchemaToDraft7(newSchema[key], { changed, draft }); } else { newSchema[key] = _.cloneDeep(newSchema[key]); } diff --git a/src/lib/src/shared/index.ts b/src/lib/src/shared/index.ts index 271550a1..e3a0a502 100755 --- a/src/lib/src/shared/index.ts +++ b/src/lib/src/shared/index.ts @@ -26,7 +26,7 @@ export { resolveSchemaReferences, getSubSchema, combineAllOf, fixRequiredArrayProperties } from './json-schema.functions'; -export { convertSchemaToDraft6 } from './convert-schema-to-draft6.function'; +export { convertSchemaToDraft7 } from './convert-schema.function'; export { mergeSchemas } from './merge-schemas.function';