Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update from draft6 to draft 7 #259

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/src/json-schema-form.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down
28 changes: 14 additions & 14 deletions ...ared/convert-schema-to-draft6.function.ts → src/lib/src/shared/convert-schema.ts
100755 → 100644
Original file line number Diff line number Diff line change
@@ -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'];
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/src/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
2 changes: 1 addition & 1 deletion src/lib/src/shared/json-schema.functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down