708179e9fbc80a3b500007a347cce1ba6a13a48a
[sdc.git] / openecomp-ui / src / nfvo-utils / Validator.js
1 /*!
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
17 import * as ValidatorJS from 'validator';
18 import i18n from 'nfvo-utils/i18n/i18n.js';
19
20 class Validator {
21         static get globalValidationFunctions() {
22                 return {
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),
27                         numeric: value => {
28                                 if (value === '') {
29                                         // to allow empty value which is not zero
30                                         return true;
31                                 }
32                                 return ValidatorJS.isNumeric(value);
33                         },
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)
46                 };
47         }
48
49         static get globalValidationMessagingFunctions() {
50                 return {
51                         required: () => i18n('Field is required'),
52                         maxLength: (value, maxLength) => i18n('Field value has exceeded it\'s limit, {maxLength}. current length: {length}', {
53                                 length: value.length,
54                                 maxLength
55                         }),
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.')
72                 };
73         }
74
75         static validateItem(value, data, type) {
76                 let validationFunc = this.globalValidationFunctions[type];
77                 const isValid = validationFunc(value, data);
78                 let errorText = '';
79                 if (!isValid) {
80                         errorText = this.globalValidationMessagingFunctions[type](value, data);
81                 }
82                 return {
83                         isValid,
84                         errorText
85                 };
86         }
87
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) {
93                                 return result;
94                         }
95                 }
96                 if (customValidations) {
97                         let validationFunc = customValidations[fieldName];
98                         if (validationFunc) {
99                                 result = validationFunc(value, state);
100                         }
101                 }
102                 return result;
103         }
104
105         static isItemNameAlreadyExistsInList({itemId, itemName, list}) {
106                 return list[itemName] && list[itemName] !== itemId;
107         }
108 }
109
110 export default Validator;