UNPKG

lex-model-validator

Version:
131 lines (130 loc) 5.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); // tslint:disable-next-line var required = function (constraintValue, validationPair) { var value = validationPair.value, key = validationPair.key; if ((constraintValue && typeof value === 'undefined') || value === null) { this.pushError("'" + key + "' is required but 'undefined'"); } }; // tslint:disable-next-line var validate = function (constraintValue, validationPair) { constraintValue.call(this, validationPair); }; // tslint:disable-next-line var minLength = function (constraintValue, validationPair) { var value = validationPair.value, key = validationPair.key; if (Array.isArray(value) || typeof value === 'string' || typeof value === 'number') { var length_1 = typeof value === 'number' ? String(value).length : value.length; if (length_1 < constraintValue) { this.pushError("'" + key + "' has to be at least " + constraintValue + " characters long but it's length is " + length_1); } } else { // this.pushError(`'${key}' is not a type that can have a minimum length`); } }; // tslint:disable-next-line var maxLength = function (constraintValue, validationPair) { var value = validationPair.value, key = validationPair.key; if (Array.isArray(value) || typeof value === 'string' || typeof value === 'number') { var length_2 = typeof value === 'number' ? String(value).length : value.length; if (length_2 > constraintValue) { this.pushError("'" + key + "' has to be lower than or equal to " + constraintValue + " characters long but it's length is " + length_2); } } else { // this.pushError(`'${key}' is not a type that can have a maximum length`); } }; // tslint:disable-next-line var type = function (constraintValue, validationPair) { var value = validationPair.value, key = validationPair.key; var valueType = typeof value; if (value && valueType !== constraintValue) { this.pushError("'" + key + "' is supposed to be of type '" + constraintValue + "' but is type '" + valueType + "'"); } }; // tslint:disable-next-line var isArray = function (constraintValue, validationPair) { var value = validationPair.value, key = validationPair.key; if (value && constraintValue !== Array.isArray(value)) { this.pushError("'" + key + "' is " + (constraintValue ? "supposed to be an array but is not" : "not supposed to be an array but is")); } }; // tslint:disable-next-line var min = function (constraintValue, validationPair) { var value = validationPair.value, key = validationPair.key; if (typeof value === 'number' || typeof value === 'string') { var numberValue = typeof value === 'string' ? +value : value; if (numberValue < constraintValue) { this.pushError("'" + key + "' has to be at least " + constraintValue + " but is " + numberValue); } } else { // this.pushError(`'${key}' is a type that cannot have a minimum value.`); } }; // tslint:disable-next-line var max = function (constraintValue, validationPair) { var value = validationPair.value, key = validationPair.key; if (typeof value === 'number' || typeof value === 'string') { var numberValue = typeof value === 'string' ? +value : value; if (numberValue > constraintValue) { this.pushError("'" + key + "' has to be lower than or equal to " + constraintValue + " but is " + numberValue); } } else { // this.pushError(`'${key}' is a type that cannot have a maximum value.`); } }; // tslint:disable-next-line var equals = function (constraintValue, validationPair) { var value = validationPair.value, key = validationPair.key; if (value !== constraintValue) { this.pushError("'" + key + "' has to be equal to '" + JSON.stringify(constraintValue, undefined, 2) + "' but is '" + JSON.stringify(value, undefined, 2) + "'"); } }; // tslint:disable-next-line var matches = function (constraintValue, validationPair) { var value = validationPair.value, key = validationPair.key; if (typeof value === 'string' || typeof value === 'number') { var stringValue = typeof value === 'number' ? String(value) : value; if (typeof constraintValue === 'string' || constraintValue instanceof RegExp) { var regexp = typeof constraintValue === 'string' ? new RegExp(constraintValue) : constraintValue; if (!regexp.test(stringValue)) { this.pushError("'" + key + "' has to match regexp '" + regexp.source + "' but is '" + stringValue + "'"); } } else if (Array.isArray(constraintValue) && !constraintValue.includes(stringValue)) { var allowedValuesString_1 = ''; constraintValue.forEach(function (val, index) { allowedValuesString_1 += "'" + val + "'" + (index !== constraintValue.length - 1) + " ? ', ' : ''"; }); this.pushError("'" + key + "' has to be one of the following values " + allowedValuesString_1 + " but is '" + stringValue + "'"); } } }; exports.CONSTRAINT_VALIDATION_FUNCTION_MAP = { required: required, validate: validate, minLength: minLength, maxLength: maxLength, type: type, isArray: isArray, min: min, max: max, equals: equals, matches: matches };