2 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13 * or implied. See the License for the specific language governing
14 * permissions and limitations under the License.
17 import * as ValidatorJS from 'validator';
18 import i18n from 'nfvo-utils/i18n/i18n.js';
21 static get globalValidationFunctions() {
23 required: value => value !== '',
24 requiredChooseOption: value => value !== '',
25 maxLength: (value, length) => ValidatorJS.isLength(value, {max: length}),
26 minLength: (value, length) => ValidatorJS.isLength(value, {min: length}),
27 pattern: (value, pattern) => ValidatorJS.matches(value, pattern),
30 // to allow empty value which is not zero
33 return ValidatorJS.isNumeric(value);
35 maximum: (value, maxValue) => {return (value === undefined) ? true : (value <= maxValue);},
36 minimum: (value, minValue) => {return (value === undefined) ? true : (value >= minValue);},
37 maximumExclusive: (value, maxValue) => {return (value === undefined) ? true : (value < maxValue);},
38 minimumExclusive: (value, minValue) => {return (value === undefined) ? true : (value > minValue);},
39 alphanumeric: value => ValidatorJS.isAlphanumeric(value),
40 alphanumericWithSpaces: value => ValidatorJS.isAlphanumeric(value.replace(/ /g, '')),
41 validateName: value => ValidatorJS.isAlphanumeric(value.replace(/\s|\.|\_|\-/g, ''), 'en-US'),
42 validateVendorName: value => ValidatorJS.isAlphanumeric(value.replace(/[\x7F-\xFF]|\s/g, ''), 'en-US'),
43 freeEnglishText: value => ValidatorJS.isAlphanumeric(value.replace(/\s|\.|\_|\-|\,|\(|\)|\?/g, ''), 'en-US'),
44 email: value => ValidatorJS.isEmail(value),
45 ip: value => ValidatorJS.isIP(value),
46 url: value => ValidatorJS.isURL(value),
47 alphanumericWithUnderscores: value => ValidatorJS.isAlphanumeric(value.replace(/_/g, ''))
51 static get globalValidationMessagingFunctions() {
53 required: () => i18n('Field is required'),
54 requiredChooseOption: () => i18n('Field should have one of these options'),
55 maxLength: (value, maxLength) => i18n('Field value has exceeded it\'s limit, {maxLength}. current length: {length}', {
59 minLength: (value, minLength) => i18n(`Field value should contain at least ${minLength} characters.`),
60 pattern: (value, pattern) => i18n(`Field value should match the pattern: ${pattern}.`),
61 numeric: () => i18n('Field value should contain numbers only.'),
62 maximum: (value, maxValue) => i18n(`Field value should be less or equal to: ${maxValue}.`),
63 minimum: (value, minValue) => i18n(`Field value should be at least: ${minValue.toString()}.`),
64 maximumExclusive: (value, maxValue) => i18n(`Field value should be less than: ${maxValue}.`),
65 minimumExclusive: (value, minValue) => i18n(`Field value should be more than: ${minValue.toString()}.`),
66 alphanumeric: () => i18n('Field value should contain letters or digits only.'),
67 alphanumericWithSpaces: () => i18n('Field value should contain letters, digits or spaces only.'),
68 validateName: ()=> i18n('Field value should contain English letters, digits , spaces, underscores, dashes and dots only.'),
69 validateVendorName: ()=> i18n('Field value should contain English letters digits and spaces only.'),
70 freeEnglishText: ()=> i18n('Field value should contain English letters, digits , spaces, underscores, dashes and dots only.'),
71 email: () => i18n('Field value should be a valid email address.'),
72 ip: () => i18n('Field value should be a valid ip address.'),
73 url: () => i18n('Field value should be a valid url address.'),
74 general: () => i18n('Field value is invalid.'),
75 alphanumericWithUnderscores: () => i18n('Field value should contain letters, digits or _ only.')
79 static validateItem(value, data, type) {
80 let validationFunc = this.globalValidationFunctions[type];
81 const isValid = validationFunc(value, data);
84 errorText = this.globalValidationMessagingFunctions[type](value, data);
92 static validate(fieldName, value, validations, state, customValidations) {
93 let result = { isValid: true, errorText: '' };
94 for (let validation of validations) {
95 result = this.validateItem(value, validation.data, validation.type);
96 if (!result.isValid) {
100 if (customValidations) {
101 let validationFunc = customValidations[fieldName];
102 if (validationFunc) {
103 result = validationFunc(value, state);
109 static isItemNameAlreadyExistsInList({itemId, itemName, list}) {
110 return list[itemName] && list[itemName] !== itemId;
114 export default Validator;