-
-
Notifications
You must be signed in to change notification settings - Fork 49
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 on non-required keys fails with multiple items with undefined keys #115
Comments
I join you on your comment, |
What is the use case for this behaviour? |
I have the same issue : |
As I wrote, before changing or extending the current behaviour, it would help to understand what is the real life data scenario when such data model is needed. This feature is created to validate the list of objects with unique IDs, such as database IDs, for example - they would be required properties. Also, the analogous keyword "uniqueItems" would not validate non-unique "missing" items (missing items in JSON would be nulls - no other way you can represent it in JSON - and the validation would fail). So following this logic, the current behaviour makes more sense - you can only have one object where property that has to be unique is missing. In any case you can define a slightly modified version of these keyword in your code using this implementation as a starting point. |
I get what you mean, indeed it makes sense if you compare this behaviour with database validation. |
This functionality would be useful in our system. We never let the user create ids, but we may need ways for our FE to determine exactly what item has been updated. As such our API allows an optional |
I'm working on a project where we use JSON files for storing our data. Some data contains arrays where we need to make sure the items are unique based on some keys, but those keys are not always specified, in which case only the present keys should be used to validate the item, which is quite logic to me. At the very least there should be a |
We also have use cases where a user-defined object can have optional properties which need to be unique. Best example: User-Directory sync. A client wants to onboard their users to a service and sync those user-data. Some clients might want to provide an external reference with their data to provide a mapping. Also, the example in the documentation won't work either. |
My use-case: workflow steps. They might be "just" code definitions on their own, but sometimes they might have IDs, which must be unique, this way you can refer to a previous step in one of the next steps. |
Another usecase: We validate loadbalancer rules, which optionally include a priority. IF the priority is given, it must be unique across the LB. It is optional, so when no value is given, our backend generates priorities based on the rule order. |
Using
uniqueItemProperties: ['id']
whereid
is not required should (in my opinion, maybe it should be a separate function) allow multiple elements that have undefinedid
values. e.g.[{id: 'a', val: 1}]
- should pass[{id: 'a', val: 1}, {id: 'b', val: 2}]
- should pass[{id: 'a', val: 1}, {id: 'a', val: 2}]
- should fail[{id: 'a', val: 1}, {val: 2}]
- should pass[{val: 1}, {val: 2}]
- should pass (but doesn't under currentuniqueItemProperties
)I have created a runkit here (code below as well):
https://runkit.com/jamesjansson/ajv-uniqueitemproperties-on-non-required-keys
The text was updated successfully, but these errors were encountered: