Skip to content

Commit

Permalink
feat: throw exception if schema id is not a string and report error, c…
Browse files Browse the repository at this point in the history
…loses #380
  • Loading branch information
epoberezkin committed Feb 4, 2017
1 parent f5bcd57 commit de0c827
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/ajv.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ function addSchema(schema, key, _skipValidation, _meta) {
for (var i=0; i<schema.length; i++) this.addSchema(schema[i], undefined, _skipValidation, _meta);
return;
}
// can key/id have # inside?
if (schema.id !== undefined && typeof schema.id != 'string')
throw new Error('schema id must be string');
key = resolve.normalizeId(key || schema.id);
checkUnique(this, key);
this._schemas[key] = this._addSchema(schema, _skipValidation, _meta, true);
Expand Down
9 changes: 9 additions & 0 deletions spec/ajv.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,15 @@ describe('Ajv', function () {
it('should throw if schema is not an object', function() {
should.throw(function() { ajv.addSchema('foo'); });
});

it('should throw if schema id is not a string', function() {
try {
ajv.addSchema({ id: 1, type: 'integer' });
throw new Error('should have throw exception');
} catch(e) {
e.message .should.equal('schema id must be string');
}
});
});


Expand Down

0 comments on commit de0c827

Please sign in to comment.