blob: 8fcf24a714fd468f5319b7a1291ecee573e1b2b8 [file] [log] [blame]
AviZi280f8012017-06-09 02:39:56 +03001/*!
2 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17import * as ValidatorJS from 'validator';
18import i18n from 'nfvo-utils/i18n/i18n.js';
19
20class Validator {
21 static get globalValidationFunctions() {
22 return {
23 required: value => value !== '',
Avi Zivb8e2faf2017-07-18 19:45:38 +030024 requiredChooseOption: value => value !== '',
AviZi280f8012017-06-09 02:39:56 +030025 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),
28 numeric: value => {
29 if (value === '') {
30 // to allow empty value which is not zero
31 return true;
32 }
33 return ValidatorJS.isNumeric(value);
34 },
Avi Zivb8e2faf2017-07-18 19:45:38 +030035 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);},
AviZi280f8012017-06-09 02:39:56 +030039 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),
Avi Zivb8e2faf2017-07-18 19:45:38 +030046 url: value => ValidatorJS.isURL(value),
47 alphanumericWithUnderscores: value => ValidatorJS.isAlphanumeric(value.replace(/_/g, ''))
AviZi280f8012017-06-09 02:39:56 +030048 };
49 }
50
51 static get globalValidationMessagingFunctions() {
52 return {
53 required: () => i18n('Field is required'),
Avi Zivb8e2faf2017-07-18 19:45:38 +030054 requiredChooseOption: () => i18n('Field should have one of these options'),
AviZi280f8012017-06-09 02:39:56 +030055 maxLength: (value, maxLength) => i18n('Field value has exceeded it\'s limit, {maxLength}. current length: {length}', {
56 length: value.length,
57 maxLength
58 }),
Avi Zivb8e2faf2017-07-18 19:45:38 +030059 minLength: (value, minLength) => i18n(`Field value should contain at least ${minLength} characters.`),
60 pattern: (value, pattern) => i18n(`Field value should match the pattern: ${pattern}.`),
AviZi280f8012017-06-09 02:39:56 +030061 numeric: () => i18n('Field value should contain numbers only.'),
Avi Zivb8e2faf2017-07-18 19:45:38 +030062 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()}.`),
AviZi280f8012017-06-09 02:39:56 +030066 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.'),
Avi Zivb8e2faf2017-07-18 19:45:38 +030074 general: () => i18n('Field value is invalid.'),
75 alphanumericWithUnderscores: () => i18n('Field value should contain letters, digits or _ only.')
AviZi280f8012017-06-09 02:39:56 +030076 };
77 }
78
79 static validateItem(value, data, type) {
80 let validationFunc = this.globalValidationFunctions[type];
81 const isValid = validationFunc(value, data);
82 let errorText = '';
83 if (!isValid) {
84 errorText = this.globalValidationMessagingFunctions[type](value, data);
85 }
86 return {
87 isValid,
88 errorText
89 };
90 }
91
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) {
97 return result;
98 }
99 }
100 if (customValidations) {
101 let validationFunc = customValidations[fieldName];
102 if (validationFunc) {
103 result = validationFunc(value, state);
104 }
105 }
106 return result;
107 }
108
109 static isItemNameAlreadyExistsInList({itemId, itemName, list}) {
110 return list[itemName] && list[itemName] !== itemId;
111 }
112}
113
114export default Validator;