UNPKG

@jsbailey/reactive-form-validators

Version:

[![npm version](https://badge.fury.io/js/%40rxweb%2Freactive-form-validators.svg)](https://badge.fury.io/js/%40rxweb%2Freactive-form-validators) [![Gitter](https://badges.gitter.im/rx-web/Lobby.svg)](https://gitter.im/rxweb-project/rxweb?utm_source=badge

1,368 lines (1,324 loc) 314 kB
import { isNumeric } from 'rxjs/internal-compatibility'; import { Injectable, NgModule } from '@angular/core'; import { FormBuilder, FormsModule, ReactiveFormsModule } from '@angular/forms'; /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ class CreditCardRegex { constructor() { this.Visa = new RegExp('^(?:4[0-9]{12})(?:[0-9]{3})?$'); this.AmericanExpress = new RegExp('^(?:3[47][0-9]{13})$'); this.Maestro = new RegExp('^(?:(?:5[0678]\\d\\d|6304|6390|67\\d\\d)\\d{8,15})$'); this.JCB = new RegExp('^(?:(?:2131|1800|35\\d{3})\\d{11})$'); this.Discover = new RegExp('^(?:6(?:011|5[0-9]{2})(?:[0-9]{12}))$'); this.DinersClub = new RegExp('^(?:3(?:0[0-5]|[68][0-9])[0-9]{11})$'); this.MasterCard = new RegExp('^(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}$'); } } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ /** @type {?} */ const RegExRule = { alpha: /^[a-zA-Z]+$/, alphaWithSpace: /^[a-zA-Z\s]+$/, onlyDigit: /^[0-9]+$/, isDigitExits: /\d/g, lowerCase: /[a-z]/g, upperCase: /[A-Z]/g, specialCharacter: /[!@#$%^&*(),.?":{}|<>]/g, advancedEmail: /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/, basicEmail: /^(([^<>()\[\]\\.,,:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/, alphaNumeric: /^[0-9a-zA-Z]+$/, alphaNumericWithSpace: /^[0-9a-zA-Z\s]+$/, hexColor: /#([a-f0-9]{3}|[a-f0-9]{4}(?:[a-f0-9]{2}){0,2})\b/gi, strictHexColor: /^#([a-f0-9]{3,4}|[a-f0-9]{4}(?:[a-f0-9]{2}){1,2})\b$/i, float: /^(?:[-+]?(?:[0-9]+))?(?:\.[0-9]*)?(?:[eE][\+\-]?(?:[0-9]+))?$/, decimal: /^[-+]?([0-9]+|\.[0-9]+|[0-9]+\.[0-9]+)$/, hexaDecimal: /^[0-9A-F]+$/i, date: /^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/, time: /(00|[0-9]|1[0-9]|2[0-3]):([0-9]|[0-5][0-9])$/, url: /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/, creditCard: new CreditCardRegex(), }; /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ /** @type {?} */ const ALPHABET = "alphabet"; /** @type {?} */ const DIGIT = "digit"; /** @type {?} */ const CONTAINS = "contains"; /** @type {?} */ const LOWERCASE = "lowerCase"; /** @type {?} */ const UPPERCASE = "upperCase"; /** @type {?} */ const SPECIAL_CHARACTER = "specialCharacter"; /** @type {?} */ const MIN_LENGTH = "minLength"; /** @type {?} */ const MAX_LENGTH = "maxLength"; class RegexValidator { /** * @param {?} value * @param {?} regex * @return {?} */ static isExits(value, regex) { return value.match(regex) != null; } /** * @param {?} value * @param {?} regex * @return {?} */ static isValid(value, regex) { return regex.test(value); } /** * @param {?} value * @return {?} */ static isNotBlank(value) { return value != undefined && value != "" && value != null; } /** * @param {?} passwordValidation * @param {?} value * @return {?} */ static isValidPassword(passwordValidation, value) { /** @type {?} */ let isValid = false; /** @type {?} */ let keyName = "status"; /** @type {?} */ let objectProperties = Object.getOwnPropertyNames(passwordValidation); for (let propertyName of objectProperties) { switch (propertyName) { case ALPHABET: isValid = RegexValidator.isExits(value, RegExRule["alpha"]); keyName = ALPHABET; break; case DIGIT: isValid = RegexValidator.isValid(value, RegExRule["isDigitExits"]); keyName = DIGIT; break; case CONTAINS: isValid = value.indexOf(passwordValidation[CONTAINS]) != -1; keyName = CONTAINS; break; case LOWERCASE: isValid = RegexValidator.isValid(value, RegExRule["lowerCase"]); keyName = LOWERCASE; break; case UPPERCASE: isValid = RegexValidator.isValid(value, RegExRule["upperCase"]); keyName = UPPERCASE; break; case SPECIAL_CHARACTER: isValid = RegexValidator.isExits(value, RegExRule["specialCharacter"]); keyName = SPECIAL_CHARACTER; break; case MIN_LENGTH: isValid = value.length >= passwordValidation[propertyName]; keyName = MIN_LENGTH; break; case MAX_LENGTH: isValid = value.length <= passwordValidation[propertyName]; keyName = MAX_LENGTH; break; } if (!isValid) break; } return { isValid: isValid, keyName: keyName }; } } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ class ReactiveFormConfig { /** * @param {?} jObject * @return {?} */ static set(jObject) { if (jObject) ReactiveFormConfig.json = jObject; } } ReactiveFormConfig.json = {}; /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ class ObjectMaker { /** * @param {?} key * @param {?} message * @param {?} values * @return {?} */ static toJson(key, message, values) { /** @type {?} */ let messageText = (message) ? message : (ReactiveFormConfig && ReactiveFormConfig.json && ReactiveFormConfig.json["validationMessage"] && ReactiveFormConfig.json["validationMessage"][key]) ? ReactiveFormConfig.json["validationMessage"][key] : ''; values.forEach((t, index) => { messageText = messageText.replace(`{{${index}}}`, t); }); /** @type {?} */ let jObject = {}; jObject[key] = { message: messageText, refValues: values }; return jObject; } /** * @return {?} */ static null() { return null; } } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ class Linq { /** * @param {?} expression * @return {?} */ static functionCreator(expression) { /** @type {?} */ var functionSetter = []; /** @type {?} */ var match = expression.match(/^\s*\(?\s*([^)]*)\s*\)?\s*=>(.*)/); /** @type {?} */ var splitSelect = match[2].split(","); for (var i = 0; i < splitSelect.length; i++) { /** @type {?} */ var equalToOperator = splitSelect[i].match(/^\s*\(?\s*([^)]*)\s*\)?\s*==(.*)/); if (equalToOperator !== null) { functionSetter = new Function(match[1], "return " + equalToOperator[0]); } else { equalToOperator = splitSelect[i].match(/^\s*\(?\s*([^)]*)\s*\)?\s*=(.*)/); if (equalToOperator === null) { functionSetter = new Function(match[1], "return " + splitSelect[i]); } else { functionSetter = new Function(match[1], "return " + equalToOperator[2]); } } } if (splitSelect.length == 0) functionSetter = { accessFunction: new Function(match[1], "return " + match[2]) }; return functionSetter; } /** * @param {?} jObject * @param {?} expression * @param {?} parentObject * @return {?} */ static IsPassed(jObject, expression, parentObject) { /** @type {?} */ let expressionFunction = expression; if (parentObject && typeof expression == "string") expressionFunction = Linq.functionCreator(expression); if (parentObject && expressionFunction) return expressionFunction(parentObject, jObject); return true; } /** * @param {?} expression * @return {?} */ static expressionColumns(expression) { /** @type {?} */ var columns = []; /** @type {?} */ let splitExpressions = []; if (typeof expression == "string") expression.split("=>")[1].split(" && ").forEach(t => { t.split(" || ").forEach(x => { splitExpressions.push(x.trim().split(' ')[0]); }); }); else String(expression).split(" return ")[1].split(" && ").forEach(t => { t.split(" || ").forEach(x => { splitExpressions.push(x.trim().split(' ')[0]); }); }); splitExpressions.forEach(t => { /** @type {?} */ var splitText = t.split('.'); if (splitText.length == 2) columns.push({ propName: splitText[1].trim() }); else { /** @type {?} */ var arrayProp = splitText[1].split('['); /** @type {?} */ let jObject = { propName: splitText[splitText.length - 1].trim(), objectPropName: arrayProp[0], arrayIndex: arrayProp.length > 1 ? arrayProp[1].replace("]", "") : undefined }; columns.push(jObject); } }); return columns; } } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ class ApplicationUtil { /** * @param {?} control * @return {?} */ static getParentObjectValue(control) { if (control.parent) { /** @type {?} */ let parent = this.parentObjectValue(control.parent); return parent.value; } return {}; } /** * @param {?} control * @return {?} */ static parentObjectValue(control) { if (!control.parent) return control; else control = this.parentObjectValue(control.parent); return control; } /** * @param {?} config * @return {?} */ static getConfigObject(config) { return (config != undefined && config != true) ? config : {}; } } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ /** @type {?} */ const AnnotationTypes = { numeric: 'numeric', required: 'required', minLength: 'minLength', maxLength: 'maxLength', minNumber: 'minNumber', maxNumber: 'maxNumber', pattern: 'pattern', password: 'password', compare: 'compare', minDate: 'minDate', maxDate: 'maxDate', alpha: 'alpha', alphaNumeric: 'alphaNumeric', email: 'email', hexColor: 'hexColor', lowerCase: 'lowerCase', url: 'url', upperCase: 'upperCase', nested: 'nested', propArray: 'propArray', propObject: 'propObject', contains: 'contains', range: 'range', custom: 'custom', digit: "digit", creditCard: "creditCard", time: "time", json: "json", greaterThan: "greaterThan", greaterThanEqualTo: "greaterThanEqualTo", lessThan: "lessThan", lessThanEqualTo: "lessThanEqualTo" }; /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ /** * @param {?} config * @param {?} conditionalValidationProps * @return {?} */ function alphaValidator(config, conditionalValidationProps) { return (control) => { /** @type {?} */ const controlValue = control.value; /** @type {?} */ const formGroupValue = ApplicationUtil.getParentObjectValue(control); config = ApplicationUtil.getConfigObject(config); /** @type {?} */ const parentObject = (control.parent) ? control.parent.value : undefined; if (Linq.IsPassed(formGroupValue, config.conditionalExpression, parentObject)) { if (RegexValidator.isNotBlank(controlValue)) { /** @type {?} */ var testResult = false; if (!config.allowWhiteSpace) testResult = RegexValidator.isValid(controlValue, RegExRule["alpha"]); else testResult = RegexValidator.isValid(controlValue, RegExRule["alphaWithSpace"]); if (!testResult) return ObjectMaker.toJson(AnnotationTypes["alpha"], config.message || null, [controlValue]); } } return ObjectMaker.null(); }; } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ /** * @param {?} config * @return {?} */ function alphaNumericValidator(config) { return (control) => { /** @type {?} */ const controlValue = control.value; /** @type {?} */ const formGroupValue = ApplicationUtil.getParentObjectValue(control); config = ApplicationUtil.getConfigObject(config); /** @type {?} */ const parentObject = (control.parent) ? control.parent.value : undefined; if (Linq.IsPassed(formGroupValue, config.conditionalExpression, parentObject)) { if (RegexValidator.isNotBlank(controlValue)) { /** @type {?} */ var testResult = true; if (!config.allowWhiteSpace) testResult = RegexValidator.isValid(controlValue, RegExRule["alphaNumeric"]); else testResult = RegexValidator.isValid(controlValue, RegExRule["alphaNumericWithSpace"]); if (!testResult) return ObjectMaker.toJson(AnnotationTypes["alphaNumeric"], config.message || null, [controlValue]); } } return ObjectMaker.null(); }; } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ /** * @param {?} config * @return {?} */ function compareValidator(config) { return (control) => { /** @type {?} */ const compareControl = control.root.get([config.fieldName]); /** @type {?} */ const controlValue = control.value; /** @type {?} */ const compareControlValue = (compareControl) ? compareControl.value : ''; if (RegexValidator.isNotBlank(controlValue)) { if (!(compareControl && compareControl.value === controlValue)) return ObjectMaker.toJson(AnnotationTypes["compare"], config.message || null, [controlValue, compareControlValue]); } return ObjectMaker.null(); }; } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ /** * @param {?} config * @return {?} */ function containsValidator(config) { return (control) => { /** @type {?} */ const controlValue = control.value; /** @type {?} */ const formGroupValue = ApplicationUtil.getParentObjectValue(control); config = ApplicationUtil.getConfigObject(config); /** @type {?} */ const parentObject = (control.parent) ? control.parent.value : undefined; if (Linq.IsPassed(formGroupValue, config.conditionalExpression, parentObject)) { if (RegexValidator.isNotBlank(controlValue)) { if (controlValue.indexOf(config.value) == -1) return ObjectMaker.toJson(AnnotationTypes["contains"], config.message || null, [config.value, controlValue]); } } return ObjectMaker.null(); }; } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ /** @enum {number} */ const CreditCardType = { Visa: 1, AmericanExpress: 2, Maestro: 3, JCB: 4, Discover: 5, DinersClub: 6, MasterCard: 7, }; CreditCardType[CreditCardType.Visa] = 'Visa'; CreditCardType[CreditCardType.AmericanExpress] = 'AmericanExpress'; CreditCardType[CreditCardType.Maestro] = 'Maestro'; CreditCardType[CreditCardType.JCB] = 'JCB'; CreditCardType[CreditCardType.Discover] = 'Discover'; CreditCardType[CreditCardType.DinersClub] = 'DinersClub'; CreditCardType[CreditCardType.MasterCard] = 'MasterCard'; /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ /** * @param {?} config * @return {?} */ function creditCardValidator(config) { return (control) => { /** @type {?} */ const controlValue = control.value; /** @type {?} */ const formGroupValue = ApplicationUtil.getParentObjectValue(control); config = ApplicationUtil.getConfigObject(config); /** @type {?} */ const parentObject = (control.parent) ? control.parent.value : undefined; if (Linq.IsPassed(formGroupValue, config.conditionalExpression, parentObject)) { if (RegexValidator.isNotBlank(controlValue)) { /** @type {?} */ let isValid = false; for (let creditCardType of config.creditCardTypes) { switch (creditCardType) { case CreditCardType.AmericanExpress: isValid = RegexValidator.isValid(controlValue, RegExRule["creditCard"].AmericanExpress); break; case CreditCardType.DinersClub: isValid = RegexValidator.isValid(controlValue, RegExRule["creditCard"].DinersClub); break; case CreditCardType.Discover: isValid = RegexValidator.isValid(controlValue, RegExRule["creditCard"].Discover); break; case CreditCardType.JCB: isValid = RegexValidator.isValid(controlValue, RegExRule["creditCard"].JCB); break; case CreditCardType.Maestro: isValid = RegexValidator.isValid(controlValue, RegExRule["creditCard"].Maestro); break; case CreditCardType.MasterCard: isValid = RegexValidator.isValid(controlValue, RegExRule["creditCard"].MasterCard); break; case CreditCardType.Visa: isValid = RegexValidator.isValid(controlValue, RegExRule["creditCard"].Visa); break; } } if (!isValid) return ObjectMaker.toJson(AnnotationTypes["creditCard"], config.message || null, [controlValue]); } } return ObjectMaker.null(); }; } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ /** * @param {?} config * @return {?} */ function digitValidator(config) { return (control) => { /** @type {?} */ const controlValue = control.value; /** @type {?} */ const formGroupValue = ApplicationUtil.getParentObjectValue(control); config = ApplicationUtil.getConfigObject(config); /** @type {?} */ const parentObject = (control.parent) ? control.parent.value : undefined; if (Linq.IsPassed(formGroupValue, config.conditionalExpression, parentObject)) { if (RegexValidator.isNotBlank(controlValue)) { if (!RegexValidator.isValid(controlValue, RegExRule["onlyDigit"])) return ObjectMaker.toJson(AnnotationTypes["digit"], config.message || null, [controlValue]); } } return ObjectMaker.null(); }; } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ class DateProvider { /** * @return {?} */ regex() { /** @type {?} */ var regExp; if (ReactiveFormConfig && ReactiveFormConfig.json && ReactiveFormConfig.json["internationalization"] && ReactiveFormConfig.json["internationalization"].dateFormat && ReactiveFormConfig.json["internationalization"].seperator) { /** @type {?} */ var seperator = ReactiveFormConfig.json["internationalization"].seperator; switch (ReactiveFormConfig.json["internationalization"].dateFormat) { case 'ymd': regExp = /^(\d{4}-\d{1,2}-\d{1,2})$/; break; case 'dmy': case 'mdy': regExp = /^(\d{1,2}-\d{1,2}-\d{4})$/; break; } } return regExp; } /** * @param {?} value * @return {?} */ getDate(value) { /** @type {?} */ let year; if (ReactiveFormConfig && ReactiveFormConfig.json && ReactiveFormConfig.json["internationalization"] && ReactiveFormConfig.json["internationalization"].dateFormat && ReactiveFormConfig.json["internationalization"].seperator) { /** @type {?} */ var seperator = ReactiveFormConfig.json["internationalization"].seperator; switch (ReactiveFormConfig.json["internationalization"].dateFormat) { case 'ymd': [year, month, day] = value.split(seperator).map((val) => +val); break; case 'dmy': [day, month, year] = value.split(seperator).map((val) => +val); break; case 'mdy': [month, day, year] = value.split(seperator).map((val) => +val); break; } } return new Date(year, month - 1, day); } /** * @param {?} value * @return {?} */ isValid(value) { value = value.replace(ReactiveFormConfig.json["internationalization"].seperator, '-').replace(ReactiveFormConfig.json["internationalization"].seperator, '-'); return this.regex().test(value); } } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ /** * @param {?} config * @return {?} */ function emailValidator(config) { return (control) => { /** @type {?} */ const controlValue = control.value; config = ApplicationUtil.getConfigObject(config); /** @type {?} */ const formGroupValue = ApplicationUtil.getParentObjectValue(control); /** @type {?} */ const parentObject = (control.parent) ? control.parent.value : undefined; if (Linq.IsPassed(formGroupValue, config.conditionalExpression, parentObject)) { if (RegexValidator.isNotBlank(controlValue)) { if (!RegexValidator.isValid(controlValue, RegExRule["basicEmail"])) return ObjectMaker.toJson(AnnotationTypes["email"], config.message || null, [controlValue]); } } return ObjectMaker.null(); }; } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ /** * @param {?} config * @return {?} */ function hexColorValidator(config) { return (control) => { /** @type {?} */ const controlValue = control.value; /** @type {?} */ const formGroupValue = ApplicationUtil.getParentObjectValue(control); config = ApplicationUtil.getConfigObject(config); /** @type {?} */ const parentObject = (control.parent) ? control.parent.value : undefined; if (Linq.IsPassed(formGroupValue, config.conditionalExpression, parentObject)) { if (RegexValidator.isNotBlank(controlValue)) { /** @type {?} */ let hexRegex = config.isStrict ? RegExRule["strictHexColor"] : RegExRule["hexColor"]; if (!RegexValidator.isValid(controlValue, hexRegex)) return ObjectMaker.toJson(AnnotationTypes["hexColor"], config.message || null, [controlValue]); } } return ObjectMaker.null(); }; } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ /** * @param {?} config * @return {?} */ function lowercaseValidator(config) { return (control) => { /** @type {?} */ const controlValue = control.value; /** @type {?} */ const formGroupValue = ApplicationUtil.getParentObjectValue(control); config = ApplicationUtil.getConfigObject(config); /** @type {?} */ const parentObject = (control.parent) ? control.parent.value : undefined; if (Linq.IsPassed(formGroupValue, config.conditionalExpression, parentObject)) { if (RegexValidator.isNotBlank(controlValue)) { if (!(controlValue === controlValue.toLowerCase())) return ObjectMaker.toJson(AnnotationTypes["lowerCase"], config.message || null, [controlValue]); } } return ObjectMaker.null(); }; } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ /** * @param {?} config * @return {?} */ function maxDateValidator(config) { return (control) => { /** @type {?} */ var dateProvider = new DateProvider(); /** @type {?} */ const controlValue = control.value; /** @type {?} */ const formGroupValue = ApplicationUtil.getParentObjectValue(control); config = ApplicationUtil.getConfigObject(config); /** @type {?} */ const parentObject = (control.parent) ? control.parent.value : undefined; if (Linq.IsPassed(formGroupValue, config.conditionalExpression, parentObject)) { if (RegexValidator.isNotBlank(controlValue)) { if (dateProvider.isValid(controlValue)) { /** @type {?} */ let maxDate = config.value; /** @type {?} */ let currentValueDate = dateProvider.getDate(controlValue); if (!(maxDate >= currentValueDate)) return ObjectMaker.toJson(AnnotationTypes["maxDate"], config.message || null, [control.value]); } else return ObjectMaker.toJson(AnnotationTypes["maxDate"], config.message || null, [control.value]); } } return ObjectMaker.null(); }; } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ /** * @param {?} config * @return {?} */ function maxLengthValidator(config) { return (control) => { /** @type {?} */ const controlValue = control.value; /** @type {?} */ const formGroupValue = ApplicationUtil.getParentObjectValue(control); config = ApplicationUtil.getConfigObject(config); /** @type {?} */ const parentObject = (control.parent) ? control.parent.value : undefined; if (Linq.IsPassed(formGroupValue, config.conditionalExpression, parentObject)) { if (RegexValidator.isNotBlank(controlValue)) { if (!(controlValue.length <= config.value)) return ObjectMaker.toJson(AnnotationTypes["maxLength"], config.message || null, [controlValue]); } } return ObjectMaker.null(); }; } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ /** * @param {?} config * @return {?} */ function maxNumberValidator(config) { return (control) => { /** @type {?} */ const controlValue = control.value; /** @type {?} */ const formGroupValue = ApplicationUtil.getParentObjectValue(control); config = ApplicationUtil.getConfigObject(config); /** @type {?} */ const parentObject = (control.parent) ? control.parent.value : undefined; if (Linq.IsPassed(formGroupValue, config.conditionalExpression, parentObject)) { if (RegexValidator.isNotBlank(controlValue)) { if (!(parseFloat(controlValue) <= config.value)) return ObjectMaker.toJson(AnnotationTypes["maxNumber"], config.message || null, [controlValue]); } } return ObjectMaker.null(); }; } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ /** * @param {?} config * @return {?} */ function minDateValidator(config) { return (control) => { /** @type {?} */ var dateProvider = new DateProvider(); /** @type {?} */ const controlValue = control.value; /** @type {?} */ const formGroupValue = ApplicationUtil.getParentObjectValue(control); config = ApplicationUtil.getConfigObject(config); /** @type {?} */ const parentObject = (control.parent) ? control.parent.value : undefined; if (Linq.IsPassed(formGroupValue, config.conditionalExpression, parentObject)) { if (RegexValidator.isNotBlank(controlValue)) { if (dateProvider.isValid(controlValue)) { /** @type {?} */ let minDate = config.value; /** @type {?} */ let currentControlValue = dateProvider.getDate(controlValue); if (!(currentControlValue >= minDate)) return ObjectMaker.toJson(AnnotationTypes["minDate"], config.message || null, [control.value]); } else return ObjectMaker.toJson(AnnotationTypes["minDate"], config.message || null, [control.value]); } } return ObjectMaker.null(); }; } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ /** * @param {?} config * @return {?} */ function minLengthValidator(config) { return (control) => { /** @type {?} */ const controlValue = control.value; /** @type {?} */ const formGroupValue = ApplicationUtil.getParentObjectValue(control); config = ApplicationUtil.getConfigObject(config); /** @type {?} */ const parentObject = (control.parent) ? control.parent.value : undefined; if (Linq.IsPassed(formGroupValue, config.conditionalExpression, parentObject)) { if (RegexValidator.isNotBlank(controlValue)) { if (!(String(controlValue).length >= config.value)) return ObjectMaker.toJson(AnnotationTypes["minLength"], config.message || null, [controlValue]); } } return ObjectMaker.null(); }; } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ /** * @param {?} config * @return {?} */ function minNumberValidator(config) { return (control) => { /** @type {?} */ let controlValue = control.value; /** @type {?} */ const formGroupValue = ApplicationUtil.getParentObjectValue(control); config = ApplicationUtil.getConfigObject(config); /** @type {?} */ const parentObject = (control.parent) ? control.parent.value : undefined; if (Linq.IsPassed(formGroupValue, config.conditionalExpression, parentObject)) { if (RegexValidator.isNotBlank(controlValue)) { if (!(parseFloat(controlValue) >= config.value)) return ObjectMaker.toJson(AnnotationTypes["minNumber"], config.message || null, [controlValue]); } } return ObjectMaker.null(); }; } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ /** * @param {?} config * @return {?} */ function passwordValidator(config) { return (control) => { /** @type {?} */ let controlValue = control.value; /** @type {?} */ const formGroupValue = ApplicationUtil.getParentObjectValue(control); if (RegexValidator.isNotBlank(controlValue)) { /** @type {?} */ let validation = RegexValidator.isValidPassword(config.validation, controlValue); if (!validation["isValid"]) return ObjectMaker.toJson(AnnotationTypes["password"], config.message || null, [controlValue]); } return ObjectMaker.null(); }; } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ /** * @param {?} config * @return {?} */ function rangeValidator(config) { return (control) => { /** @type {?} */ let controlValue = control.value; /** @type {?} */ const formGroupValue = ApplicationUtil.getParentObjectValue(control); config = ApplicationUtil.getConfigObject(config); /** @type {?} */ const parentObject = (control.parent) ? control.parent.value : undefined; if (Linq.IsPassed(formGroupValue, config.conditionalExpression, parentObject)) { if (RegexValidator.isNotBlank(controlValue)) { if (!(String(controlValue).indexOf(".") == -1 && parseInt(controlValue) >= config.minimumNumber && parseInt(controlValue) <= config.maximumNumber)) return ObjectMaker.toJson(AnnotationTypes["range"], config.message || null, [config.minimumNumber, config.maximumNumber, controlValue]); } } return ObjectMaker.null(); }; } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ /** * @param {?} config * @return {?} */ function uppercaseValidator(config) { return (control) => { /** @type {?} */ const controlValue = control.value; /** @type {?} */ const formGroupValue = ApplicationUtil.getParentObjectValue(control); config = ApplicationUtil.getConfigObject(config); /** @type {?} */ const parentObject = (control.parent) ? control.parent.value : undefined; if (Linq.IsPassed(formGroupValue, config.conditionalExpression, parentObject)) { if (RegexValidator.isNotBlank(controlValue)) { if (!(controlValue === controlValue.toUpperCase())) return ObjectMaker.toJson(AnnotationTypes["upperCase"], config.message || null, [controlValue]); } } return ObjectMaker.null(); }; } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ /** * @param {?} config * @return {?} */ function requiredValidator(config) { return (control) => { /** @type {?} */ const controlValue = control.value; /** @type {?} */ const formGroupValue = ApplicationUtil.getParentObjectValue(control); config = ApplicationUtil.getConfigObject(config); /** @type {?} */ const parentObject = (control.parent) ? control.parent.value : undefined; if (Linq.IsPassed(formGroupValue, config.conditionalExpression, parentObject)) { if (!RegexValidator.isNotBlank(controlValue)) { return ObjectMaker.toJson(AnnotationTypes["required"], config.message || null, [controlValue]); } } return ObjectMaker.null(); }; } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ /** * @param {?} config * @return {?} */ function patternValidator(config) { return (control) => { /** @type {?} */ let controlValue = control.value; /** @type {?} */ const formGroupValue = ApplicationUtil.getParentObjectValue(control); config = ApplicationUtil.getConfigObject(config); /** @type {?} */ const parentObject = (control.parent) ? control.parent.value : undefined; if (Linq.IsPassed(formGroupValue, config.conditionalExpression, parentObject)) { if (RegexValidator.isNotBlank(controlValue)) { for (var pattern in config.pattern) if (!(RegexValidator.isValid(controlValue, config.pattern[pattern]))) return ObjectMaker.toJson(pattern, config.message || null, [controlValue]); } } return ObjectMaker.null(); }; } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ /** * @param {?} config * @param {?} conditionalValidationProps * @return {?} */ function timeValidator(config, conditionalValidationProps) { return (control) => { /** @type {?} */ const controlValue = control.value; /** @type {?} */ const formGroupValue = ApplicationUtil.getParentObjectValue(control); config = ApplicationUtil.getConfigObject(config); /** @type {?} */ const parentObject = (control.parent) ? control.parent.value : undefined; if (Linq.IsPassed(formGroupValue, config.conditionalExpression, parentObject)) { if (RegexValidator.isNotBlank(controlValue)) { /** @type {?} */ var testResult = false; /** @type {?} */ let valueLength = 5; if (config.allowSeconds) valueLength = 8; testResult = RegexValidator.isValid(controlValue, RegExRule["time"]) && controlValue.length == valueLength; if (!testResult) return ObjectMaker.toJson(AnnotationTypes["time"], config.message || null, [controlValue]); } } return ObjectMaker.null(); }; } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ /** * @param {?} config * @param {?} conditionalValidationProps * @return {?} */ function urlValidator(config, conditionalValidationProps) { return (control) => { /** @type {?} */ const controlValue = control.value; /** @type {?} */ const formGroupValue = ApplicationUtil.getParentObjectValue(control); config = ApplicationUtil.getConfigObject(config); /** @type {?} */ const parentObject = (control.parent) ? control.parent.value : undefined; if (Linq.IsPassed(formGroupValue, config.conditionalExpression, parentObject)) { if (RegexValidator.isNotBlank(controlValue)) { if (!RegexValidator.isValid(controlValue, RegExRule["url"])) return ObjectMaker.toJson(AnnotationTypes["url"], config.message || null, [controlValue]); } } return ObjectMaker.null(); }; } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ /** * @param {?} config * @param {?} conditionalValidationProps * @return {?} */ function jsonValidator(config, conditionalValidationProps) { return (control) => { /** @type {?} */ const controlValue = control.value; /** @type {?} */ const formGroupValue = ApplicationUtil.getParentObjectValue(control); config = ApplicationUtil.getConfigObject(config); /** @type {?} */ const parentObject = (control.parent) ? control.parent.value : undefined; if (Linq.IsPassed(formGroupValue, config.conditionalExpression, parentObject)) { if (RegexValidator.isNotBlank(controlValue)) { try { /** @type {?} */ var parseValue = isNumeric(controlValue); if (parseValue || controlValue == "true" || controlValue == "false") { throw "invalid value"; } /** @type {?} */ var json = JSON.parse(controlValue); } catch (ex) { return ObjectMaker.toJson(AnnotationTypes["json"], config.message || null, [controlValue]); } } } return ObjectMaker.null(); }; } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ /** * @param {?} config * @return {?} */ function greaterThanValidator(config) { return (control) => { /** @type {?} */ const matchControl = control.root.get([config.fieldName]); /** @type {?} */ const controlValue = control.value; /** @type {?} */ const matchControlValue = (matchControl) ? matchControl.value : ''; /** @type {?} */ const formGroupValue = ApplicationUtil.getParentObjectValue(control); /** @type {?} */ const parentObject = (control.parent) ? control.parent.value : undefined; if (Linq.IsPassed(formGroupValue, config.conditionalExpression, parentObject)) { if (RegexValidator.isNotBlank(controlValue) && RegexValidator.isNotBlank(matchControlValue)) { if (!(matchControl && parseFloat(controlValue) > parseFloat(matchControlValue))) return ObjectMaker.toJson(AnnotationTypes["greaterThan"], config.message || null, [controlValue, matchControlValue]); } } return ObjectMaker.null(); }; } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ /** * @param {?} config * @return {?} */ function greaterThanEqualToValidator(config) { return (control) => { /** @type {?} */ const matchControl = control.root.get([config.fieldName]); /** @type {?} */ const controlValue = control.value; /** @type {?} */ const matchControlValue = (matchControl) ? matchControl.value : ''; /** @type {?} */ const formGroupValue = ApplicationUtil.getParentObjectValue(control); /** @type {?} */ const parentObject = (control.parent) ? control.parent.value : undefined; if (Linq.IsPassed(formGroupValue, config.conditionalExpression, parentObject)) { if ((RegexValidator.isNotBlank(controlValue) && RegexValidator.isNotBlank(matchControlValue))) { if (!(matchControl && parseFloat(controlValue) >= parseFloat(matchControlValue))) return ObjectMaker.toJson(AnnotationTypes["greaterThanEqualTo"], config.message || null, [controlValue, matchControlValue]); } } return ObjectMaker.null(); }; } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ /** * @param {?} config * @return {?} */ function lessThanEqualToValidator(config) { return (control) => { /** @type {?} */ const matchControl = control.root.get([config.fieldName]); /** @type {?} */ const controlValue = control.value; /** @type {?} */ const matchControlValue = (matchControl) ? matchControl.value : ''; /** @type {?} */ const formGroupValue = ApplicationUtil.getParentObjectValue(control); /** @type {?} */ const parentObject = (control.parent) ? control.parent.value : undefined; if (Linq.IsPassed(formGroupValue, config.conditionalExpression, parentObject)) { if ((RegexValidator.isNotBlank(controlValue) && RegexValidator.isNotBlank(matchControlValue))) { if (!(matchControl && parseFloat(controlValue) <= parseFloat(matchControlValue))) return ObjectMaker.toJson(AnnotationTypes["lessThanEqualTo"], config.message || null, [controlValue, matchControlValue]); } } return ObjectMaker.null(); }; } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ /** * @param {?} config * @return {?} */ function lessThanValidator(config) { return (control) => { /** @type {?} */ const matchControl = control.root.get([config.fieldName]); /** @type {?} */ const controlValue = control.value; /** @type {?} */ const matchControlValue = (matchControl) ? matchControl.value : ''; /** @type {?} */ const formGroupValue = ApplicationUtil.getParentObjectValue(control); /** @type {?} */ const parentObject = (control.parent) ? control.parent.value : undefined; if (Linq.IsPassed(formGroupValue, config.conditionalExpression, parentObject)) { if ((RegexValidator.isNotBlank(controlValue) && RegexValidator.isNotBlank(matchControlValue))) { if (!(matchControl && parseFloat(controlValue) < parseFloat(matchControlValue))) return ObjectMaker.toJson(AnnotationTypes["lessThan"], config.message || null, [controlValue, matchControlValue]); } } return ObjectMaker.null(); }; } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ /** @type {?} */ const PROPERTY = "property"; /** @type {?} */ const OBJECT_PROPERTY = "objectProperty"; /** @type {?} */ const ARRAY_PROPERTY = "arrayProperty"; /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ /** @type {?} */ const defaultContainer = new (class { constructor() { this.instances = []; } /** * @template T * @param {?} instanceFunc * @return {?} */ get(instanceFunc) { /** @type {?} */ let instance = this.instances.filter(instance => instance.instance === instanceFunc)[0]; return instance; } /** * @param {?} instanceFunc * @return {?} */ addInstanceContainer(instanceFunc) { /** @type {?} */ let instanceContainer = { instance: instanceFunc, propertyAnnotations: [], properties: [] }; this.instances.push(instanceContainer); return instanceContainer; } /** * @param {?} instanceFunc * @param {?} propertyInfo * @return {?} */ addProperty(instanceFunc, propertyInfo) { /** @type {?} */ let instance = this.instances.filter(instance => instance.instance === instanceFunc)[0]; if (instance) { this.addPropertyInfo(instance, propertyInfo); } else { instance = this.addInstanceContainer(instanceFunc); this.addPropertyInfo(instance, propertyInfo); } } /** * @param {?} instance * @param {?} propertyInfo * @return {?} */ addPropertyInfo(instance, propertyInfo) { /** @type {?} */ var property = instance.properties.filter(t => t.name == propertyInfo.name)[0]; if (!property) instance.properties.push(propertyInfo); } /** * @param {?} instanceFunc * @param {?} decoratorConfiguration * @return {?} */ addAnnotation(instanceFunc, decoratorConfiguration) { this.addProperty(instanceFunc, { propertyType: PROPERTY, name: decoratorConfiguration.propertyName }); /** @type {?} */ let instance = this.instances.filter(instance => in