| 1 |
import _Ajv from "../ajv" |
| 2 |
import chai from "../chai" |
| 3 |
chai.should() |
| 4 |
|
| 5 |
describe("issue #768, fix passContext in recursive $ref", () => { |
| 6 |
let ajv, contexts: any[] |
| 7 |
|
| 8 |
beforeEach(() => { |
| 9 |
contexts = [] |
| 10 |
}) |
| 11 |
|
| 12 |
describe("passContext = true", () => { |
| 13 |
it("should pass this value as context to user-defined keyword validation function", () => { |
| 14 |
const validate = getValidate(true) |
| 15 |
const self = {} |
| 16 |
validate.call(self, {bar: "a", baz: {bar: "b"}}) |
| 17 |
contexts.should.have.length(2) |
| 18 |
contexts.forEach((ctx) => ctx.should.equal(self)) |
| 19 |
}) |
| 20 |
}) |
| 21 |
|
| 22 |
describe("passContext = false", () => { |
| 23 |
it("should pass ajv instance as context to user-defined keyword validation function", () => { |
| 24 |
const validate = getValidate(false) |
| 25 |
validate({bar: "a", baz: {bar: "b"}}) |
| 26 |
contexts.should.have.length(2) |
| 27 |
contexts.forEach((ctx) => ctx.should.equal(ajv)) |
| 28 |
}) |
| 29 |
}) |
| 30 |
|
| 31 |
describe("ref is fragment and passContext = true", () => { |
| 32 |
it("should pass this value as context to user-defined keyword validation function", () => { |
| 33 |
const validate = getValidateFragments(true) |
| 34 |
const self = {} |
| 35 |
validate.call(self, {baz: {corge: "a", quux: {baz: {corge: "b"}}}}) |
| 36 |
contexts.should.have.length(2) |
| 37 |
contexts.forEach((ctx) => ctx.should.equal(self)) |
| 38 |
}) |
| 39 |
}) |
| 40 |
|
| 41 |
describe("ref is fragment and passContext = false", () => { |
| 42 |
it("should pass ajv instance as context to user-defined keyword validation function", () => { |
| 43 |
const validate = getValidateFragments(false) |
| 44 |
validate({baz: {corge: "a", quux: {baz: {corge: "b"}}}}) |
| 45 |
contexts.should.have.length(2) |
| 46 |
contexts.forEach((ctx) => ctx.should.equal(ajv)) |
| 47 |
}) |
| 48 |
}) |
| 49 |
|
| 50 |
function getValidate(passContext) { |
| 51 |
ajv = new _Ajv({passContext}) |
| 52 |
ajv.addKeyword({keyword: "testValidate", validate: storeContext}) |
| 53 |
|
| 54 |
const schema = { |
| 55 |
$id: "foo", |
| 56 |
type: "object", |
| 57 |
required: ["bar"], |
| 58 |
properties: { |
| 59 |
bar: {testValidate: true}, |
| 60 |
baz: { |
| 61 |
$ref: "foo", |
| 62 |
}, |
| 63 |
}, |
| 64 |
} |
| 65 |
|
| 66 |
return ajv.compile(schema) |
| 67 |
} |
| 68 |
|
| 69 |
function getValidateFragments(passContext) { |
| 70 |
ajv = new _Ajv({passContext}) |
| 71 |
ajv.addKeyword({keyword: "testValidate", validate: storeContext}) |
| 72 |
|
| 73 |
ajv.addSchema({ |
| 74 |
$id: "foo", |
| 75 |
definitions: { |
| 76 |
bar: { |
| 77 |
type: "object", |
| 78 |
properties: { |
| 79 |
baz: { |
| 80 |
$ref: "boo", |
| 81 |
}, |
| 82 |
}, |
| 83 |
}, |
| 84 |
}, |
| 85 |
}) |
| 86 |
|
| 87 |
ajv.addSchema({ |
| 88 |
$id: "boo", |
| 89 |
type: "object", |
| 90 |
required: ["corge"], |
| 91 |
properties: { |
| 92 |
quux: {$ref: "foo#/definitions/bar"}, |
| 93 |
corge: {testValidate: true}, |
| 94 |
}, |
| 95 |
}) |
| 96 |
|
| 97 |
return ajv.compile({$ref: "foo#/definitions/bar"}) |
| 98 |
} |
| 99 |
|
| 100 |
function storeContext(this: any) { |
| 101 |
contexts.push(this) |
| 102 |
return true |
| 103 |
} |
| 104 |
}) |