|
| 1 | +import assert from 'assert'; |
| 2 | + |
| 3 | +import SpecBuilder from "../../../../src/OpenSpec/SpecBuilder"; |
| 4 | +import SpecLibrary from "../../../../src/OpenSpec/SpecLibrary"; |
| 5 | + |
| 6 | + |
| 7 | +function getSpecInstance() |
| 8 | +{ |
| 9 | + let specData = { type: 'array', items: { type: 'string' } }; |
| 10 | + let spec = SpecBuilder.getInstance().build(specData, new SpecLibrary()); |
| 11 | + |
| 12 | + return spec; |
| 13 | +} |
| 14 | + |
| 15 | +function getValidValueInstance() |
| 16 | +{ |
| 17 | + return ['one', 'two', 'three']; |
| 18 | +} |
| 19 | + |
| 20 | +function getInvalidValueInstance() |
| 21 | +{ |
| 22 | + return [1, 2, 3]; |
| 23 | +} |
| 24 | + |
| 25 | +describe('ArrayValidationTest', function() { |
| 26 | + |
| 27 | + it('testValidValue', function() { |
| 28 | + let spec = getSpecInstance(); |
| 29 | + let value = getValidValueInstance(); |
| 30 | + |
| 31 | + let result = spec.validate(value); |
| 32 | + |
| 33 | + assert(result, "Given value not recognized by the spec, even when it should."); |
| 34 | + }); |
| 35 | + |
| 36 | + it('testInvalidValue', function() { |
| 37 | + let spec = getSpecInstance(); |
| 38 | + let value = getInvalidValueInstance(); |
| 39 | + |
| 40 | + let result = spec.validate(value); |
| 41 | + |
| 42 | + assert(!result, "Given value recognized by the spec, even when it should not."); |
| 43 | + }); |
| 44 | + |
| 45 | + it('testSeveralValidations', function() { |
| 46 | + |
| 47 | + let specData, value, spec, result; |
| 48 | + |
| 49 | + // 1 ------------------------------------------------ |
| 50 | + specData = { |
| 51 | + 'type': 'array', |
| 52 | + 'items': { |
| 53 | + 'type': 'array', |
| 54 | + 'items': { |
| 55 | + 'type': 'mixed', |
| 56 | + 'options': [ |
| 57 | + {'type':'string'}, |
| 58 | + {'type':'boolean'} |
| 59 | + ] |
| 60 | + } |
| 61 | + } |
| 62 | + }; |
| 63 | + |
| 64 | + value = [ |
| 65 | + [true, false, true], |
| 66 | + ['string', 'value', false], |
| 67 | + [], |
| 68 | + ['other', 'another one'] |
| 69 | + ]; |
| 70 | + |
| 71 | + spec = SpecBuilder.getInstance().build(specData, new SpecLibrary()); |
| 72 | + result = spec.validate(value); |
| 73 | + |
| 74 | + assert(result, "Not validated array of arrays of string|boolean."); |
| 75 | + // End: 1 ------------------------------------------------ |
| 76 | + |
| 77 | + // 2 ------------------------------------------------ |
| 78 | + specData = { |
| 79 | + 'type': 'object', |
| 80 | + 'extensible': true, |
| 81 | + 'fields': { |
| 82 | + 'name': {'type': 'string'}, |
| 83 | + 'happy': {'type': 'boolean'}, |
| 84 | + 'age': {'type': 'string'}, |
| 85 | + 'hobbies': { |
| 86 | + 'type': 'array', |
| 87 | + 'items': { |
| 88 | + 'type': 'string' |
| 89 | + } |
| 90 | + }, |
| 91 | + 'address': { |
| 92 | + 'type': 'object', |
| 93 | + 'fields': { |
| 94 | + 'country': {'type': 'string'}, |
| 95 | + 'city': {'type': 'string'}, |
| 96 | + 'phones': {'type': 'array', 'items': {'type': 'string'}}, |
| 97 | + } |
| 98 | + }, |
| 99 | + } |
| 100 | + }; |
| 101 | + |
| 102 | + value = { |
| 103 | + 'name': 'Daniel', |
| 104 | + 'happy': true, |
| 105 | + 'age': '37', |
| 106 | + 'hobbies': ['numismatics', 'rubik cubes'], |
| 107 | + 'address': { |
| 108 | + 'country': 'Argentina', |
| 109 | + 'city': 'Tandil', |
| 110 | + 'phones': [] |
| 111 | + }, |
| 112 | + 'comments': '' |
| 113 | + }; |
| 114 | + |
| 115 | + spec = SpecBuilder.getInstance().build(specData, new SpecLibrary()); |
| 116 | + result = spec.validate(value); |
| 117 | + |
| 118 | + assert(result, "Not validated person info."); |
| 119 | + // End: 2 ------------------------------------------------ |
| 120 | + |
| 121 | + // 3 ------------------------------------------------ |
| 122 | + specData = {'type' : 'array'}; |
| 123 | + let values = [ |
| 124 | + [], // Empty array |
| 125 | + [false, true, true], // Boolean array |
| 126 | + ["hello", "bye"], // String array |
| 127 | + [null, null, null], // Null array |
| 128 | + [{'name' : 'Daniel', 'alias' : 'Dani'}, {'a': 1, 'b': [2, 3]}], // Object array |
| 129 | + [[], {'field' : 'value'}, [null, false, "hi!"]], // Array of arrays (bidimensional array / matrix) |
| 130 | + ]; |
| 131 | + |
| 132 | + spec = SpecBuilder.getInstance().build(specData, new SpecLibrary()); |
| 133 | + for (let i = 0; i < values.length; i++) { |
| 134 | + let value = values[i]; |
| 135 | + |
| 136 | + result = spec.validate(value); |
| 137 | + assert(result, "Not validated array."); |
| 138 | + } |
| 139 | + |
| 140 | + let complexValue = values; |
| 141 | + result = spec.validate(complexValue); |
| 142 | + assert(result, "Not validated array of any type of items."); |
| 143 | + // End: 3 ------------------------------------------------ |
| 144 | + }); |
| 145 | + |
| 146 | +}); |
0 commit comments