Skip to content

Commit

Permalink
Merge pull request #343 from epoberezkin/fix-equal
Browse files Browse the repository at this point in the history
fix: compare Date and RegExp objects, closes #342
  • Loading branch information
Evgeny Poberezkin committed Nov 14, 2016
2 parents a0e647e + 00bf5c7 commit c6127ee
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
15 changes: 13 additions & 2 deletions lib/compile/equal.js
@@ -1,5 +1,7 @@
'use strict';

/*eslint complexity: 0*/

module.exports = function equal(a, b) {
if (a === b) return true;

Expand All @@ -18,11 +20,20 @@ module.exports = function equal(a, b) {

if (a && b && typeof a === 'object' && typeof b === 'object') {
var keys = Object.keys(a);

if (keys.length !== Object.keys(b).length) return false;

var dateA = a instanceof Date
, dateB = b instanceof Date;
if (dateA && dateB) return a.getTime() == b.getTime();
if (dateA != dateB) return false;

var regexpA = a instanceof RegExp
, regexpB = b instanceof RegExp;
if (regexpA && regexpB) return a.toString() == b.toString();
if (regexpA != regexpB) return false;

for (i = 0; i < keys.length; i++)
if (b[keys[i]] === undefined) return false;
if (!Object.prototype.hasOwnProperty.call(b, keys[i])) return false;

for (i = 0; i < keys.length; i++)
if(!equal(a[keys[i]], b[keys[i]])) return false;
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "ajv",
"version": "4.8.2",
"version": "4.9.0",
"description": "Another JSON Schema Validator",
"main": "lib/ajv.js",
"webpack": "dist/ajv.bundle.js",
Expand Down
1 change: 1 addition & 0 deletions spec/.eslintrc.json
Expand Up @@ -8,6 +8,7 @@
"globals": {
"describe": false,
"it": false,
"before": false,
"beforeEach": false
}
}
29 changes: 29 additions & 0 deletions spec/issues.spec.js
Expand Up @@ -467,3 +467,32 @@ describe('issue #259, support validating [meta-]schemas against themselves', fun
ajv.addMetaSchema(hyperSchema);
});
});


describe('issue #342, support uniqueItems with some non-JSON objects', function() {
var validate;

before(function() {
var ajv = new Ajv;
validate = ajv.compile({ uniqueItems: true });
});

it('should allow different RegExps', function() {
validate([/foo/, /bar/]) .should.equal(true);
validate([/foo/ig, /foo/gi]) .should.equal(false);
validate([/foo/, {}]) .should.equal(true);
});

it('should allow different Dates', function() {
validate([new Date('2016-11-11'), new Date('2016-11-12')]) .should.equal(true);
validate([new Date('2016-11-11'), new Date('2016-11-11')]) .should.equal(false);
validate([new Date('2016-11-11'), {}]) .should.equal(true);
});

it('should allow undefined properties', function() {
validate([{}, {foo: undefined}]) .should.equal(true);
validate([{foo: undefined}, {}]) .should.equal(true);
validate([{foo: undefined}, {bar: undefined}]) .should.equal(true);
validate([{foo: undefined}, {foo: undefined}]) .should.equal(false);
});
});

0 comments on commit c6127ee

Please sign in to comment.