java-bean-validation.js
Version:
Java Bean Validation implementation for JavaScript
272 lines • 9.11 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var core_1 = require("./core");
var MAX_INTEGER_VALUE = 2147483647;
var validator;
core_1.VALIDATORS['AssertFalse'] = function AssertFalseValidator(value) {
if (value === null || value === undefined) {
return true;
}
if (!(value === true || value === false)) {
return true;
}
return value === false;
};
core_1.VALIDATORS['AssertTrue'] = function AssertTrueValidator(value) {
if (value === null || value === undefined) {
return true;
}
if (!(value === true || value === false)) {
return true;
}
return value === true;
};
validator = function DecimalMaxValidator(value, attributes) {
if (value === null || value === undefined) {
return true;
}
if (typeof value === 'string') {
if (value === 'NaN') {
value = +value;
}
else {
value = +value;
if (isNaN(value)) {
return true;
}
}
}
if (typeof value !== 'number') {
return true;
}
var inclusive = attributes.inclusive;
if (inclusive) {
return value <= +attributes.value;
}
else {
return value < +attributes.value;
}
};
validator.defaultValues = { inclusive: true };
core_1.VALIDATORS['DecimalMax'] = validator;
validator = function DecimalMinValidator(value, attributes) {
if (value === null || value === undefined) {
return true;
}
if (typeof value === 'string') {
if (value === 'NaN') {
value = +value;
}
else {
value = +value;
if (isNaN(value)) {
return true;
}
}
}
if (typeof value !== 'number') {
return true;
}
var inclusive = attributes.inclusive;
if (inclusive) {
return value >= +attributes.value;
}
else {
return value > +attributes.value;
}
};
validator.defaultValues = { inclusive: true };
core_1.VALIDATORS['DecimalMin'] = validator;
core_1.VALIDATORS['Digits'] = function DigitsValidator(value, attributes) {
if (value === null || value === undefined) {
return true;
}
if (typeof value === 'string') {
if (value === 'NaN') {
value = +value;
}
else {
value = +value;
if (isNaN(value)) {
return true;
}
}
}
if (typeof value !== 'number') {
return true;
}
if (isNaN(value)) {
return false;
}
var split = (value + '').split('.', 2);
if (split[0].length > +attributes.integer) {
return false;
}
if (split[1] && split[1].length > +attributes.fraction) {
return false;
}
return true;
};
core_1.VALIDATORS['Future'] = function FutureValidator(value) {
if (value === null || value === undefined) {
return true;
}
if (typeof value === 'string') {
value = new Date(value);
}
if (!(value instanceof Date)) {
return true;
}
return value.getTime() > new Date().getTime();
};
core_1.VALIDATORS['Max'] = function MaxValidator(value, attributes) {
if (value === null || value === undefined) {
return true;
}
if (typeof value === 'string') {
value = +value;
if (isNaN(value)) {
return true;
}
}
if (typeof value !== 'number') {
return true;
}
if (isNaN(value)) {
return true;
}
return value <= +attributes.value;
};
core_1.VALIDATORS['Min'] = function MinValidator(value, attributes) {
if (value === null || value === undefined) {
return true;
}
if (typeof value === 'string') {
value = +value;
if (isNaN(value)) {
return true;
}
}
if (typeof value !== 'number') {
return true;
}
if (isNaN(value)) {
return true;
}
return value >= +attributes.value;
};
validator = function NotNullValidator(value) {
return value !== null && value !== undefined;
};
validator.isHtmlRequiredValidator = true;
core_1.VALIDATORS['NotNull'] = validator;
core_1.VALIDATORS['Null'] = function NullValidator(value) {
return value === null || value === undefined;
};
core_1.VALIDATORS['Past'] = function PastValidator(value) {
if (value === null || value === undefined) {
return true;
}
if (typeof value === 'string') {
value = new Date(value);
}
if (!(value instanceof Date)) {
return false;
}
return value.getTime() < new Date().getTime();
};
validator = function PatternValidator(value, attributes, constraint, path, globalViolations) {
if (value === null || value === undefined) {
return true;
}
if (typeof value === 'number') {
value = value + '';
}
if (value instanceof Date) {
value = value + '';
}
if (typeof value !== 'string') {
return true;
}
var flags = '';
var attributesFlags = attributes.flags;
for (var i = 0, length_1 = attributesFlags.length; i < length_1; i++) {
var flag = attributesFlags[i];
if (flag === 'UNIX_LINES' || flag === 0) {
var violatedConstraint = { constraintName: 'InvalidConstraintAttributeValue', attributes: { constraintName: constraint.constraintName, attributeName: 'flags', attributeValue: flag, description: 'Pattert Validator: Unix lines flag not supported' } };
core_1.addViolation(violatedConstraint, value, path, globalViolations);
return false;
}
if (flag === 'CASE_INSENSITIVE' || flag === 1) {
flags = flags + 'i';
}
if (flag === 'COMMENTS' || flag === 2) {
var violatedConstraint = { constraintName: 'InvalidConstraintAttributeValue', attributes: { constraintName: constraint.constraintName, attributeName: 'flags', attributeValue: flag, description: 'Pattert Validator: Comments flag not supported' } };
core_1.addViolation(violatedConstraint, value, path, globalViolations);
return false;
}
if (flag === 'MULTILINE' || flag === 3) {
flags = flags + 'm';
}
if (flag === 'DOTALL' || flag === 4) {
var violatedConstraint = { constraintName: 'InvalidConstraintAttributeValue', attributes: { constraintName: constraint.constraintName, attributeName: 'flags', attributeValue: flag, description: 'Pattert Validator: Dot all flag not supported' } };
core_1.addViolation(violatedConstraint, value, path, globalViolations);
return false;
}
if (flag === 'UNICODE_CASE' || flag === 5) {
var violatedConstraint = { constraintName: 'InvalidConstraintAttributeValue', attributes: { constraintName: constraint.constraintName, attributeName: 'flags', attributeValue: flag, description: 'Pattert Validator: Unicode case flag not supported' } };
core_1.addViolation(violatedConstraint, value, path, globalViolations);
return false;
}
if (flag === 'CANON_EQ' || flag === 6) {
var violatedConstraint = { constraintName: 'InvalidConstraintAttributeValue', attributes: { constraintName: constraint.constraintName, attributeName: 'flags', attributeValue: flag, description: 'Pattert Validator: CanonEq flag not supported' } };
core_1.addViolation(violatedConstraint, value, path, globalViolations);
return false;
}
else {
var violatedConstraint = { constraintName: 'InvalidConstraintAttributeValue', attributes: { constraintName: constraint.constraintName, attributeName: 'flags', attributeValue: flag, description: 'Pattern Validator: Invalid Flag type' } };
core_1.addViolation(violatedConstraint, value, path, globalViolations);
return false;
}
}
var regex = new RegExp(attributes.regexp, flags);
return value.search(regex) >= 0;
};
validator.defaultValues = { flags: [] };
core_1.VALIDATORS['Pattern'] = validator;
validator = function SizeValidator(value, attributes) {
if (value === null || value === undefined) {
return true;
}
var min = attributes.min;
var max = attributes.max;
var length = -1;
if (Array.isArray(value)) {
length = value.length;
}
else if (typeof value === 'object') {
length = 0;
var key = void 0;
for (key in value) {
if (value.hasOwnProperty(key) && key[0] !== '$') {
length++;
}
}
}
else if (typeof value === 'string') {
length = value.length;
}
else if (typeof value === 'number') {
length = (value + '').length;
}
else if (value instanceof Date) {
length = (value + '').length;
}
else {
return true;
}
return +min <= length && +max >= length;
};
validator.defaultValues = { min: 0, max: MAX_INTEGER_VALUE };
core_1.VALIDATORS['Size'] = validator;
//# sourceMappingURL=validators.js.map