|
| 1 | +const expect = require('chai').expect |
| 2 | +const SchemaValidator = require('../../src/schema-validator') |
| 3 | + |
| 4 | +describe('validation-error-converter test', () => { |
| 5 | + |
| 6 | + const schema = { |
| 7 | + $id: 'http://example.com/schemas/schema.json', |
| 8 | + type: 'object', |
| 9 | + additionalProperties: false, |
| 10 | + properties: { |
| 11 | + typeError: { |
| 12 | + type: 'string' |
| 13 | + }, |
| 14 | + additionalPropertyError: { |
| 15 | + type: 'object', |
| 16 | + additionalProperties: false, |
| 17 | + properties: { |
| 18 | + a: { |
| 19 | + type: 'string' |
| 20 | + } |
| 21 | + } |
| 22 | + }, |
| 23 | + requiredError: { |
| 24 | + type: 'object', |
| 25 | + required: ['a'], |
| 26 | + properties: { |
| 27 | + a: { |
| 28 | + type: 'string' |
| 29 | + } |
| 30 | + } |
| 31 | + }, |
| 32 | + constError: { |
| 33 | + type: 'string', |
| 34 | + const: 'dummy' |
| 35 | + }, |
| 36 | + enumError: { |
| 37 | + type: 'string', |
| 38 | + enum: [ |
| 39 | + '1', |
| 40 | + '2', |
| 41 | + '3' |
| 42 | + ] |
| 43 | + }, |
| 44 | + propEnumError: { |
| 45 | + type: 'object', |
| 46 | + propertyNames: { |
| 47 | + enum: [ |
| 48 | + 'a', |
| 49 | + 'b', |
| 50 | + 'c' |
| 51 | + ] |
| 52 | + } |
| 53 | + }, |
| 54 | + formatUrlError: { |
| 55 | + type: 'string', |
| 56 | + format: 'httpUrl' |
| 57 | + }, |
| 58 | + formatError: { |
| 59 | + type: 'string', |
| 60 | + format: 'email' |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + const schemaValidator = new SchemaValidator(schema.$id, [schema]) |
| 65 | + |
| 66 | + it('type', () => { |
| 67 | + const data = { |
| 68 | + typeError: {} |
| 69 | + } |
| 70 | + |
| 71 | + const result = schemaValidator.validate(data) |
| 72 | + expect(result).to.include({ |
| 73 | + field: '.typeError', |
| 74 | + message: "Property 'typeError' must be string.", |
| 75 | + rejected_value: data.typeError |
| 76 | + }) |
| 77 | + }) |
| 78 | + |
| 79 | + it('additionalProperties', () => { |
| 80 | + const data = { |
| 81 | + additionalPropertyError: { |
| 82 | + d: {} |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + const result = schemaValidator.validate(data) |
| 87 | + expect(result).to.include({ |
| 88 | + field: '.additionalPropertyError.d', |
| 89 | + message: "Property 'd' is not supported." |
| 90 | + }) |
| 91 | + }) |
| 92 | + |
| 93 | + it('required', () => { |
| 94 | + const data = { |
| 95 | + requiredError: {} |
| 96 | + } |
| 97 | + |
| 98 | + const result = schemaValidator.validate(data) |
| 99 | + expect(result).to.include({ |
| 100 | + field: '.requiredError', |
| 101 | + message: "Missing property 'a'." |
| 102 | + }) |
| 103 | + }) |
| 104 | + |
| 105 | + it('const', () => { |
| 106 | + const data = { |
| 107 | + constError: 'a' |
| 108 | + } |
| 109 | + |
| 110 | + const result = schemaValidator.validate(data) |
| 111 | + expect(result).to.include({ |
| 112 | + field: '.constError', |
| 113 | + message: "Property 'constError' must have value 'dummy'.", |
| 114 | + rejected_value: 'a' |
| 115 | + }) |
| 116 | + }) |
| 117 | + |
| 118 | + it('enum - values', () => { |
| 119 | + const data = { |
| 120 | + enumError: 'a' |
| 121 | + } |
| 122 | + |
| 123 | + const result = schemaValidator.validate(data) |
| 124 | + expect(result).to.include({ |
| 125 | + field: '.enumError', |
| 126 | + message: "Property 'enumError' with value 'a' does not match any allowed value: 1, 2, 3.", |
| 127 | + rejected_value: 'a' |
| 128 | + }) |
| 129 | + }) |
| 130 | + |
| 131 | + it('enum - properties', () => { |
| 132 | + const data = { |
| 133 | + propEnumError: {d: {}} |
| 134 | + } |
| 135 | + |
| 136 | + const result = schemaValidator.validate(data) |
| 137 | + expect(result).to.include({ |
| 138 | + field: '.propEnumError.d', |
| 139 | + message: "Property 'd' at '.propEnumError' does not match any allowed property: a, b, c.", |
| 140 | + }) |
| 141 | + }) |
| 142 | + |
| 143 | + it('format URL', () => { |
| 144 | + const data = { |
| 145 | + formatUrlError: 'dummy' |
| 146 | + } |
| 147 | + |
| 148 | + const result = schemaValidator.validate(data) |
| 149 | + expect(result).to.include({ |
| 150 | + field: '.formatUrlError', |
| 151 | + message: `Property 'formatUrlError' must be a valid URL. Current value is "dummy"`, |
| 152 | + rejected_value: 'dummy' |
| 153 | + }) |
| 154 | + }) |
| 155 | + |
| 156 | + it('format other', () => { |
| 157 | + const data = { |
| 158 | + formatError: 'dummy' |
| 159 | + } |
| 160 | + |
| 161 | + const result = schemaValidator.validate(data) |
| 162 | + expect(result).to.include({ |
| 163 | + field: '.formatError', |
| 164 | + message: `Property 'formatError' should match format "email"`, |
| 165 | + rejected_value: 'dummy' |
| 166 | + }) |
| 167 | + }) |
| 168 | +}) |
0 commit comments