angular2-schema-form
Version:
Angular2 Schema Form (DISCLAIMER: it is not related to angular-schema-form)
71 lines (70 loc) • 2.62 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var ZSchema = require('z-schema');
var SchemaValidatorFactory = (function () {
function SchemaValidatorFactory() {
}
return SchemaValidatorFactory;
}());
export { SchemaValidatorFactory };
var ZSchemaValidatorFactory = (function (_super) {
__extends(ZSchemaValidatorFactory, _super);
function ZSchemaValidatorFactory() {
var _this = _super.call(this) || this;
_this.zschema = new ZSchema({
breakOnFirstError: false
});
return _this;
}
ZSchemaValidatorFactory.prototype.createValidatorFn = function (schema) {
var _this = this;
return function (value) {
if (schema.type === 'number' || schema.type === 'integer') {
value = +value;
}
_this.zschema.validate(value, schema);
var err = _this.zschema.getLastErrors();
_this.denormalizeRequiredPropertyPaths(err);
return err || null;
};
};
ZSchemaValidatorFactory.prototype.getSchema = function (schema, ref) {
// check definitions are valid
var isValid = this.zschema.compileSchema(schema);
if (isValid) {
return this.getDefinition(schema, ref);
}
else {
throw this.zschema.getLastError();
}
};
ZSchemaValidatorFactory.prototype.denormalizeRequiredPropertyPaths = function (err) {
if (err && err.length) {
err = err.map(function (error) {
if (error.path === '#/' && error.code === 'OBJECT_MISSING_REQUIRED_PROPERTY') {
error.path = "" + error.path + error.params[0];
}
return error;
});
}
};
ZSchemaValidatorFactory.prototype.getDefinition = function (schema, ref) {
var foundSchema = schema;
ref.split('/').slice(1).forEach(function (ptr) {
if (ptr) {
foundSchema = foundSchema[ptr];
}
});
return foundSchema;
};
return ZSchemaValidatorFactory;
}(SchemaValidatorFactory));
export { ZSchemaValidatorFactory };