UNPKG

java-bean-validation.js

Version:
194 lines (174 loc) 5.55 kB
import { VALIDATORS, Validator, TypeName } from "./core"; let validator: Validator; /* ******************************************************************************************** * Validators for data types */ validator = function ObjectValidator(value: any): boolean { if (value === null || value === undefined) { return true; } return typeof value === 'object'; }; validator.isGlobalValidator = true; VALIDATORS['Object'] = validator; validator = function EnumValidator(value: any, attributes: { values?: (number | string)[] }): boolean { // Automatic for enums, generated by Java if (value === null || value === undefined) { return true; } if (!attributes.values) { return false; } value = '' + value; if (value === '') { return true; } return attributes.values.indexOf(value) >= 0; }; validator.isGlobalValidator = true; VALIDATORS['Enum'] = validator; validator = function IntegerNumberValidator(value: any): boolean { if (value === null || value === undefined) { return true; } if (typeof value === 'string') { if (value === '') { return true; } value = +value; } return typeof value === 'number' && !isNaN(value) && isFinite(value) && (value % 1 === 0); }; validator.isGlobalValidator = true; VALIDATORS['IntegerNumber'] = validator; validator = function FloatNumberValidator(value: any): boolean { if (value === null || value === undefined) { return true; } if (typeof value === 'string') { if (value === '') { return true; } value = +value; if (isNaN(value)) { return false; } } return typeof value === 'number'; }; validator.isGlobalValidator = true; VALIDATORS['FloatNumber'] = validator; validator = function StringValidator(value: any): boolean { if (value === null || value === undefined) { return true; } if (typeof value === 'number') { return true; } if (value instanceof Date) { return true; } return typeof value === 'string'; }; validator.isGlobalValidator = true; VALIDATORS['String'] = validator; validator = function BooleanValidator(value: any): boolean { if (value === null || value === undefined) { return true; } return value === true || value === false; }; validator.isGlobalValidator = true; VALIDATORS['Boolean'] = validator; validator = function DateValidator(value: any): boolean { if (value === null || value === undefined) { return true; } if (typeof value === 'string') { value = new Date(value); } if (!(value instanceof Date)) { return false; } return !isNaN(value.getTime()); }; validator.isGlobalValidator = true; VALIDATORS['Date'] = validator; validator = function TimeValidator(value: any): boolean { if (value === null || value === undefined) { return true; } if (typeof value === 'string') { value = new Date(value); } if (!(value instanceof Date)) { return false; } return !isNaN(value.getTime()); }; validator.isGlobalValidator = true; VALIDATORS['Time'] = validator; validator = function TimestampValidator(value: any): boolean { if (value === null || value === undefined) { return true; } if (typeof value === 'string') { value = new Date(value); } if (!(value instanceof Date)) { return false; } return !isNaN(value.getTime()); }; validator.isGlobalValidator = true; VALIDATORS['Timestamp'] = validator; validator = function ListValidator(value: any): boolean { if (value === null || value === undefined) { return true; } return Array.isArray(value); }; validator.isGlobalValidator = true; VALIDATORS['List'] = validator; validator = function MapValidator(value: any): boolean { if (value === null || value === undefined) { return true; } if (!Array.isArray(value)) { return false; } return typeof value === 'object'; }; validator.isGlobalValidator = true; VALIDATORS['Map'] = validator; /* ******************************************************************************************** * Other useful validators */ validator = function InvalidNumberGenenricArgumentsValidator(_value: any, _attributes: { typeName: string, expected: number, found: number }): boolean { return false; }; validator.isGlobalValidator = true; VALIDATORS['InvalidNumberGenenricArguments'] = validator; validator = function TypeNotFoundValidator(_value: any, _attributes: { typeName: TypeName }): boolean { return false; }; validator.isGlobalValidator = true; VALIDATORS['TypeNotFound'] = validator; validator = function PropertyNotFoundValidator(_value: any, _attributes: { typeName: TypeName, propertyName: string }): boolean { return false; }; validator.isGlobalValidator = true; VALIDATORS['PropertyNotFound'] = validator; validator = function ValidatorNotFoundValidator(_value: any, _attributes: { constraintName: string }): boolean { return false; }; validator.isGlobalValidator = true; VALIDATORS['ValidatorNotFound'] = validator; validator = function InvalidConstraintAttributeValueValidator(_value: any, _attributes: { constraintName: string, attributeName: string, attributeValue: any, description: string }): boolean { return false; }; validator.isGlobalValidator = true; VALIDATORS['InvalidConstraintAttributeValue'] = validator;