Unique validation failing on update


#1

Trying to add unique validation in my update handler like this:

const book = await Book.find(params.id);
    if (!book) {
      return response.status(404).json({ data: 'Resource not found' });
    }

    const rules = {
      title: 'required|accepted|unique:books,title,id,${book.primaryKeyValue}',
      isbn: 'required|accepted|unique:books,isbn,id,${book.primaryKeyValue}',
      publisher_name: 'required|accepted',
      author_name: 'required'
    };

but the validation fails for the the unique rule. What am I missing? I am new to Adonis.


#2

Since you have a template literal - ${book.primaryKeyValue} - shouldn’t you be using a back-tick ` instead of a single quote '?

const rules = {
      title: `required|accepted|unique:books,title,id,${book.primaryKeyValue}`,
      isbn: `required|accepted|unique:books,isbn,id,${book.primaryKeyValue}`,
      publisher_name: 'required|accepted',
      author_name: 'required'
    };

#3

Thanks for the tip. It worked.