blob: 324198ef2e682b783a0377405c06fdbf6e7e6fea [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 {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020021 static get globalValidationFunctions() {
22 return {
23 required: value => {
24 return typeof value === 'string'
25 ? value.replace(/\s+/g, '') !== ''
26 : value !== '';
27 },
28 requiredChooseOption: value => value !== '',
29 maxLength: (value, length) =>
30 ValidatorJS.isLength(value, { max: length }),
31 minLength: (value, length) =>
32 ValidatorJS.isLength(value, { min: length }),
33 pattern: (value, pattern) => ValidatorJS.matches(value, pattern),
34 numeric: value => {
35 if (value === '') {
36 // to allow empty value which is not zero
37 return true;
38 }
39 return ValidatorJS.isNumeric(value);
40 },
41 maximum: (value, maxValue) => {
42 return value === undefined ? true : value <= maxValue;
43 },
44 minimum: (value, minValue) => {
45 return value === undefined ? true : value >= minValue;
46 },
47 maximumExclusive: (value, maxValue) => {
48 return value === undefined ? true : value < maxValue;
49 },
50 minimumExclusive: (value, minValue) => {
51 return value === undefined ? true : value > minValue;
52 },
53 alphanumeric: value => ValidatorJS.isAlphanumeric(value),
54 alphanumericWithSpaces: value =>
55 ValidatorJS.isAlphanumeric(value.replace(/ /g, '')),
56 validateName: value =>
57 ValidatorJS.isAlphanumeric(
58 value.replace(/\s|\.|\_|\-/g, ''),
59 'en-US'
60 ),
61 validateVendorName: value =>
62 ValidatorJS.isAlphanumeric(
63 value.replace(/[\x7F-\xFF]|\s/g, ''),
64 'en-US'
65 ),
66 freeEnglishText: value =>
67 ValidatorJS.isAlphanumeric(
68 value.replace(/\s|\.|\_|\-|\,|\(|\)|\?/g, ''),
69 'en-US'
70 ),
71 email: value => ValidatorJS.isEmail(value),
72 ip: value => ValidatorJS.isIP(value),
73 url: value => ValidatorJS.isURL(value),
74 alphanumericWithUnderscores: value =>
75 ValidatorJS.isAlphanumeric(value.replace(/_/g, '')),
76 requiredChoiceWithOther: (value, otherValue) => {
77 let chosen = value.choice;
78 // if we have an empty multiple select we have a problem since it's required
79 let validationFunc = this.globalValidationFunctions['required'];
80 if (value.choices) {
81 if (value.choices.length === 0) {
82 return false;
83 } else {
84 // continuing validation with the first chosen value in case we have the 'Other' field
85 chosen = value.choices[0];
86 }
87 }
88 if (chosen !== otherValue) {
89 return validationFunc(chosen, true);
90 } else {
91 // when 'Other' was chosen, validate other value
92 return validationFunc(value.other, true);
93 }
94 }
95 };
96 }
AviZi280f8012017-06-09 02:39:56 +030097
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020098 static get globalValidationMessagingFunctions() {
99 return {
100 required: () => i18n('Field is required'),
101 requiredChooseOption: () =>
102 i18n('Field should have one of these options'),
103 requiredChoiceWithOther: () => i18n('Field is required'),
104 maxLength: (value, maxLength) =>
105 i18n(
106 "Field value has exceeded it's limit, {maxLength}. current length: {length}",
107 {
108 length: value.length,
109 maxLength
110 }
111 ),
112 minLength: (value, minLength) =>
113 i18n(
114 'Field value should contain at least {minLength} characters.',
115 {
116 minLength: minLength
117 }
118 ),
119 pattern: (value, pattern) =>
120 i18n('Field value should match the pattern: {pattern}.', {
121 pattern: pattern
122 }),
123 numeric: () => i18n('Field value should contain numbers only.'),
124 maximum: (value, maxValue) =>
125 i18n('Field value should be less or equal to: {maxValue}.', {
126 maxValue: maxValue
127 }),
128 minimum: (value, minValue) =>
129 i18n('Field value should be at least: {minValue}.', {
130 minValue: minValue.toString()
131 }),
132 maximumExclusive: (value, maxValue) =>
133 i18n('Field value should be less than: {maxValue}.', {
134 maxValue: maxValue
135 }),
136 minimumExclusive: (value, minValue) =>
137 i18n('Field value should be more than: {minValue}.', {
138 minValue: minValue.toString()
139 }),
140 alphanumeric: () =>
141 i18n('Field value should contain letters or digits only.'),
142 alphanumericWithSpaces: () =>
143 i18n(
144 'Field value should contain letters, digits or spaces only.'
145 ),
146 validateName: () =>
147 i18n(
148 'Field value should contain English letters, digits , spaces, underscores, dashes and dots only.'
149 ),
150 validateVendorName: () =>
151 i18n(
152 'Field value should contain English letters digits and spaces only.'
153 ),
154 freeEnglishText: () =>
155 i18n(
156 'Field value should contain English letters, digits , spaces, underscores, dashes and dots only.'
157 ),
158 email: () => i18n('Field value should be a valid email address.'),
159 ip: () => i18n('Field value should be a valid ip address.'),
160 url: () => i18n('Field value should be a valid url address.'),
161 general: () => i18n('Field value is invalid.'),
162 alphanumericWithUnderscores: () =>
163 i18n('Field value should contain letters, digits or _ only.')
164 };
165 }
AviZi280f8012017-06-09 02:39:56 +0300166
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200167 static validateItem(value, data, type) {
168 let validationFunc = this.globalValidationFunctions[type];
169 const isValid = validationFunc(value, data);
170 let errorText = '';
171 if (!isValid) {
172 errorText = this.globalValidationMessagingFunctions[type](
173 value,
174 data
175 );
176 }
177 return {
178 isValid,
179 errorText
180 };
181 }
AviZi280f8012017-06-09 02:39:56 +0300182
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200183 static validate(fieldName, value, validations, state, customValidations) {
184 let result = { isValid: true, errorText: '' };
185 for (let validation of validations) {
186 result = this.validateItem(value, validation.data, validation.type);
187 if (!result.isValid) {
188 return result;
189 }
190 }
191 if (customValidations) {
192 let validationFunc = customValidations[fieldName];
193 if (validationFunc) {
194 result = validationFunc(value, state);
195 }
196 }
197 return result;
198 }
AviZi280f8012017-06-09 02:39:56 +0300199
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200200 static isItemNameAlreadyExistsInList({ itemId, itemName, list }) {
201 itemName = itemName.toLowerCase();
202 return list[itemName] && list[itemName] !== itemId;
203 }
AviZi280f8012017-06-09 02:39:56 +0300204}
205
206export default Validator;