UNPKG

java-bean-validation.js

Version:
300 lines 11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var core_1 = require("../core/core"); var MAX_INTEGER_VALUE = 2147483647; var validator; function modCheckBase(value, attributes, isCheckDigitValid, checkDigitIndex, constraint, path, globalViolations) { var startIndex = attributes.startIndex; var endIndex = attributes.endIndex; var ignoreNonDigitCharacters = attributes.ignoreNonDigitCharacters; var digitsAsString; if (endIndex === MAX_INTEGER_VALUE) { digitsAsString = value.substring(0, value.length - 1); } else if (checkDigitIndex === -1) { digitsAsString = value.substring(startIndex, endIndex); } else { digitsAsString = value.substring(startIndex, endIndex + 1); } if (digitsAsString.length <= 0) { return false; } var checkDigit; if (checkDigitIndex === -1) { if (endIndex === MAX_INTEGER_VALUE) { checkDigit = value.charAt(value.length - 1); } else { checkDigit = value.charAt(endIndex); } } else { checkDigit = value.charAt(checkDigitIndex); } if (checkDigit.length <= 0) { return false; } if (ignoreNonDigitCharacters) { digitsAsString = digitsAsString.replace(/[^0-9]+/g, ''); } var digits = []; for (var i = 0, length_1 = digitsAsString.length; i < length_1; i++) { var digit = +digitsAsString.charAt(i); if (isNaN(digit)) { return false; } digits.push(digit); } return isCheckDigitValid(digits, checkDigit, attributes, value, constraint, path, globalViolations); } function luhnCheckValidator(digits, checkDigit) { var modResult = calculateLuhnMod10Check(digits); var checkValue = +checkDigit; if (!isNaN(checkValue)) { return false; } return checkValue === modResult; } function mod10CheckValidator(digits, checkDigit, attributes) { var multiplier = attributes.multiplier; var weight = attributes.weight; var modResult = calculateMod10Check(digits, multiplier, weight); var checkValue = +checkDigit; if (!isNaN(checkValue)) { return false; } return checkValue === modResult; } function mod11CheckValidator(digits, checkDigit, attributes, value, constraint, path, globalViolations, customWeights) { if (customWeights === void 0) { customWeights = []; } var processingDirection = attributes.processingDirection; var threshold = attributes.threshold; var treatCheck10As = attributes.treatCheck10As; var treatCheck11As = attributes.treatCheck11As; var reverseOrder; if (processingDirection === 'LEFT_TO_RIGHT' || processingDirection === 1) { reverseOrder = true; } else if (processingDirection === 'RIGHT_TO_LEFT' || processingDirection === 0) { reverseOrder = false; } else { var violatedConstraint = { constraintName: 'InvalidConstraintAttributeValue', attributes: { constraintName: constraint.constraintName, attributeName: 'processingDirection', attributeValue: processingDirection, description: 'Mod11Check Validator: Invalid processingDirection type' } }; core_1.addViolation(violatedConstraint, value, path, globalViolations); return false; } if (reverseOrder) { digits.reverse(); } var modResult = calculateModXCheckWithWeights.apply(void 0, [digits, 11, threshold].concat(customWeights)); switch (modResult) { case 10: return checkDigit === treatCheck10As; case 11: return checkDigit === treatCheck11As; default: { var checkValue = +checkDigit; if (!isNaN(checkValue)) { return false; } return checkValue === modResult; } } } function modCheckValidator(digits, checkDigit, attributes, value, constraint, path, globalViolations) { var modType = attributes.modType; var multiplier = attributes.multiplier; var modResult = -1; if (modType === 'MOD11' || modType === 1) { modResult = calculateMod11Check(digits, multiplier); if (modResult === 10 || modResult === 11) { modResult = 0; } } else if (modType === 'MOD10' || modType === 0) { modResult = calculateLuhnMod10Check(digits); } else { var violatedConstraint = { constraintName: 'InvalidConstraintAttributeValue', attributes: { constraintName: constraint.constraintName, attributeName: 'modType', attributeValue: modType, description: 'ModCheck Validator: Invalid modType type' } }; core_1.addViolation(violatedConstraint, value, path, globalViolations); return false; } var checkValue = +checkDigit; if (!isNaN(checkValue)) { return false; } return checkValue === modResult; } function calculateLuhnMod10Check(digits) { var sum = 0; var even = true; for (var index = digits.length - 1; index >= 0; index--) { var digit = digits[index]; if (even) { digit <<= 1; } if (digit > 9) { digit -= 9; } sum += digit; even = !even; } return (10 - (sum % 10)) % 10; } function calculateMod10Check(digits, multiplier, weight) { var sum = 0; var even = true; for (var index = digits.length - 1; index >= 0; index--) { var digit = digits[index]; if (even) { digit *= multiplier; } else { digit *= weight; } sum += digit; even = !even; } return (10 - (sum % 10)) % 10; } function calculateMod11Check(digits, threshold) { if (threshold === void 0) { threshold = MAX_INTEGER_VALUE; } var sum = 0; var multiplier = 2; for (var index = digits.length - 1; index >= 0; index--) { sum += digits[index] * multiplier++; if (multiplier > threshold) { multiplier = 2; } } return 11 - (sum % 11); } function calculateModXCheckWithWeights(digits, moduloParam, threshold) { var weights = []; for (var _i = 3; _i < arguments.length; _i++) { weights[_i - 3] = arguments[_i]; } var sum = 0; var multiplier = 1; for (var index = digits.length - 1; index >= 0; index--) { if (weights.length != 0) { multiplier = weights[weights.length - index % weights.length - 1]; } else { multiplier++; if (multiplier > threshold) { multiplier = 2; } } sum += digits[index] * multiplier; } return moduloParam - (sum % moduloParam); } core_1.VALIDATORS['CreditCardNumber'] = function CreditCardNumberValidator(_value) { return true; }; validator = function EANValidator(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 type = attributes.type; if (type === 'EAN13' || type === 0) { return value.length === 13; } else if (type === 'EAN8' || type === 1) { return value.length === 8; } else { var violatedConstraint = { constraintName: 'InvalidConstraintAttributeValue', attributes: { constraintName: constraint.constraintName, attributeName: 'type', attributeValue: type, description: 'EAN Validator: Invalid EAN type' } }; core_1.addViolation(violatedConstraint, value, path, globalViolations); return false; } }; validator.defaultValues = { type: 'EAN13' }; core_1.VALIDATORS['EAN'] = validator; validator = function LuhnCheckValidator(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 checkDigitIndex = attributes.checkDigitIndex; return modCheckBase(value, attributes, luhnCheckValidator, checkDigitIndex, constraint, path, globalViolations); }; validator.defaultValues = { startIndex: 0, endIndex: MAX_INTEGER_VALUE, checkDigitIndex: -1, ignoreNonDigitCharacters: true }; core_1.VALIDATORS['LuhnCheck'] = validator; validator = function Mod10CheckValidator(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 checkDigitIndex = attributes.checkDigitIndex; return modCheckBase(value, attributes, mod10CheckValidator, checkDigitIndex, constraint, path, globalViolations); }; validator.defaultValues = { multiplier: 3, weight: 1, startIndex: 0, endIndex: MAX_INTEGER_VALUE, checkDigitIndex: -1, ignoreNonDigitCharacters: true }; core_1.VALIDATORS['Mod10Check'] = validator; validator = function Mod11CheckValidator(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 checkDigitIndex = attributes.checkDigitIndex; return modCheckBase(value, attributes, mod11CheckValidator, checkDigitIndex, constraint, path, globalViolations); }; validator.defaultValues = { threshold: MAX_INTEGER_VALUE, startIndex: 0, endIndex: MAX_INTEGER_VALUE, checkDigitIndex: -1, ignoreNonDigitCharacters: false, treatCheck10As: 'X', treatCheck11As: '0', processingDirection: 'RIGHT_TO_LEFT' }; core_1.VALIDATORS['Mod11Check'] = validator; validator = function ModCheckValidator(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 checkDigitPosition = attributes.checkDigitPosition; if (checkDigitPosition === undefined) { checkDigitPosition = -1; } return modCheckBase(value, attributes, modCheckValidator, checkDigitPosition, constraint, path, globalViolations); }; validator.defaultValues = { startIndex: 0, endIndex: MAX_INTEGER_VALUE, checkDigitPosition: -1, ignoreNonDigitCharacters: true }; core_1.VALIDATORS['ModCheck'] = validator; //# sourceMappingURL=modCheckValidators.js.map