2 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 import Validator from 'src/nfvo-utils/Validator.js';
19 describe('Validator ', () => {
21 it('class does exist', () => {
22 expect(Validator).toBeTruthy();
25 it('returns global validation functions', () => {
26 expect(Validator.globalValidationFunctions).toBeTruthy();
29 it('returns global validation messages', () => {
30 expect(Validator.globalValidationMessagingFunctions).toBeTruthy();
33 it('validates data per specific types list', () => {
34 const types = ['required', 'maxLength', 'minLength', 'pattern', 'numeric', 'maximum', 'minimum',
35 'alphanumeric', 'alphanumericWithSpaces', 'validateName', 'validateVendorName', 'freeEnglishText', 'email', 'ip', 'url', 'maximumExclusive', 'minimumExclusive'];
37 for (let i = 0; i < types.length; i++) {
38 expect(Validator.globalValidationFunctions[types[i]]).toBeTruthy();
42 it('gives validation messages per specific types list', () => {
43 const types = ['required', 'maxLength', 'minLength', 'pattern', 'numeric', 'maximum', 'minimum',
44 'alphanumeric', 'alphanumericWithSpaces', 'validateName', 'validateVendorName', 'freeEnglishText', 'email', 'ip', 'url', 'maximumExclusive', 'minimumExclusive'];
46 for (let i = 0; i < types.length; i++) {
47 expect(Validator.globalValidationFunctions[types[i]]).toBeTruthy();
51 it('returns a validation response of {isValid, errorText} when validating only by validator.js', () => {
52 const result = Validator.validateItem('a', null, 'required');
53 const keys = Object.keys(result);
54 expect(keys.length).toBe(2);
55 expect(keys).toContain('isValid');
56 expect(keys).toContain('errorText');
59 it('returns a validation response of {isValid, errorText} when validating with custom functions', () => {
60 const errorText = 'ran custom validation';
61 const result = Validator.validate('myfield','a', [{type: 'required', data: null}], {}, { 'myfield' : () => { return { isValid: false, errorText};} });
62 const keys = Object.keys(result);
63 expect(keys.length).toBe(2);
64 expect(keys).toContain('isValid');
65 expect(keys).toContain('errorText');
66 expect(result.errorText).toBe(errorText);