lex-model-validator
Version:
Validate lex-language-models with ease.
97 lines (96 loc) • 3.83 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var lodash_get_1 = __importDefault(require("lodash.get"));
var __1 = require("..");
var Validator = /** @class */ (function () {
function Validator($master, $parent) {
this.$master = $master;
this.$parent = $parent;
this.$children = [];
}
Object.defineProperty(Validator.prototype, "name", {
get: function () {
return this.constructor.name;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Validator.prototype, "childrenCount", {
get: function () {
return this.$children.length;
},
enumerable: true,
configurable: true
});
Validator.prototype.use = function () {
var _this = this;
var validatorConstructors = [];
for (var _i = 0; _i < arguments.length; _i++) {
validatorConstructors[_i] = arguments[_i];
}
validatorConstructors.forEach(function (constructor) {
if (!_this.$children.some(function (validator) {
return validator.name === constructor.name;
})) {
_this.$children.push(new constructor(_this.$master, _this.$parent));
}
});
};
Validator.prototype.run = function (data) {
this.validate(data, this.getConstraints());
};
Validator.prototype.runChildren = function (data) {
this.$children.forEach(__1.runValidation.bind(this, data));
};
Validator.prototype.pushError = function (error) {
if (typeof error === 'string') {
this.$master.collectedErrors.push(new __1.BaseError(error));
}
else {
this.$master.collectedErrors.push(error);
}
};
Validator.prototype.validate = function (data, constraints, prefix) {
var pathsToObjectsToBeValidated = Object.keys(constraints);
for (var _i = 0, pathsToObjectsToBeValidated_1 = pathsToObjectsToBeValidated; _i < pathsToObjectsToBeValidated_1.length; _i++) {
var path = pathsToObjectsToBeValidated_1[_i];
var constraint = constraints[path];
if (typeof constraint.required === 'undefined') {
constraint.required = true;
}
var objectToBeValidated = lodash_get_1.default(data, path);
var constraintProps = this.getSortedConstraintProperties(constraint);
for (var _a = 0, constraintProps_1 = constraintProps; _a < constraintProps_1.length; _a++) {
var constraintProp = constraintProps_1[_a];
this.callValidation({
key: constraintProp,
value: constraint[constraintProp]
}, {
key: prefix ? prefix + "." + path : path,
value: objectToBeValidated
});
}
}
};
Validator.prototype.getSortedConstraintProperties = function (constraint) {
return Object.keys(constraint).sort(function (a, b) {
if (a === 'required') {
return -1;
}
else if (b === 'required') {
return 1;
}
else {
return 0;
}
});
};
Validator.prototype.callValidation = function (constraintPair, validationPair) {
__1.CONSTRAINT_VALIDATION_FUNCTION_MAP[constraintPair.key].call(this, constraintPair.value, validationPair);
};
return Validator;
}());
exports.Validator = Validator;