| 1 |
import _Ajv from "../ajv" |
| 2 |
import chai from "../chai" |
| 3 |
const should = chai.should() |
| 4 |
|
| 5 |
describe("reporting options", () => { |
| 6 |
describe("verbose", () => { |
| 7 |
it("should add schema, parentSchema and data to errors with verbose option == true", () => { |
| 8 |
testVerbose(new _Ajv({verbose: true})) |
| 9 |
testVerbose(new _Ajv({verbose: true, allErrors: true})) |
| 10 |
|
| 11 |
function testVerbose(ajv) { |
| 12 |
const schema = { |
| 13 |
type: "object", |
| 14 |
properties: { |
| 15 |
foo: {type: "number", minimum: 5}, |
| 16 |
}, |
| 17 |
} |
| 18 |
const validate = ajv.compile(schema) |
| 19 |
|
| 20 |
const data = {foo: 3} |
| 21 |
validate(data).should.equal(false) |
| 22 |
validate.errors.should.have.length(1) |
| 23 |
const err = validate.errors[0] |
| 24 |
|
| 25 |
should.equal(err.schema, 5) |
| 26 |
err.parentSchema.should.eql({type: "number", minimum: 5}) |
| 27 |
err.parentSchema.should.equal(schema.properties.foo) // by reference |
| 28 |
should.equal(err.data, 3) |
| 29 |
} |
| 30 |
}) |
| 31 |
}) |
| 32 |
|
| 33 |
describe("allErrors", () => { |
| 34 |
it('should be disabled inside "not" keyword', () => { |
| 35 |
test(new _Ajv(), false) |
| 36 |
test(new _Ajv({allErrors: true}), true) |
| 37 |
|
| 38 |
function test(ajv, allErrors) { |
| 39 |
let format1called = false, |
| 40 |
format2called = false |
| 41 |
|
| 42 |
ajv.addFormat("format1", () => { |
| 43 |
format1called = true |
| 44 |
return false |
| 45 |
}) |
| 46 |
|
| 47 |
ajv.addFormat("format2", () => { |
| 48 |
format2called = true |
| 49 |
return false |
| 50 |
}) |
| 51 |
|
| 52 |
const schema1 = { |
| 53 |
type: "string", |
| 54 |
allOf: [{format: "format1"}, {format: "format2"}], |
| 55 |
} |
| 56 |
|
| 57 |
ajv.validate(schema1, "abc").should.equal(false) |
| 58 |
ajv.errors.should.have.length(allErrors ? 2 : 1) |
| 59 |
format1called.should.equal(true) |
| 60 |
format2called.should.equal(allErrors) |
| 61 |
|
| 62 |
const schema2 = { |
| 63 |
not: schema1, |
| 64 |
} |
| 65 |
|
| 66 |
format1called = format2called = false |
| 67 |
ajv.validate(schema2, "abc").should.equal(true) |
| 68 |
should.equal(ajv.errors, null) |
| 69 |
format1called.should.equal(true) |
| 70 |
format2called.should.equal(false) |
| 71 |
} |
| 72 |
}) |
| 73 |
}) |
| 74 |
|
| 75 |
describe("logger", () => { |
| 76 |
/** |
| 77 |
* The logger option tests are based on the meta scenario which writes into the logger.warn |
| 78 |
*/ |
| 79 |
|
| 80 |
const origConsoleWarn = console.warn |
| 81 |
let consoleCalled |
| 82 |
|
| 83 |
beforeEach(() => { |
| 84 |
consoleCalled = false |
| 85 |
console.warn = () => (consoleCalled = true) |
| 86 |
}) |
| 87 |
|
| 88 |
afterEach(() => { |
| 89 |
console.warn = origConsoleWarn |
| 90 |
}) |
| 91 |
|
| 92 |
it("no user-defined logger is given - global console should be used", () => { |
| 93 |
const ajv = new _Ajv({meta: false}) |
| 94 |
|
| 95 |
ajv.compile({ |
| 96 |
type: "number", |
| 97 |
minimum: 1, |
| 98 |
}) |
| 99 |
|
| 100 |
should.equal(consoleCalled, true) |
| 101 |
}) |
| 102 |
|
| 103 |
it("user-defined logger is an object - logs should only report to it", () => { |
| 104 |
let loggerCalled = false |
| 105 |
|
| 106 |
const logger = { |
| 107 |
warn: log, |
| 108 |
log: log, |
| 109 |
error: log, |
| 110 |
} |
| 111 |
|
| 112 |
const ajv = new _Ajv({ |
| 113 |
meta: false, |
| 114 |
logger: logger, |
| 115 |
}) |
| 116 |
|
| 117 |
ajv.compile({ |
| 118 |
type: "number", |
| 119 |
minimum: 1, |
| 120 |
}) |
| 121 |
|
| 122 |
should.equal(loggerCalled, true) |
| 123 |
should.equal(consoleCalled, false) |
| 124 |
|
| 125 |
function log() { |
| 126 |
loggerCalled = true |
| 127 |
} |
| 128 |
}) |
| 129 |
|
| 130 |
it("logger option is false - no logs should be reported", () => { |
| 131 |
const ajv = new _Ajv({ |
| 132 |
meta: false, |
| 133 |
logger: false, |
| 134 |
}) |
| 135 |
|
| 136 |
ajv.compile({ |
| 137 |
type: "number", |
| 138 |
minimum: 1, |
| 139 |
}) |
| 140 |
|
| 141 |
should.equal(consoleCalled, false) |
| 142 |
}) |
| 143 |
|
| 144 |
it("logger option is an object without required methods - an error should be thrown", () => { |
| 145 |
const opts: any = { |
| 146 |
meta: false, |
| 147 |
logger: {}, |
| 148 |
} |
| 149 |
;(() => new _Ajv(opts)).should.throw( |
| 150 |
Error, |
| 151 |
/logger must implement log, warn and error methods/ |
| 152 |
) |
| 153 |
}) |
| 154 |
}) |
| 155 |
}) |