AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 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 { |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 23 | required: value => { |
| 24 | return typeof value === 'string' ? value.replace(/\s+/g, '') !== '' : value !== ''; |
| 25 | }, |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 26 | requiredChooseOption: value => value !== '', |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 27 | 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 Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 37 | 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);}, |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 41 | 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 Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 48 | url: value => ValidatorJS.isURL(value), |
ilanap | 2c9358a | 2017-09-13 14:41:21 +0300 | [diff] [blame^] | 49 | 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 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 68 | }; |
| 69 | } |
| 70 | |
| 71 | static get globalValidationMessagingFunctions() { |
| 72 | return { |
| 73 | required: () => i18n('Field is required'), |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 74 | requiredChooseOption: () => i18n('Field should have one of these options'), |
ilanap | 2c9358a | 2017-09-13 14:41:21 +0300 | [diff] [blame^] | 75 | requiredChoiceWithOther: () => i18n('Field is required'), |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 76 | maxLength: (value, maxLength) => i18n('Field value has exceeded it\'s limit, {maxLength}. current length: {length}', { |
| 77 | length: value.length, |
| 78 | maxLength |
| 79 | }), |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 80 | minLength: (value, minLength) => i18n(`Field value should contain at least ${minLength} characters.`), |
| 81 | pattern: (value, pattern) => i18n(`Field value should match the pattern: ${pattern}.`), |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 82 | numeric: () => i18n('Field value should contain numbers only.'), |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 83 | 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()}.`), |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 87 | 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 Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 95 | general: () => i18n('Field value is invalid.'), |
| 96 | alphanumericWithUnderscores: () => i18n('Field value should contain letters, digits or _ only.') |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 97 | }; |
| 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 Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 131 | itemName = itemName.toLowerCase(); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 132 | return list[itemName] && list[itemName] !== itemId; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | export default Validator; |