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 maxLength: (value, length) => ValidatorJS.isLength(value, {max: length}),
25 minLength: (value, length) => ValidatorJS.isLength(value, {min: length}),
26 pattern: (value, pattern) => ValidatorJS.matches(value, pattern),
29 // to allow empty value which is not zero
32 return ValidatorJS.isNumeric(value);
34 maximum: (value, maxValue) => value <= maxValue,
35 minimum: (value, minValue) => value >= minValue,
36 maximumExclusive: (value, maxValue) => value < maxValue,
37 minimumExclusive: (value, minValue) => value > minValue,
38 alphanumeric: value => ValidatorJS.isAlphanumeric(value),
39 alphanumericWithSpaces: value => ValidatorJS.isAlphanumeric(value.replace(/ /g, '')),
40 validateName: value => ValidatorJS.isAlphanumeric(value.replace(/\s|\.|\_|\-/g, ''), 'en-US'),
41 validateVendorName: value => ValidatorJS.isAlphanumeric(value.replace(/[\x7F-\xFF]|\s/g, ''), 'en-US'),
42 freeEnglishText: value => ValidatorJS.isAlphanumeric(value.replace(/\s|\.|\_|\-|\,|\(|\)|\?/g, ''), 'en-US'),
43 email: value => ValidatorJS.isEmail(value),
44 ip: value => ValidatorJS.isIP(value),
45 url: value => ValidatorJS.isURL(value)
49 static get globalValidationMessagingFunctions() {
51 required: () => i18n('Field is required'),
52 maxLength: (value, maxLength) => i18n('Field value has exceeded it\'s limit, {maxLength}. current length: {length}', {
56 minLength: (value, minLength) => i18n('Field value should contain at least {minLength} characters.', {minLength}),
57 pattern: (value, pattern) => i18n('Field value should match the pattern: {pattern}.', {pattern}),
58 numeric: () => i18n('Field value should contain numbers only.'),
59 maximum: (value, maxValue) => i18n('Field value should be less or equal to: {maxValue}.', {maxValue}),
60 minimum: (value, minValue) => i18n('Field value should be at least: {minValue}.', {minValue: minValue.toString()}),
61 maximumExclusive: (value, maxValue) => i18n('Field value should be less than: {maxValue}.', {maxValue}),
62 minimumExclusive: (value, minValue) => i18n('Field value should be more than: {minValue}.', {minValue: minValue.toString()}),
63 alphanumeric: () => i18n('Field value should contain letters or digits only.'),
64 alphanumericWithSpaces: () => i18n('Field value should contain letters, digits or spaces only.'),
65 validateName: ()=> i18n('Field value should contain English letters, digits , spaces, underscores, dashes and dots only.'),
66 validateVendorName: ()=> i18n('Field value should contain English letters digits and spaces only.'),
67 freeEnglishText: ()=> i18n('Field value should contain English letters, digits , spaces, underscores, dashes and dots only.'),
68 email: () => i18n('Field value should be a valid email address.'),
69 ip: () => i18n('Field value should be a valid ip address.'),
70 url: () => i18n('Field value should be a valid url address.'),
71 general: () => i18n('Field value is invalid.')
75 static validateItem(value, data, type) {
76 let validationFunc = this.globalValidationFunctions[type];
77 const isValid = validationFunc(value, data);
80 errorText = this.globalValidationMessagingFunctions[type](value, data);
88 static validate(fieldName, value, validations, state, customValidations) {
89 let result = { isValid: true, errorText: '' };
90 for (let validation of validations) {
91 result = this.validateItem(value, validation.data, validation.type);
92 if (!result.isValid) {
96 if (customValidations) {
97 let validationFunc = customValidations[fieldName];
99 result = validationFunc(value, state);
105 static isItemNameAlreadyExistsInList({itemId, itemName, list}) {
106 return list[itemName] && list[itemName] !== itemId;
110 export default Validator;