blob: 1df82a2ada78dee59eff9bae90a05586a9bf00a9 [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),
49 alphanumericWithUnderscores: value => ValidatorJS.isAlphanumeric(value.replace(/_/g, ''))
AviZi280f8012017-06-09 02:39:56 +030050 };
51 }
52
53 static get globalValidationMessagingFunctions() {
54 return {
55 required: () => i18n('Field is required'),
Avi Zivb8e2faf2017-07-18 19:45:38 +030056 requiredChooseOption: () => i18n('Field should have one of these options'),
AviZi280f8012017-06-09 02:39:56 +030057 maxLength: (value, maxLength) => i18n('Field value has exceeded it\'s limit, {maxLength}. current length: {length}', {
58 length: value.length,
59 maxLength
60 }),
Avi Zivb8e2faf2017-07-18 19:45:38 +030061 minLength: (value, minLength) => i18n(`Field value should contain at least ${minLength} characters.`),
62 pattern: (value, pattern) => i18n(`Field value should match the pattern: ${pattern}.`),
AviZi280f8012017-06-09 02:39:56 +030063 numeric: () => i18n('Field value should contain numbers only.'),
Avi Zivb8e2faf2017-07-18 19:45:38 +030064 maximum: (value, maxValue) => i18n(`Field value should be less or equal to: ${maxValue}.`),
65 minimum: (value, minValue) => i18n(`Field value should be at least: ${minValue.toString()}.`),
66 maximumExclusive: (value, maxValue) => i18n(`Field value should be less than: ${maxValue}.`),
67 minimumExclusive: (value, minValue) => i18n(`Field value should be more than: ${minValue.toString()}.`),
AviZi280f8012017-06-09 02:39:56 +030068 alphanumeric: () => i18n('Field value should contain letters or digits only.'),
69 alphanumericWithSpaces: () => i18n('Field value should contain letters, digits or spaces only.'),
70 validateName: ()=> i18n('Field value should contain English letters, digits , spaces, underscores, dashes and dots only.'),
71 validateVendorName: ()=> i18n('Field value should contain English letters digits and spaces only.'),
72 freeEnglishText: ()=> i18n('Field value should contain English letters, digits , spaces, underscores, dashes and dots only.'),
73 email: () => i18n('Field value should be a valid email address.'),
74 ip: () => i18n('Field value should be a valid ip address.'),
75 url: () => i18n('Field value should be a valid url address.'),
Avi Zivb8e2faf2017-07-18 19:45:38 +030076 general: () => i18n('Field value is invalid.'),
77 alphanumericWithUnderscores: () => i18n('Field value should contain letters, digits or _ only.')
AviZi280f8012017-06-09 02:39:56 +030078 };
79 }
80
81 static validateItem(value, data, type) {
82 let validationFunc = this.globalValidationFunctions[type];
83 const isValid = validationFunc(value, data);
84 let errorText = '';
85 if (!isValid) {
86 errorText = this.globalValidationMessagingFunctions[type](value, data);
87 }
88 return {
89 isValid,
90 errorText
91 };
92 }
93
94 static validate(fieldName, value, validations, state, customValidations) {
95 let result = { isValid: true, errorText: '' };
96 for (let validation of validations) {
97 result = this.validateItem(value, validation.data, validation.type);
98 if (!result.isValid) {
99 return result;
100 }
101 }
102 if (customValidations) {
103 let validationFunc = customValidations[fieldName];
104 if (validationFunc) {
105 result = validationFunc(value, state);
106 }
107 }
108 return result;
109 }
110
111 static isItemNameAlreadyExistsInList({itemId, itemName, list}) {
Avi Ziv61070c92017-07-26 17:37:57 +0300112 itemName = itemName.toLowerCase();
AviZi280f8012017-06-09 02:39:56 +0300113 return list[itemName] && list[itemName] !== itemId;
114 }
115}
116
117export default Validator;