test for passContext option

master
Evgeny Poberezkin 2016-02-16 10:12:32 +00:00
parent de65510f1e
commit 8fa4d0f05f
4 changed files with 67 additions and 3 deletions

View File

@ -30,7 +30,6 @@ var {{=$errs}} = errors;
valid{{=$it.level}}
{{??}}
{{?$asyncKeyword}}{{=it.yieldAwait}} {{?}}{{=$ruleValidate.code}}.call(
{{ 'istanbul ignore if'; }}
{{? it.opts.passContext }}this{{??}}self{{?}}
{{ var $validateArgs = $ruleValidate.validate.length; }}
{{? $rDef.compile }}

View File

@ -3,7 +3,6 @@
{{# def.setupKeyword }}
{{## def._validateRef:_v:
{{ 'istanbul ignore if'; }}
{{? it.opts.passContext }}
{{=_v}}.call(this,
{{??}}

View File

@ -1,6 +1,6 @@
{
"name": "ajv",
"version": "3.6.3",
"version": "3.7.0",
"description": "Another JSON Schema Validator",
"main": "lib/ajv.js",
"files": [

View File

@ -481,4 +481,70 @@ describe('Ajv Options', function () {
});
});
});
describe('passContext', function() {
var ajv, contexts;
beforeEach(function() {
contexts = [];
})
describe('= true', function() {
it('should pass this value as context to custom keyword validation function', function() {
var validate = getValidate(true);
var self = {};
validate.call(self, {});
contexts .should.have.length(4);
contexts.forEach(function(ctx) {
ctx .should.equal(self);
});
});
});
describe('= false', function() {
it('should pass ajv instance as context to custom keyword validation function', function() {
var validate = getValidate(false);
var self = {};
validate.call(self, {});
contexts .should.have.length(4);
contexts.forEach(function(ctx) {
ctx .should.equal(ajv);
});
});
})
function getValidate(passContext) {
ajv = Ajv({ passContext: passContext, inlineRefs: false });
ajv.addKeyword('testValidate', { validate: storeContext });
ajv.addKeyword('testCompile', { compile: compileTestValidate });
var schema = {
definitions: {
test1: {
testValidate: true,
testCompile: true,
},
test2: {
allOf: [ { $ref: '#/definitions/test1' } ]
}
},
allOf: [
{ $ref: '#/definitions/test1' },
{ $ref: '#/definitions/test2' }
]
};
return ajv.compile(schema);
}
function storeContext() {
contexts.push(this);
return true;
}
function compileTestValidate() {
return storeContext;
}
});
});