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), |
| 49 | alphanumericWithUnderscores: value => ValidatorJS.isAlphanumeric(value.replace(/_/g, '')) |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 50 | }; |
| 51 | } |
| 52 | |
| 53 | static get globalValidationMessagingFunctions() { |
| 54 | return { |
| 55 | required: () => i18n('Field is required'), |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 56 | requiredChooseOption: () => i18n('Field should have one of these options'), |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 57 | maxLength: (value, maxLength) => i18n('Field value has exceeded it\'s limit, {maxLength}. current length: {length}', { |
| 58 | length: value.length, |
| 59 | maxLength |
| 60 | }), |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 61 | minLength: (value, minLength) => i18n(`Field value should contain at least ${minLength} characters.`), |
| 62 | pattern: (value, pattern) => i18n(`Field value should match the pattern: ${pattern}.`), |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 63 | numeric: () => i18n('Field value should contain numbers only.'), |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 64 | 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()}.`), |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 68 | 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 Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 76 | general: () => i18n('Field value is invalid.'), |
| 77 | alphanumericWithUnderscores: () => i18n('Field value should contain letters, digits or _ only.') |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 78 | }; |
| 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 Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 112 | itemName = itemName.toLowerCase(); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 113 | return list[itemName] && list[itemName] !== itemId; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | export default Validator; |