Skip to content

Commit

Permalink
option addUsedSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Jan 15, 2016
1 parent 8c4557f commit c99d675
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,7 @@ Defaults:
schemas: {},
meta: true,
validateSchema: true,
addUsedSchema: true,
inlineRefs: true,
loopRequired: Infinity,
multipleOfPrecision: false,
Expand Down Expand Up @@ -701,6 +702,7 @@ Defaults:
- `true` (default) - if the validation fails, throw the exception.
- `"log"` - if the validation fails, log error.
- `false` - skip schema validation.
- _addUsedSchema_: by default methods `compile` and `validate` add schemas to the instance if they have `id` property that doesn't start with "#". If `id` is present and it is not unique the exception will be thrown. Set this option to `false` to skip adding schemas to the instance and the `id` uniqueness check when these methods are used. This option does not affect `addSchema` method.
- _inlineRefs_: Affects compilation of referenced schemas. Option values:
- `true` (default) - the referenced schemas that don't have refs in them are inlined, regardless of their size - that substantially improves performance at the cost of the bigger size of compiled schema functions.
- `false` - to not inline referenced schemas (they will be compiled as separate functions).
Expand Down
10 changes: 6 additions & 4 deletions lib/ajv.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ function Ajv(opts) {
// can key/id have # inside?
key = resolve.normalizeId(key || schema.id);
checkUnique(key);
var schemaObj = self._schemas[key] = _addSchema(schema, _skipValidation);
var schemaObj = self._schemas[key] = _addSchema(schema, _skipValidation, true);
schemaObj.meta = _meta;
}

Expand Down Expand Up @@ -195,14 +195,16 @@ function Ajv(opts) {
}


function _addSchema(schema, skipValidation) {
function _addSchema(schema, skipValidation, shouldAddSchema) {
if (typeof schema != 'object') throw new Error('schema should be object');
var jsonStr = stableStringify(schema);
var cached = self._cache.get(jsonStr);
if (cached) return cached;

shouldAddSchema = shouldAddSchema || self.opts.addUsedSchema !== false;

var id = resolve.normalizeId(schema.id);
if (id) checkUnique(id);
if (id && shouldAddSchema) checkUnique(id);

if (self.opts.validateSchema !== false && !skipValidation)
validateSchema(schema, true);
Expand All @@ -216,7 +218,7 @@ function Ajv(opts) {
jsonStr: jsonStr,
});

if (id[0] != '#') self._refs[id] = schemaObj;
if (id[0] != '#' && shouldAddSchema) self._refs[id] = schemaObj;
self._cache.put(jsonStr, schemaObj);

return schemaObj;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ajv",
"version": "3.2.3",
"version": "3.3.0",
"description": "Another JSON Schema Validator",
"main": "lib/ajv.js",
"files": [
Expand Down
80 changes: 80 additions & 0 deletions spec/options.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,4 +401,84 @@ describe('Ajv Options', function () {
}
});
});


describe('addUsedSchema', function() {
[true, undefined].forEach(function (optionValue) {
describe('= ' + optionValue, function() {
var ajv;

beforeEach(function() {
ajv = Ajv({ addUsedSchema: optionValue });
});

describe('compile and validate', function() {
it('should add schema', function() {
var schema = { id: 'str', type: 'string' };
var validate = ajv.compile(schema);
validate('abc') .should.equal(true);
validate(1) .should.equal(false);
ajv.getSchema('str') .should.equal(validate);

var schema = { id: 'int', type: 'integer' };
ajv.validate(schema, 1) .should.equal(true);
ajv.validate(schema, 'abc') .should.equal(false);
ajv.getSchema('int') .should.be.a('function');
});

it('should throw with duplicate ID', function() {
ajv.compile({ id: 'str', type: 'string' });
should.throw(function() {
ajv.compile({ id: 'str', minLength: 2 });
});

var schema = { id: 'int', type: 'integer' };
var schema2 = { id: 'int', minimum: 0 };
ajv.validate(schema, 1) .should.equal(true);
should.throw(function() {
ajv.validate(schema2, 1);
});
});
});
});
});

describe('= false', function() {
var ajv;

beforeEach(function() {
ajv = Ajv({ addUsedSchema: false });
});


describe('compile and validate', function() {
it('should NOT add schema', function() {
var schema = { id: 'str', type: 'string' };
var validate = ajv.compile(schema);
validate('abc') .should.equal(true);
validate(1) .should.equal(false);
should.equal(ajv.getSchema('str'), undefined);

var schema = { id: 'int', type: 'integer' };
ajv.validate(schema, 1) .should.equal(true);
ajv.validate(schema, 'abc') .should.equal(false);
should.equal(ajv.getSchema('int'), undefined);
});

it('should NOT throw with duplicate ID', function() {
ajv.compile({ id: 'str', type: 'string' });
should.not.throw(function() {
ajv.compile({ id: 'str', minLength: 2 });
});

var schema = { id: 'int', type: 'integer' };
var schema2 = { id: 'int', minimum: 0 };
ajv.validate(schema, 1) .should.equal(true);
should.not.throw(function() {
ajv.validate(schema2, 1) .should.equal(true);
});
});
});
});
});
});

0 comments on commit c99d675

Please sign in to comment.