|
| 1 | +import {validate} from './validate'; |
| 2 | +import {validateArray} from './validate_array'; |
| 3 | +import v8 from '../reference/v8.json' with {type: 'json'}; |
| 4 | +import {describe, test, expect} from 'vitest'; |
| 5 | + |
| 6 | +describe('Validate Array', () => { |
| 7 | + test('Should return error if type is not array', () => { |
| 8 | + let errors = validateArray({ |
| 9 | + validateSpec: validate, |
| 10 | + key: 'testArray', |
| 11 | + value: '3', |
| 12 | + valueSpec: {type: 'array', value: 'number'}, |
| 13 | + styleSpec: v8 |
| 14 | + }); |
| 15 | + expect(errors).toHaveLength(1); |
| 16 | + expect(errors[0].message).toBe('testArray: array expected, string found'); |
| 17 | + |
| 18 | + errors = validateArray({ |
| 19 | + validateSpec: validate, |
| 20 | + key: 'testArray', |
| 21 | + value: true, |
| 22 | + valueSpec: {type: 'array', value: 'number'}, |
| 23 | + styleSpec: v8 |
| 24 | + }); |
| 25 | + expect(errors).toHaveLength(1); |
| 26 | + expect(errors[0].message).toBe('testArray: array expected, boolean found'); |
| 27 | + |
| 28 | + errors = validateArray({ |
| 29 | + validateSpec: validate, |
| 30 | + key: 'testArray', |
| 31 | + value: null, |
| 32 | + valueSpec: {type: 'array', value: 'number'}, |
| 33 | + styleSpec: v8 |
| 34 | + }); |
| 35 | + expect(errors).toHaveLength(1); |
| 36 | + expect(errors[0].message).toBe('testArray: array expected, null found'); |
| 37 | + |
| 38 | + errors = validateArray({ |
| 39 | + validateSpec: validate, |
| 40 | + key: 'testArray', |
| 41 | + value: {x: 1, y: 1}, |
| 42 | + valueSpec: {type: 'array', value: 'number'}, |
| 43 | + styleSpec: v8 |
| 44 | + }); |
| 45 | + expect(errors).toHaveLength(1); |
| 46 | + expect(errors[0].message).toBe('testArray: array expected, object found'); |
| 47 | + |
| 48 | + errors = validateArray({ |
| 49 | + validateSpec: validate, |
| 50 | + key: 'testArray', |
| 51 | + value: 123, |
| 52 | + valueSpec: {type: 'array', value: 'number'}, |
| 53 | + styleSpec: v8 |
| 54 | + }); |
| 55 | + expect(errors).toHaveLength(1); |
| 56 | + expect(errors[0].message).toBe('testArray: array expected, number found'); |
| 57 | + }); |
| 58 | + |
| 59 | + test('Should return error if array length does not match spec', () => { |
| 60 | + const errors = validateArray({ |
| 61 | + validateSpec: validate, |
| 62 | + key: 'testArray', |
| 63 | + value: [1, 2, 3], |
| 64 | + valueSpec: {type: 'array', value: 'number', length: 2}, |
| 65 | + styleSpec: v8 |
| 66 | + }); |
| 67 | + expect(errors).toHaveLength(1); |
| 68 | + expect(errors[0].message).toBe('testArray: array length 2 expected, length 3 found'); |
| 69 | + }); |
| 70 | + |
| 71 | + test('Should pass if array length matches spec', () => { |
| 72 | + const errors = validateArray({ |
| 73 | + validateSpec: validate, |
| 74 | + key: 'testArray', |
| 75 | + value: [1, 2], |
| 76 | + valueSpec: {type: 'array', value: 'number', length: 2}, |
| 77 | + styleSpec: v8 |
| 78 | + }); |
| 79 | + expect(errors).toHaveLength(0); |
| 80 | + }); |
| 81 | + |
| 82 | + test('Should return error if array contains invalid element types', () => { |
| 83 | + let errors = validateArray({ |
| 84 | + validateSpec: validate, |
| 85 | + key: 'testArray', |
| 86 | + value: ['1', '2'], |
| 87 | + valueSpec: {type: 'array', value: 'number'}, |
| 88 | + styleSpec: v8 |
| 89 | + }); |
| 90 | + expect(errors).toHaveLength(2); |
| 91 | + expect(errors[0].message).toBe('testArray[0]: number expected, string found'); |
| 92 | + expect(errors[1].message).toBe('testArray[1]: number expected, string found'); |
| 93 | + |
| 94 | + errors = validateArray({ |
| 95 | + validateSpec: validate, |
| 96 | + key: 'testArray', |
| 97 | + value: [1, true, 3], |
| 98 | + valueSpec: {type: 'array', value: 'number'}, |
| 99 | + styleSpec: v8 |
| 100 | + }); |
| 101 | + expect(errors).toHaveLength(1); |
| 102 | + expect(errors[0].message).toBe('testArray[1]: number expected, boolean found'); |
| 103 | + |
| 104 | + errors = validateArray({ |
| 105 | + validateSpec: validate, |
| 106 | + key: 'testArray', |
| 107 | + value: [1, 2, null], |
| 108 | + valueSpec: {type: 'array', value: 'number'}, |
| 109 | + styleSpec: v8 |
| 110 | + }); |
| 111 | + expect(errors).toHaveLength(1); |
| 112 | + expect(errors[0].message).toBe('testArray[2]: number expected, null found'); |
| 113 | + }); |
| 114 | + |
| 115 | + test('Should pass if array contains valid element types', () => { |
| 116 | + let errors = validateArray({ |
| 117 | + validateSpec: validate, |
| 118 | + key: 'testArray', |
| 119 | + value: [1, 2, 3], |
| 120 | + valueSpec: {type: 'array', value: 'number'}, |
| 121 | + styleSpec: v8 |
| 122 | + }); |
| 123 | + expect(errors).toHaveLength(0); |
| 124 | + |
| 125 | + errors = validateArray({ |
| 126 | + validateSpec: validate, |
| 127 | + key: 'testArray', |
| 128 | + value: ['a', 'b', 'c'], |
| 129 | + valueSpec: {type: 'array', value: 'string'}, |
| 130 | + styleSpec: v8 |
| 131 | + }); |
| 132 | + expect(errors).toHaveLength(0); |
| 133 | + |
| 134 | + errors = validateArray({ |
| 135 | + validateSpec: validate, |
| 136 | + key: 'testArray', |
| 137 | + value: [true, false, true], |
| 138 | + valueSpec: {type: 'array', value: 'boolean'}, |
| 139 | + styleSpec: v8 |
| 140 | + }); |
| 141 | + expect(errors).toHaveLength(0); |
| 142 | + }); |
| 143 | + |
| 144 | + test('Should validate arrays with complex element specs', () => { |
| 145 | + const errors = validateArray({ |
| 146 | + validateSpec: validate, |
| 147 | + key: 'testArray', |
| 148 | + value: [1, 2, 3], |
| 149 | + valueSpec: { |
| 150 | + type: 'array', |
| 151 | + value: { |
| 152 | + type: 'number' |
| 153 | + } |
| 154 | + }, |
| 155 | + styleSpec: v8 |
| 156 | + }); |
| 157 | + expect(errors).toHaveLength(0); |
| 158 | + }); |
| 159 | +}); |
0 commit comments