angular2-schema-form
Version:
Angular2 Schema Form (DISCLAIMER: it is not related to angular-schema-form)
151 lines (150 loc) • 6.54 kB
JavaScript
import { isBlank } from './utils';
function formatMessage(message, path) {
return "Parsing error on " + path + ": " + message;
}
function schemaError(message, path) {
var mesg = formatMessage(message, path);
throw new Error(mesg);
}
function schemaWarning(message, path) {
var mesg = formatMessage(message, path);
throw new Error(mesg);
}
var SchemaPreprocessor = (function () {
function SchemaPreprocessor() {
}
SchemaPreprocessor.preprocess = function (jsonSchema, path) {
if (path === void 0) { path = '/'; }
jsonSchema = jsonSchema || {};
if (jsonSchema.type === 'object') {
SchemaPreprocessor.checkProperties(jsonSchema, path);
SchemaPreprocessor.checkAndCreateFieldsets(jsonSchema, path);
}
else if (jsonSchema.type === 'array') {
SchemaPreprocessor.checkItems(jsonSchema, path);
}
SchemaPreprocessor.normalizeWidget(jsonSchema);
SchemaPreprocessor.recursiveCheck(jsonSchema, path);
};
SchemaPreprocessor.checkProperties = function (jsonSchema, path) {
if (isBlank(jsonSchema.properties)) {
jsonSchema.properties = {};
schemaWarning('Provided json schema does not contain a \'properties\' entry. Output schema will be empty', path);
}
};
SchemaPreprocessor.checkAndCreateFieldsets = function (jsonSchema, path) {
if (jsonSchema.fieldsets === undefined) {
if (jsonSchema.order !== undefined) {
SchemaPreprocessor.replaceOrderByFieldsets(jsonSchema);
}
else {
SchemaPreprocessor.createFieldsets(jsonSchema);
}
}
SchemaPreprocessor.checkFieldsUsage(jsonSchema, path);
};
SchemaPreprocessor.checkFieldsUsage = function (jsonSchema, path) {
var fieldsId = Object.keys(jsonSchema.properties);
var usedFields = {};
for (var _i = 0, _a = jsonSchema.fieldsets; _i < _a.length; _i++) {
var fieldset = _a[_i];
for (var _b = 0, _c = fieldset.fields; _b < _c.length; _b++) {
var fieldId = _c[_b];
if (usedFields[fieldId] === undefined) {
usedFields[fieldId] = [];
}
usedFields[fieldId].push(fieldset.id);
}
}
for (var _d = 0, fieldsId_1 = fieldsId; _d < fieldsId_1.length; _d++) {
var fieldId = fieldsId_1[_d];
if (usedFields.hasOwnProperty(fieldId)) {
if (usedFields[fieldId].length > 1) {
schemaError(fieldId + " is referenced by more than one fieldset: " + usedFields[fieldId], path);
}
delete usedFields[fieldId];
}
else if (jsonSchema.required.indexOf(fieldId) > -1) {
schemaError(fieldId + " is a required field but it is not referenced as part of a 'order' or a 'fieldset' property", path);
}
else {
delete jsonSchema[fieldId];
schemaWarning("Removing unreferenced field " + fieldId, path);
}
}
for (var remainingfieldsId in usedFields) {
if (usedFields.hasOwnProperty(remainingfieldsId)) {
schemaWarning("Referencing non-existent field " + remainingfieldsId + " in one or more fieldsets", path);
}
}
};
SchemaPreprocessor.createFieldsets = function (jsonSchema) {
jsonSchema.order = Object.keys(jsonSchema.properties);
SchemaPreprocessor.replaceOrderByFieldsets(jsonSchema);
};
SchemaPreprocessor.replaceOrderByFieldsets = function (jsonSchema) {
jsonSchema.fieldsets = [{
id: 'fieldset-default',
title: jsonSchema.title || '',
description: jsonSchema.description || '',
name: jsonSchema.name || '',
fields: jsonSchema.order
}];
delete jsonSchema.order;
};
SchemaPreprocessor.normalizeWidget = function (fieldSchema) {
var widget = fieldSchema.widget;
if (widget === undefined) {
widget = { 'id': fieldSchema.type };
}
else if (typeof widget === 'string') {
widget = { 'id': widget };
}
fieldSchema.widget = widget;
};
SchemaPreprocessor.checkItems = function (jsonSchema, path) {
if (jsonSchema.items === undefined) {
schemaError('No \'items\' property in array', path);
}
};
SchemaPreprocessor.recursiveCheck = function (jsonSchema, path) {
if (jsonSchema.type === 'object') {
for (var fieldId in jsonSchema.properties) {
if (jsonSchema.properties.hasOwnProperty(fieldId)) {
var fieldSchema = jsonSchema.properties[fieldId];
SchemaPreprocessor.preprocess(fieldSchema, path + fieldId + '/');
}
}
if (jsonSchema.hasOwnProperty('definitions')) {
for (var fieldId in jsonSchema.definitions) {
if (jsonSchema.definitions.hasOwnProperty(fieldId)) {
var fieldSchema = jsonSchema.definitions[fieldId];
SchemaPreprocessor.removeRecursiveRefProperties(fieldSchema, "#/definitions/" + fieldId);
SchemaPreprocessor.preprocess(fieldSchema, path + fieldId + '/');
}
}
}
}
else if (jsonSchema.type === 'array') {
SchemaPreprocessor.preprocess(jsonSchema.items, path + '*/');
}
};
SchemaPreprocessor.removeRecursiveRefProperties = function (jsonSchema, definitionPath) {
// to avoid infinite loop
if (jsonSchema.type === 'object') {
for (var fieldId in jsonSchema.properties) {
if (jsonSchema.properties.hasOwnProperty(fieldId)) {
if (jsonSchema.properties[fieldId].$ref
&& jsonSchema.properties[fieldId].$ref === definitionPath) {
delete jsonSchema.properties[fieldId];
}
else if (jsonSchema.properties[fieldId].type === 'object') {
SchemaPreprocessor.removeRecursiveRefProperties(jsonSchema.properties[fieldId], definitionPath);
}
}
}
}
};
return SchemaPreprocessor;
}());
export { SchemaPreprocessor };