Several unique fields


#1

Hello, in my database I have a single index with more than one column.

Example:

table.string(‘ddd’).notNullable()
table.string(‘number’).notNullable()
table.unique([‘number’, ‘ddd’])

const rules = {
      ddd: 'required|unique:phones',
      number: 'required|unique:phones'
    }

  const validation = await validate(request.all(), rules)

That way the above validator will check if the ddd column is unique and then verify that the number column is unique and will not check if the two columns together are unique. Does anyone have a solution?


#2

I don’t think there is any default solution for this.
You’ll have to write your own validation rule for this https://adonisjs.com/docs/4.1/validator#_extending_validator


#3

@duducp Yes rolling your own validation rule is the simplest and the best way to achieve this.

Feel free to ask questions, if you have trouble implementing your own rule