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

uniqueItemsProperties - combined unique properties #386

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = {
files: ["*.ts"],
rules: {
...tsConfig.rules,
complexity: ["error", 18],
complexity: ["error", 26],
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-unnecessary-condition": "warn",
"@typescript-eslint/no-unsafe-assignment": "off",
Expand Down
41 changes: 40 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,11 @@ The keyword allows to check that some properties in array items are unique.

This keyword applies only to arrays. If the data is not an array, the validation succeeds.

The value of this keyword must be an array of strings - property names that should have unique values across all items.
The value of this keyword must be either be an array of strings, an array or array of strings, or a combination of the two.

##### Where Values are Array of Strings

Where the values are an array of strings the strings should be property names that should have unique values across all items.

```javascript
const schema = {
Expand All @@ -293,6 +297,41 @@ const invalidData2 = [
]
```

##### Where Values are Arrays of Array of Strings

Where the values are arrays of array of strings the strings, property names that are used the the nested array should be unique together across all items.

```javascript
const schema = {
type: "array",
uniqueItemProperties: [["id", "name"]],
}

const validData1 = [
{id: 1, name: "taco"},
{id: 2, name: "burrito"},
{id: 3, name: "salsa"},
]

const validData2 = [
{id: 1, name: "taco"},
{id: 1, name: "burrito"}, // duplicate "id"
{id: 3, name: "salsa"},
]

const validData3 = [
{id: 1, name: "taco"},
{id: 2, name: "taco"}, // duplicate "name"
{id: 3, name: "salsa"},
]

const invalidData = [
{id: 1, name: "taco"},
{id: 1, name: "taco"}, // duplicate "name" and 'id
{id: 3, name: "salsa"},
]
```

This keyword is contributed by [@blainesch](https://github.com/blainesch).

**Please note**: currently `uniqueItemProperties` is not supported in [standalone validation code](https://github.com/ajv-validator/ajv/blob/master/docs/standalone.md) - it has to be implemented as [`code` keyword](https://github.com/ajv-validator/ajv/blob/master/docs/keywords.md#define-keyword-with-code-generation-function) to support it (PR is welcome).
Expand Down
Loading