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

uniqueItemProperties is not working #107

Closed
Diana131194 opened this issue Nov 12, 2019 · 7 comments
Closed

uniqueItemProperties is not working #107

Diana131194 opened this issue Nov 12, 2019 · 7 comments

Comments

@Diana131194
Copy link

Hi,
i am trying to use the "uniqueItemProperties" keyword in a json scema like this:
{
"type": "array",
"uniqueItemProperties": ["username"],
"items": {
"type": "object",
"required": [
"username",
"email"
],
"properties": {
"id": {
"type": "integer"
},
"username": {
"type": "string"
},
"email": {
"type": "string",
"format": "email"
}
}
}
}

But, when i am using this schema on a json that has 2 objects with the same username the validation function return true.
Any ideas what am i missing?

Thanks

@epoberezkin
Copy link
Member

Difficult to say without the code sample. You may have missed adding the keyword to ajv instance

@Diana131194
Copy link
Author

Here is the code sample in my project:

const Ajv = require('Ajv');
const ajv = new Ajv();
const normalise = require('ajv-error-messages');
const schema = require('./schema.json');
require("ajv-keywords")(ajv, ["uniqueItemProperties"])

const validate = ajv.compile(schema);
var start = new Date().getTime();
const valid = validate(data);
var end = new Date().getTime();
console.log((end - start) + " milliseconds");

Thank you!

@epoberezkin
Copy link
Member

Looks correct. Please submit something that can be executed to runkit.com - I.e. without private dependencies, with data object. Are you sure that the data you pass is the array? And that the environment you are in parses JSON correctly in require? Anyway, full sample in run kit will allow to understand what is wrong.

@Diana131194
Copy link
Author

Thank you for your help, i see now that it works.
I have another question.
Can i check a uniqueness of two entries together?
For example, if i want the key of the object to be the username and the email (so if only the username is duplicated in 2 objects it's ok, but if the username and the email is duplicate in 2 objects it is not ok).
Is it possible to check something like this with the Ajv?
Thank you!

@epoberezkin
Copy link
Member

epoberezkin commented Dec 7, 2019

@Diana131194 this is not how keyword is defined, it expects each property to have unique values across all items, not the combination of properties.

You can extend this keyword to accept arrays of strings in addition to strings as items in the array to pass to the keyword. So in your case the schema would look like: {"uniqueItemProperties": [["email", "username"]]} (obviously it won't work now - it needs changing the keyword implementation).

If it is not a massive complication it can be merged - I suggest submitting a separate issue with a feature proposal to extend it if you are going to implement it.

@SimonKlausLudwig
Copy link

Any update on this feature.

@atng
Copy link

atng commented Apr 16, 2023

Implimented in PR here @SimonKlausLudwig @Diana131194

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

No branches or pull requests

4 participants