2015-11-19 00:23:18 +03:00
|
|
|
'use strict';
|
|
|
|
|
2015-12-14 18:20:40 +03:00
|
|
|
import {ElementRef} from 'angular2/core';
|
2016-02-10 14:19:50 +03:00
|
|
|
|
|
|
|
import {RedocComponent, BaseComponent, SchemaManager} from '../base';
|
2016-04-19 18:56:20 +03:00
|
|
|
import {DropDown} from '../../common/components/DropDown/dropdown';
|
2015-11-29 18:06:08 +03:00
|
|
|
import JsonPointer from '../../utils/JsonPointer';
|
2015-11-19 00:23:18 +03:00
|
|
|
|
|
|
|
@RedocComponent({
|
|
|
|
selector: 'json-schema',
|
|
|
|
templateUrl: './lib/components/JsonSchema/json-schema.html',
|
|
|
|
styleUrls: ['./lib/components/JsonSchema/json-schema.css'],
|
2016-04-19 18:56:20 +03:00
|
|
|
directives: [JsonSchema, DropDown],
|
2016-04-27 22:34:27 +03:00
|
|
|
inputs: ['isArray', 'final', 'nestOdd', 'childFor', 'skipReadOnly']
|
2015-11-19 00:23:18 +03:00
|
|
|
})
|
2016-02-10 14:19:50 +03:00
|
|
|
@Reflect.metadata('parameters', [[SchemaManager], [ElementRef]])
|
2015-11-19 00:23:18 +03:00
|
|
|
export default class JsonSchema extends BaseComponent {
|
|
|
|
constructor(schemaMgr, elementRef) {
|
|
|
|
super(schemaMgr);
|
2016-02-10 16:48:07 +03:00
|
|
|
this.$element = elementRef.nativeElement;
|
2016-01-09 23:34:44 +03:00
|
|
|
this.final = false;
|
2015-11-19 00:23:18 +03:00
|
|
|
}
|
|
|
|
|
2016-04-19 18:56:20 +03:00
|
|
|
selectDerived(subClassIdx) {
|
|
|
|
let subClass = this.schema.derived[subClassIdx];
|
|
|
|
if (!subClass || subClass.active) return;
|
2016-03-27 01:44:11 +03:00
|
|
|
this.schema.derived.forEach((subSchema) => {
|
2016-03-18 16:06:22 +03:00
|
|
|
subSchema.active = false;
|
|
|
|
});
|
|
|
|
subClass.active = true;
|
2016-04-13 15:46:41 +03:00
|
|
|
this.derivedEmtpy = false;
|
|
|
|
if (subClass.empty) this.derivedEmtpy = true;
|
2016-03-18 16:06:22 +03:00
|
|
|
}
|
|
|
|
|
2016-03-27 01:44:11 +03:00
|
|
|
unwrapArray(schema) {
|
|
|
|
var res = schema;
|
|
|
|
if (schema && schema.type === 'array') {
|
|
|
|
let ptr = schema.items._pointer
|
|
|
|
|| JsonPointer.join(schema._pointer || this.pointer, ['items']);
|
|
|
|
res = schema.items;
|
|
|
|
res._isArray = true;
|
|
|
|
res._pointer = ptr;
|
|
|
|
res = this.unwrapArray(res);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2015-11-29 13:04:28 +03:00
|
|
|
|
2016-03-27 01:44:11 +03:00
|
|
|
prepareModel() {
|
2015-11-29 13:04:28 +03:00
|
|
|
if (!this.componentSchema) {
|
2016-01-09 18:16:43 +03:00
|
|
|
throw new Error(`Can't load component schema at ${this.pointer}`);
|
2015-11-19 00:23:18 +03:00
|
|
|
}
|
2015-11-29 13:04:28 +03:00
|
|
|
this.dereference();
|
|
|
|
|
2016-03-27 01:44:11 +03:00
|
|
|
let schema = this.componentSchema;
|
|
|
|
BaseComponent.joinAllOf(schema, {omitParent: true});
|
|
|
|
schema = this.unwrapArray(schema);
|
|
|
|
runInjectors(schema, schema, schema._pointer || this.pointer);
|
2016-01-09 23:34:44 +03:00
|
|
|
|
2016-03-27 01:44:11 +03:00
|
|
|
schema.derived = schema.derived || [];
|
2016-01-17 00:24:05 +03:00
|
|
|
|
2016-03-27 01:44:11 +03:00
|
|
|
if (!schema.isTrivial) {
|
|
|
|
this.prepareObjectPropertiesData(schema);
|
2015-11-28 01:44:35 +03:00
|
|
|
}
|
|
|
|
|
2016-03-27 01:44:11 +03:00
|
|
|
this.schema = schema;
|
2016-04-20 22:04:30 +03:00
|
|
|
this.initDerived();
|
|
|
|
}
|
|
|
|
|
|
|
|
initDerived() {
|
|
|
|
if (!this.schema.derived.length) return;
|
|
|
|
let enumArr = this.schema.properties[this.schema.properties.length - 1].enum;
|
|
|
|
if (enumArr) {
|
|
|
|
let enumOrder = {};
|
|
|
|
enumArr.forEach((enumItem, idx) => {
|
|
|
|
enumOrder[enumItem.val] = idx;
|
|
|
|
});
|
|
|
|
|
|
|
|
this.schema.derived.sort((a, b) => {
|
|
|
|
return enumOrder[a.name] > enumOrder[b.name];
|
|
|
|
});
|
|
|
|
}
|
2016-04-19 18:56:20 +03:00
|
|
|
this.selectDerived(0);
|
2016-03-27 01:44:11 +03:00
|
|
|
}
|
2015-11-28 01:44:35 +03:00
|
|
|
|
2016-03-27 01:44:11 +03:00
|
|
|
prepareObjectPropertiesData(schema) {
|
|
|
|
let requiredMap = {};
|
|
|
|
if (schema.required) {
|
|
|
|
schema.required.forEach(prop => requiredMap[prop] = true);
|
2015-11-29 18:02:54 +03:00
|
|
|
}
|
2016-01-10 18:29:35 +03:00
|
|
|
|
|
|
|
let discriminatorFieldIdx = -1;
|
|
|
|
let props = Object.keys(schema.properties).map((prop, idx) => {
|
2016-03-17 01:41:40 +03:00
|
|
|
let propertySchema = schema.properties[prop];
|
2016-04-20 19:56:20 +03:00
|
|
|
let propPointer = propertySchema._pointer ||
|
|
|
|
JsonPointer.join(schema._pointer || this.pointer, ['properties', prop]);
|
2016-03-17 01:41:40 +03:00
|
|
|
propertySchema = JsonSchema.injectPropertyData(propertySchema, prop, propPointer);
|
2016-04-13 14:21:56 +03:00
|
|
|
// stop endless discriminator recursion
|
|
|
|
if (propertySchema._pointer === this.childFor) {
|
|
|
|
propertySchema._pointer = null;
|
|
|
|
}
|
2016-03-27 01:44:11 +03:00
|
|
|
propertySchema.required = !!requiredMap[prop];
|
2016-03-17 01:41:40 +03:00
|
|
|
propertySchema.isDiscriminator = (schema.discriminator === prop);
|
2016-04-13 14:22:32 +03:00
|
|
|
if (propertySchema.isDiscriminator) {
|
|
|
|
discriminatorFieldIdx = idx;
|
|
|
|
}
|
2016-03-17 01:41:40 +03:00
|
|
|
return propertySchema;
|
2015-11-19 00:51:19 +03:00
|
|
|
});
|
2016-04-27 22:34:27 +03:00
|
|
|
if (this.skipReadOnly) {
|
|
|
|
props = props.filter(prop => !prop.readOnly);
|
|
|
|
}
|
2016-01-10 18:29:35 +03:00
|
|
|
// Move discriminator field to the end of properties list
|
|
|
|
if (discriminatorFieldIdx > -1) {
|
|
|
|
let discrProp = props.splice(discriminatorFieldIdx, 1);
|
|
|
|
props.push(discrProp[0]);
|
|
|
|
}
|
2016-03-27 01:44:11 +03:00
|
|
|
schema.properties = props;
|
2015-11-19 00:23:18 +03:00
|
|
|
}
|
|
|
|
|
2016-03-17 01:41:40 +03:00
|
|
|
static injectPropertyData(propertySchema, propertyName, propPointer) {
|
|
|
|
propertySchema = Object.assign({}, propertySchema);
|
|
|
|
|
|
|
|
propertySchema._name = propertyName;
|
|
|
|
runInjectors(propertySchema, propertySchema, propPointer);
|
|
|
|
|
|
|
|
return propertySchema;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function runInjectors(injectTo, propertySchema, propertyPointer) {
|
|
|
|
for (var injName in injectors) {
|
|
|
|
let injector = injectors[injName];
|
|
|
|
if (injector.check(propertySchema)) {
|
|
|
|
injector.inject(injectTo, propertySchema, propertyPointer);
|
2016-01-23 17:01:10 +03:00
|
|
|
}
|
2016-03-17 01:41:40 +03:00
|
|
|
}
|
|
|
|
}
|
2016-01-23 17:01:10 +03:00
|
|
|
|
2016-03-17 01:41:40 +03:00
|
|
|
const injectors = {
|
|
|
|
general: {
|
|
|
|
check: () => true,
|
2016-03-27 01:44:11 +03:00
|
|
|
inject: (injectTo, propertySchema, pointer) => {
|
|
|
|
injectTo._pointer = propertySchema._pointer || pointer;
|
2016-03-17 01:41:40 +03:00
|
|
|
injectTo._displayType = propertySchema.type;
|
|
|
|
if (propertySchema.format) injectTo._displayFormat = `<${propertySchema.format}>`;
|
|
|
|
if (propertySchema.enum) {
|
|
|
|
injectTo.enum = propertySchema.enum.map((value) => {
|
|
|
|
return {val: value, type: typeof value};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2016-03-27 01:44:11 +03:00
|
|
|
discriminator: {
|
|
|
|
check: (propertySchema) => propertySchema.discriminator,
|
|
|
|
inject: (injectTo, propertySchema = injectTo, pointer) => {
|
|
|
|
injectTo.derived = SchemaManager.instance().findDerivedDefinitions(pointer);
|
|
|
|
injectTo.discriminator = propertySchema.discriminator;
|
|
|
|
}
|
|
|
|
},
|
2016-03-17 01:41:40 +03:00
|
|
|
array: {
|
|
|
|
check: (propertySchema) => {
|
|
|
|
return propertySchema.type === 'array';
|
|
|
|
},
|
|
|
|
inject: (injectTo, propertySchema = injectTo, propPointer) => {
|
|
|
|
injectTo._isArray = true;
|
|
|
|
injectTo._pointer = propertySchema.items._pointer
|
2016-03-22 01:27:34 +03:00
|
|
|
|| JsonPointer.join(propertySchema._pointer || propPointer, ['items']);
|
2016-03-17 01:41:40 +03:00
|
|
|
|
|
|
|
runInjectors(injectTo, propertySchema.items, propPointer);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
object: {
|
|
|
|
check: (propertySchema) => {
|
|
|
|
return propertySchema.type === 'object' && propertySchema.properties;
|
|
|
|
},
|
|
|
|
inject: (injectTo, propertySchema = injectTo) => {
|
2016-03-27 01:44:11 +03:00
|
|
|
let baseName = propertySchema._pointer && JsonPointer.baseName(propertySchema._pointer);
|
|
|
|
injectTo._displayType = propertySchema.title || baseName || 'object';
|
2016-03-17 01:41:40 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
noType: {
|
|
|
|
check: (propertySchema) => !propertySchema.type,
|
|
|
|
inject: (injectTo) => {
|
|
|
|
injectTo._displayType = '< * >';
|
|
|
|
injectTo._displayTypeHint = 'This field may contain data of any type';
|
2016-03-27 01:44:11 +03:00
|
|
|
injectTo.isTrivial = true;
|
2016-02-06 18:00:31 +03:00
|
|
|
}
|
2016-03-17 01:41:40 +03:00
|
|
|
},
|
2016-01-23 17:29:34 +03:00
|
|
|
|
2016-03-17 01:41:40 +03:00
|
|
|
simpleType: {
|
|
|
|
check: (propertySchema) => {
|
|
|
|
if (propertySchema.type === 'object') {
|
2016-04-07 20:59:59 +03:00
|
|
|
return !propertySchema.properties || !Object.keys(propertySchema.properties).length;
|
2016-03-17 01:41:40 +03:00
|
|
|
}
|
|
|
|
return (propertySchema.type !== 'array') && propertySchema.type;
|
|
|
|
},
|
|
|
|
inject: (injectTo, propertySchema = injectTo) => {
|
2016-03-27 01:44:11 +03:00
|
|
|
injectTo.isTrivial = true;
|
2016-03-17 01:41:40 +03:00
|
|
|
if (injectTo._pointer) {
|
|
|
|
injectTo._pointer = undefined;
|
|
|
|
injectTo._displayType = propertySchema.title ?
|
|
|
|
`${propertySchema.title} (${propertySchema.type})` : propertySchema.type;
|
|
|
|
}
|
|
|
|
}
|
2016-03-17 02:27:09 +03:00
|
|
|
},
|
|
|
|
integer: {
|
|
|
|
check: (propertySchema) => (propertySchema.type === 'integer' || propertySchema.type === 'number'),
|
|
|
|
inject: (injectTo, propertySchema = injectTo) => {
|
|
|
|
var range = '';
|
|
|
|
if (propertySchema.minimum && propertySchema.maximum) {
|
|
|
|
range += propertySchema.exclusiveMinimum ? '( ' : '[ ';
|
|
|
|
range += propertySchema.minimum;
|
|
|
|
range += ' .. ';
|
|
|
|
range += propertySchema.maximum;
|
|
|
|
range += propertySchema.exclusiveMaximum ? ' )' : ' ]';
|
|
|
|
} else if (propertySchema.maximum) {
|
|
|
|
range += propertySchema.exclusiveMaximum? '< ' : '<= ';
|
|
|
|
range += propertySchema.maximum;
|
|
|
|
} else if (propertySchema.minimum) {
|
|
|
|
range += propertySchema.exclusiveMinimum ? '> ' : '>= ';
|
|
|
|
range += propertySchema.minimum;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (range) {
|
|
|
|
injectTo._range = range;
|
|
|
|
}
|
|
|
|
}
|
2016-04-27 23:07:06 +03:00
|
|
|
},
|
|
|
|
string: {
|
|
|
|
check: propertySchema => (propertySchema.type === 'string'),
|
|
|
|
inject: (injectTo, propertySchema = injectTo) => {
|
|
|
|
var range;
|
|
|
|
if (propertySchema.minLength && propertySchema.maxLength) {
|
|
|
|
range = `[ ${propertySchema.minLength} .. ${propertySchema.maxLength} ]`;
|
|
|
|
} else if (propertySchema.maxLength) {
|
|
|
|
range = '<= ' + propertySchema.maxLength;
|
|
|
|
} else if (propertySchema.minimum) {
|
|
|
|
range = '>= ' + propertySchema.minLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (range) {
|
|
|
|
injectTo._range = range + ' characters';
|
|
|
|
}
|
|
|
|
}
|
2015-11-19 00:23:18 +03:00
|
|
|
}
|
2016-03-17 01:41:40 +03:00
|
|
|
};
|