blob: 3f6a00e2892abc9535cce13f95f2a12c1c9b0c93 [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 {
Avi Ziv61070c92017-07-26 17:37:57 +030023 required: value => {
24 return typeof value === 'string' ? value.replace(/\s+/g, '') !== '' : value !== '';
25 },
Avi Zivb8e2faf2017-07-18 19:45:38 +030026 requiredChooseOption: value => value !== '',
AviZi280f8012017-06-09 02:39:56 +030027 maxLength: (value, length) => ValidatorJS.isLength(value, {max: length}),
28 minLength: (value, length) => ValidatorJS.isLength(value, {min: length}),
29 pattern: (value, pattern) => ValidatorJS.matches(value, pattern),
30 numeric: value => {
31 if (value === '') {
32 // to allow empty value which is not zero
33 return true;
34 }
35 return ValidatorJS.isNumeric(value);
36 },
Avi Zivb8e2faf2017-07-18 19:45:38 +030037 maximum: (value, maxValue) => {return (value === undefined) ? true : (value <= maxValue);},
38 minimum: (value, minValue) => {return (value === undefined) ? true : (value >= minValue);},
39 maximumExclusive: (value, maxValue) => {return (value === undefined) ? true : (value < maxValue);},
40 minimumExclusive: (value, minValue) => {return (value === undefined) ? true : (value > minValue);},
AviZi280f8012017-06-09 02:39:56 +030041 alphanumeric: value => ValidatorJS.isAlphanumeric(value),
42 alphanumericWithSpaces: value => ValidatorJS.isAlphanumeric(value.replace(/ /g, '')),
43 validateName: value => ValidatorJS.isAlphanumeric(value.replace(/\s|\.|\_|\-/g, ''), 'en-US'),
44 validateVendorName: value => ValidatorJS.isAlphanumeric(value.replace(/[\x7F-\xFF]|\s/g, ''), 'en-US'),
45 freeEnglishText: value => ValidatorJS.isAlphanumeric(value.replace(/\s|\.|\_|\-|\,|\(|\)|\?/g, ''), 'en-US'),
46 email: value => ValidatorJS.isEmail(value),
47 ip: value => ValidatorJS.isIP(value),
Avi Zivb8e2faf2017-07-18 19:45:38 +030048 url: value => ValidatorJS.isURL(value),
ilanap2c9358a2017-09-13 14:41:21 +030049 alphanumericWithUnderscores: value => ValidatorJS.isAlphanumeric(value.replace(/_/g, '')),
50 requiredChoiceWithOther: (value, otherValue) => {
51 let chosen = value.choice;
52 // if we have an empty multiple select we have a problem since it's required
53 let validationFunc = this.globalValidationFunctions['required'];
54 if (value.choices) {
55 if (value.choices.length === 0) {
56 return false;
57 } else {
58 // continuing validation with the first chosen value in case we have the 'Other' field
59 chosen = value.choices[0];
60 }
61 }
62 if (chosen !== otherValue) {
63 return validationFunc(chosen, true);
64 } else { // when 'Other' was chosen, validate other value
65 return validationFunc(value.other, true);
66 }
67 }
AviZi280f8012017-06-09 02:39:56 +030068 };
69 }
70
71 static get globalValidationMessagingFunctions() {
72 return {
73 required: () => i18n('Field is required'),
Avi Zivb8e2faf2017-07-18 19:45:38 +030074 requiredChooseOption: () => i18n('Field should have one of these options'),
ilanap2c9358a2017-09-13 14:41:21 +030075 requiredChoiceWithOther: () => i18n('Field is required'),
AviZi280f8012017-06-09 02:39:56 +030076 maxLength: (value, maxLength) => i18n('Field value has exceeded it\'s limit, {maxLength}. current length: {length}', {
77 length: value.length,
78 maxLength
79 }),
Avi Zivb8e2faf2017-07-18 19:45:38 +030080 minLength: (value, minLength) => i18n(`Field value should contain at least ${minLength} characters.`),
81 pattern: (value, pattern) => i18n(`Field value should match the pattern: ${pattern}.`),
AviZi280f8012017-06-09 02:39:56 +030082 numeric: () => i18n('Field value should contain numbers only.'),
Avi Zivb8e2faf2017-07-18 19:45:38 +030083 maximum: (value, maxValue) => i18n(`Field value should be less or equal to: ${maxValue}.`),
84 minimum: (value, minValue) => i18n(`Field value should be at least: ${minValue.toString()}.`),
85 maximumExclusive: (value, maxValue) => i18n(`Field value should be less than: ${maxValue}.`),
86 minimumExclusive: (value, minValue) => i18n(`Field value should be more than: ${minValue.toString()}.`),
AviZi280f8012017-06-09 02:39:56 +030087 alphanumeric: () => i18n('Field value should contain letters or digits only.'),
88 alphanumericWithSpaces: () => i18n('Field value should contain letters, digits or spaces only.'),
89 validateName: ()=> i18n('Field value should contain English letters, digits , spaces, underscores, dashes and dots only.'),
90 validateVendorName: ()=> i18n('Field value should contain English letters digits and spaces only.'),
91 freeEnglishText: ()=> i18n('Field value should contain English letters, digits , spaces, underscores, dashes and dots only.'),
92 email: () => i18n('Field value should be a valid email address.'),
93 ip: () => i18n('Field value should be a valid ip address.'),
94 url: () => i18n('Field value should be a valid url address.'),
Avi Zivb8e2faf2017-07-18 19:45:38 +030095 general: () => i18n('Field value is invalid.'),
96 alphanumericWithUnderscores: () => i18n('Field value should contain letters, digits or _ only.')
AviZi280f8012017-06-09 02:39:56 +030097 };
98 }
99
100 static validateItem(value, data, type) {
101 let validationFunc = this.globalValidationFunctions[type];
102 const isValid = validationFunc(value, data);
103 let errorText = '';
104 if (!isValid) {
105 errorText = this.globalValidationMessagingFunctions[type](value, data);
106 }
107 return {
108 isValid,
109 errorText
110 };
111 }
112
113 static validate(fieldName, value, validations, state, customValidations) {
114 let result = { isValid: true, errorText: '' };
115 for (let validation of validations) {
116 result = this.validateItem(value, validation.data, validation.type);
117 if (!result.isValid) {
118 return result;
119 }
120 }
121 if (customValidations) {
122 let validationFunc = customValidations[fieldName];
123 if (validationFunc) {
124 result = validationFunc(value, state);
125 }
126 }
127 return result;
128 }
129
130 static isItemNameAlreadyExistsInList({itemId, itemName, list}) {
Avi Ziv61070c92017-07-26 17:37:57 +0300131 itemName = itemName.toLowerCase();
AviZi280f8012017-06-09 02:39:56 +0300132 return list[itemName] && list[itemName] !== itemId;
133 }
134}
135
136export default Validator;