Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uniqueItems validation depends on the order of keys #98

Open
epoberezkin opened this issue Oct 26, 2015 · 2 comments
Open

uniqueItems validation depends on the order of keys #98

epoberezkin opened this issue Oct 26, 2015 · 2 comments

Comments

@epoberezkin
Copy link

because JSON.stringify is not stable.

var imjv = require("is-my-json-valid");
var validate = imjv({uniqueItems: true});
console.log(validate([{a:1, b:2}, {a:1, b:2}])); // false - correct
console.log(validate([{a:1, b:2}, {b:2, a:1}])); // true - incorrect

See here: https://tonicdev.com/esp/562df7591e13b90c002896f1

@epoberezkin epoberezkin changed the title uniqueItems validation depend on the order of keys in object uniqueItems validation depends on the order of keys Oct 26, 2015
@cpeers
Copy link

cpeers commented Nov 4, 2015

the problem appears to be with this function:

var unique = function(array) {
  var list = []
  for (var i = 0; i < array.length; i++) {
    list.push(typeof array[i] === 'object' ? JSON.stringify(array[i]) : array[i])
  }
  for (var i = 1; i < list.length; i++) {
    if (list.indexOf(list[i]) !== i) return false
  }
  return true
}

This function does a string comparison of the objects, rather than doing a deep comparison of the objects.

@epoberezkin
Copy link
Author

yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants