Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
add() {
// format datetime
// tslint:disable-next-line:max-line-length
const iso8601Datetime = /^\d\d\d\d-[0-1]\d-[0-3]\d[t\s](?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)(?:\.\d+)?(?:z|[+-]\d\d:\d\d)$/i;
ValidationRules
.customRule('format_datetime', (val) => !!val ? iso8601Datetime.test(val) : true, this.configuration.messages.format_datetime || '${$displayName} is not a valid date/time.');
// format date
const iso8601Date = /^\d\d\d\d-[0-1]\d-[0-3]\d$/;
ValidationRules
.customRule('format_date', (val) => !!val ? iso8601Date.test(val) : true, this.configuration.messages.format_date || '${$displayName} is not a valid date.');
// format time
const iso8601Time = /^(?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)(?:\.\d+)?(?:z|[+-]\d\d:\d\d)?$/i;
ValidationRules
.customRule('format_time', (val) => !!val ? iso8601Time.test(val) : true, this.configuration.messages.format_time || '${$displayName} is not a valid time.');
// format ipv4
const ipv4 = /^(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)$/;
ValidationRules
.customRule('format_ipv4', (val) => !!val ? ipv4.test(val) : true, this.configuration.messages.format_ipv4 || '${$displayName} is not a valid IPv4 address.');
// format ipv6
// tslint:disable-next-line:max-line-length
const ipv6 = /^\s*(?:(?:(?:[0-9a-f]{1,4}:){7}(?:[0-9a-f]{1,4}|:))|(?:(?:[0-9a-f]{1,4}:){6}(?::[0-9a-f]{1,4}|(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(?:(?:[0-9a-f]{1,4}:){5}(?:(?:(?::[0-9a-f]{1,4}){1,2})|:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(?:(?:[0-9a-f]{1,4}:){4}(?:(?:(?::[0-9a-f]{1,4}){1,3})|(?:(?::[0-9a-f]{1,4})?:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(?:(?:[0-9a-f]{1,4}:){3}(?:(?:(?::[0-9a-f]{1,4}){1,4})|(?:(?::[0-9a-f]{1,4}){0,2}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(?:(?:[0-9a-f]{1,4}:){2}(?:(?:(?::[0-9a-f]{1,4}){1,5})|(?:(?::[0-9a-f]{1,4}){0,3}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(?:(?:[0-9a-f]{1,4}:){1}(?:(?:(?::[0-9a-f]{1,4}){1,6})|(?:(?::[0-9a-f]{1,4}){0,4}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(?::(?:(?:(?::[0-9a-f]{1,4}){1,7})|(?:(?::[0-9a-f]{1,4}){0,5}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(?:%.+)?\s*$/i;
.customRule('format_ipv4', (val) => !!val ? ipv4.test(val) : true, this.configuration.messages.format_ipv4 || '${$displayName} is not a valid IPv4 address.');
// format ipv6
// tslint:disable-next-line:max-line-length
const ipv6 = /^\s*(?:(?:(?:[0-9a-f]{1,4}:){7}(?:[0-9a-f]{1,4}|:))|(?:(?:[0-9a-f]{1,4}:){6}(?::[0-9a-f]{1,4}|(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(?:(?:[0-9a-f]{1,4}:){5}(?:(?:(?::[0-9a-f]{1,4}){1,2})|:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(?:(?:[0-9a-f]{1,4}:){4}(?:(?:(?::[0-9a-f]{1,4}){1,3})|(?:(?::[0-9a-f]{1,4})?:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(?:(?:[0-9a-f]{1,4}:){3}(?:(?:(?::[0-9a-f]{1,4}){1,4})|(?:(?::[0-9a-f]{1,4}){0,2}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(?:(?:[0-9a-f]{1,4}:){2}(?:(?:(?::[0-9a-f]{1,4}){1,5})|(?:(?::[0-9a-f]{1,4}){0,3}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(?:(?:[0-9a-f]{1,4}:){1}(?:(?:(?::[0-9a-f]{1,4}){1,6})|(?:(?::[0-9a-f]{1,4}){0,4}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(?::(?:(?:(?::[0-9a-f]{1,4}){1,7})|(?:(?::[0-9a-f]{1,4}){0,5}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(?:%.+)?\s*$/i;
ValidationRules
.customRule('format_ipv6', (val) => !!val ? ipv6.test(val) : true, this.configuration.messages.format_ipv6 || '${$displayName} is not a valid IPv6 address.');
// format hostname
const hostname = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[-0-9a-z]{0,61}[0-9a-z])?)*$/i;
ValidationRules
.customRule('format_hostname', (val) => !!val ? hostname.test(val) : true, this.configuration.messages.format_hostname || '${$displayName} is not a valid hostname.');
// format uri
const uri = /^(?:[a-z][a-z0-9+-.]*:)(?:\/?\/)?[^\s]*$/i;
ValidationRules
.customRule('format_uri', (val) => !!val ? uri.test(val) : true, this.configuration.messages.format_uri || '${$displayName} is not a valid URI.');
// pattern
ValidationRules
.customRule('pattern', (val, _obj, pattern) => !!val ? (new RegExp(pattern)).test(val) : true, this.configuration.messages.pattern || '${$displayName} is not correctly formatted.', (pattern) => ({ pattern }));
}
bind(ctrl, rule) {
this.configuration.messages.format_time || '${$displayName} is not a valid time.'
);
// format ipv4
const ipv4 = /^(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)$/;
ValidationRules
.customRule(
'format_ipv4',
(val: string) => !!val ? ipv4.test(val) : true,
this.configuration.messages.format_ipv4 || '${$displayName} is not a valid IPv4 address.'
);
// format ipv6
// tslint:disable-next-line:max-line-length
const ipv6 = /^\s*(?:(?:(?:[0-9a-f]{1,4}:){7}(?:[0-9a-f]{1,4}|:))|(?:(?:[0-9a-f]{1,4}:){6}(?::[0-9a-f]{1,4}|(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(?:(?:[0-9a-f]{1,4}:){5}(?:(?:(?::[0-9a-f]{1,4}){1,2})|:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(?:(?:[0-9a-f]{1,4}:){4}(?:(?:(?::[0-9a-f]{1,4}){1,3})|(?:(?::[0-9a-f]{1,4})?:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(?:(?:[0-9a-f]{1,4}:){3}(?:(?:(?::[0-9a-f]{1,4}){1,4})|(?:(?::[0-9a-f]{1,4}){0,2}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(?:(?:[0-9a-f]{1,4}:){2}(?:(?:(?::[0-9a-f]{1,4}){1,5})|(?:(?::[0-9a-f]{1,4}){0,3}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(?:(?:[0-9a-f]{1,4}:){1}(?:(?:(?::[0-9a-f]{1,4}){1,6})|(?:(?::[0-9a-f]{1,4}){0,4}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(?::(?:(?:(?::[0-9a-f]{1,4}){1,7})|(?:(?::[0-9a-f]{1,4}){0,5}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(?:%.+)?\s*$/i;
ValidationRules
.customRule(
'format_ipv6',
(val: string) => !!val ? ipv6.test(val) : true,
this.configuration.messages.format_ipv6 || '${$displayName} is not a valid IPv6 address.'
);
// format hostname
const hostname = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[-0-9a-z]{0,61}[0-9a-z])?)*$/i;
ValidationRules
.customRule(
'format_hostname',
(val: string) => !!val ? hostname.test(val) : true,
this.configuration.messages.format_hostname || '${$displayName} is not a valid hostname.'
);
// format uri
// format ipv4
const ipv4 = /^(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)$/;
ValidationRules
.customRule('format_ipv4', (val) => !!val ? ipv4.test(val) : true, this.configuration.messages.format_ipv4 || '${$displayName} is not a valid IPv4 address.');
// format ipv6
// tslint:disable-next-line:max-line-length
const ipv6 = /^\s*(?:(?:(?:[0-9a-f]{1,4}:){7}(?:[0-9a-f]{1,4}|:))|(?:(?:[0-9a-f]{1,4}:){6}(?::[0-9a-f]{1,4}|(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(?:(?:[0-9a-f]{1,4}:){5}(?:(?:(?::[0-9a-f]{1,4}){1,2})|:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(?:(?:[0-9a-f]{1,4}:){4}(?:(?:(?::[0-9a-f]{1,4}){1,3})|(?:(?::[0-9a-f]{1,4})?:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(?:(?:[0-9a-f]{1,4}:){3}(?:(?:(?::[0-9a-f]{1,4}){1,4})|(?:(?::[0-9a-f]{1,4}){0,2}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(?:(?:[0-9a-f]{1,4}:){2}(?:(?:(?::[0-9a-f]{1,4}){1,5})|(?:(?::[0-9a-f]{1,4}){0,3}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(?:(?:[0-9a-f]{1,4}:){1}(?:(?:(?::[0-9a-f]{1,4}){1,6})|(?:(?::[0-9a-f]{1,4}){0,4}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(?::(?:(?:(?::[0-9a-f]{1,4}){1,7})|(?:(?::[0-9a-f]{1,4}){0,5}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(?:%.+)?\s*$/i;
ValidationRules
.customRule('format_ipv6', (val) => !!val ? ipv6.test(val) : true, this.configuration.messages.format_ipv6 || '${$displayName} is not a valid IPv6 address.');
// format hostname
const hostname = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[-0-9a-z]{0,61}[0-9a-z])?)*$/i;
ValidationRules
.customRule('format_hostname', (val) => !!val ? hostname.test(val) : true, this.configuration.messages.format_hostname || '${$displayName} is not a valid hostname.');
// format uri
const uri = /^(?:[a-z][a-z0-9+-.]*:)(?:\/?\/)?[^\s]*$/i;
ValidationRules
.customRule('format_uri', (val) => !!val ? uri.test(val) : true, this.configuration.messages.format_uri || '${$displayName} is not a valid URI.');
// pattern
ValidationRules
.customRule('pattern', (val, _obj, pattern) => !!val ? (new RegExp(pattern)).test(val) : true, this.configuration.messages.pattern || '${$displayName} is not correctly formatted.', (pattern) => ({ pattern }));
}
bind(ctrl, rule) {
(val: string) => !!val ? ipv6.test(val) : true,
this.configuration.messages.format_ipv6 || '${$displayName} is not a valid IPv6 address.'
);
// format hostname
const hostname = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[-0-9a-z]{0,61}[0-9a-z])?)*$/i;
ValidationRules
.customRule(
'format_hostname',
(val: string) => !!val ? hostname.test(val) : true,
this.configuration.messages.format_hostname || '${$displayName} is not a valid hostname.'
);
// format uri
const uri = /^(?:[a-z][a-z0-9+-.]*:)(?:\/?\/)?[^\s]*$/i;
ValidationRules
.customRule(
'format_uri',
(val: string) => !!val ? uri.test(val) : true,
this.configuration.messages.format_uri || '${$displayName} is not a valid URI.'
);
// pattern
ValidationRules
.customRule(
'pattern',
(val, _obj, pattern) => !!val ? (new RegExp(pattern)).test(val) : true,
this.configuration.messages.pattern || '${$displayName} is not correctly formatted.',
(pattern) => ({ pattern })
);
}
add() {
// minimum
ValidationRules
.customRule('minimum', (val, _obj, min) => val !== undefined ? val >= min : true, this.configuration.messages.minimum || '${$displayName} must be greater than or equal to ${$config.min}.', (min) => ({ min }));
// exclusionMinimum
ValidationRules
.customRule('exclusiveMinimum', (val, _obj, min) => val !== undefined ? val > min : true, this.configuration.messages.exclusiveMinimum || '${$displayName} must be greater than ${$config.min}.', (min) => ({ min }));
// maximum
ValidationRules
.customRule('maximum', (val, _obj, max) => val !== undefined ? val <= max : true, this.configuration.messages.maximum || '${$displayName} must be less than or equal to ${$config.max}.', (max) => ({ max }));
// exclusiveMaximum
ValidationRules
.customRule('exclusiveMaximum', (val, _obj, max) => val !== undefined ? val < max : true, this.configuration.messages.exclusiveMaximum || '${$displayName} must be less than ${$config.max}.', (max) => ({ max }));
// multipleOf
ValidationRules
.customRule('multipleOf', (val, _obj, multiple) => val !== undefined ? ((val % multiple) / 100) === 0 : true, this.configuration.messages.multipleOf || '${$displayName} must be a multiple of ${$config.multiple}.', (multiple) => ({ multiple }));
}
bind(ctrl, rule) {
add() {
// minimum
ValidationRules
.customRule(
'minimum',
(val, _obj, min) => val !== undefined ? val >= min : true,
this.configuration.messages.minimum || '${$displayName} must be greater than or equal to ${$config.min}.',
(min) => ({ min })
);
// exclusionMinimum
ValidationRules
.customRule(
'exclusiveMinimum',
(val, _obj, min) => val !== undefined ? val > min : true,
this.configuration.messages.exclusiveMinimum || '${$displayName} must be greater than ${$config.min}.',
(min) => ({ min })
);
return Configure;
},
languages: (l) => {
UIConstants.Languages = l;
return Configure;
}
};
if (configCallback !== undefined && typeof configCallback === 'function') {
configCallback(Configure);
}
// Validation Rules
ValidationRules
.customRule('phone', (value, obj) => value === null || value === undefined || value === '' || PhoneLib.isValid(value), '\${$displayName } is not a valid phone number.');
ValidationRules
.customRule('integer', (value, obj, min, max) => value === null || value === undefined || value === '' || (Number.isInteger(value) && value >= (isEmpty(min) ? Number.MIN_VALUE : min) && value <= (isEmpty(max) ? Number.MAX_VALUE : max)),
'\${$displayName} must be an integer value between \${$config.min} and \${$config.max}.', (min, max) => ({ min, max }));
ValidationRules
.customRule('decimal', (value, obj, min, max) => value === null || value === undefined || value === '' || (isNumber(value) && Math.floor(value % 1) === 0 && value >= (isEmpty(min) ? Number.MIN_VALUE : min) && value <= (isEmpty(max) ? Number.MAX_VALUE : max)),
'\${$displayName} must be a decimal value between \${$config.min} and \${$config.max}.', (min, max) => ({ min, max }));
ValidationRules
.customRule('language', (map, obj, controller, langInput) => {
if (!(langInput && langInput.addError && langInput.removeError)) throw new Error('Language validation must have reference to ui-language');
let promises = [];
_.forEach(map, (model, key) => {
promises.push(controller.validator.validateObject(model)
.then(e => {
if (langInput.errors.indexOf(key) > -1) langInput.removeError(key);
if (e.length > 0) langInput.addError(key);
return e.length > 0 ? true : false;
}));
add() {
// minimum
ValidationRules
.customRule(
'minimum',
(val, _obj, min) => val !== undefined ? val >= min : true,
this.configuration.messages.minimum || '${$displayName} must be greater than or equal to ${$config.min}.',
(min) => ({ min })
);
// exclusionMinimum
ValidationRules
.customRule(
'exclusiveMinimum',
(val, _obj, min) => val !== undefined ? val > min : true,
this.configuration.messages.exclusiveMinimum || '${$displayName} must be greater than ${$config.min}.',
(min) => ({ min })
);
// maximum
ValidationRules
.customRule(
'maximum',
(val, _obj, max) => val !== undefined ? val <= max : true,
this.configuration.messages.maximum || '${$displayName} must be less than or equal to ${$config.max}.',
(max) => ({ max })
);
var registerValidators = function (container) {
container.get(ValidationController).validateTrigger = validateTrigger.changeOrBlur;
ValidationRules.customRule("url", function (value) {
return value === null ||
value === undefined ||
value === "" ||
/^((http[s]?|ftp):\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-.]+[^#?\s]+)(.*)?(#[\w\-]+)?$/.test(value);
}, "\${$displayName} is not a valid url.");
ValidationRules.customRule("phone", function (value) {
return value === null || value === undefined || value === "" || parsePhoneNumberFromString(value).isValid();
}, "\${$displayName} is not a valid phone number.");
ValidationRules.customRule("number", function (value, obj, min, max) {
return value === null ||
value === undefined ||
value === "" ||
(isNumber(value) &&
value >= (isEmpty(min) ? Number.MIN_VALUE : min) &&
value <= (isEmpty(max) ? Number.MAX_VALUE : max));
}, "\${$displayName} must be an number value between \${$config.min} and \${$config.max}.", function (min, max) { return ({ min: min, max: max }); });