AdonisJS v6 is here. Learn more in our release blog post.

distinct

The distinct rule ensures that all values of a property inside an array are unique. The validation rule only works with the array schema type.

Assuming you have an array of objects, each defining a product id property and you want to ensure that no duplicates product ids are being used.

Sample Data
{
"products": [
{
"id": 1,
"quantity": 4,
},
{
"id": 3,
"quantity": 10,
},
{
"id": 8,
"quantity": 1,
}
]
}

The rule is applied on the array itself and NOT its members.

Validation rule
import { schema, rules } from '@ioc:Adonis/Core/Validator'
{
products: schema
.array([
rules.distinct('id')
])
.members(schema.object().members({
id: schema.number(),
quantity: schema.number(),
}))
}

You can also use the distinct rule with an array of literal values by using the wildcard * keyword. For example:

Sample Data
{
"tags": [1, 10, 15, 8]
}
Validation rule
{
tags: schema
.array([
rules.distinct('*')
])
.members(schema.number())
}