Unique validation on update fails in test mode


#1

Hi, I’m new to adonisjs
I have followed the instructions here
https://adonisjs.com/docs/4.1/validator#_custom_rules

In development mode it works fine when I tested in postman
I have issue in test mode
It fails on ignoring the row

Here is my rules

get rules() {
  const { user } = this.ctx.request.post()

    if (user.level === consts.USER_LEVEL_CUSTOMER) {
      return {
        username: `required|min:5|unique:users,username,id,${user.id}`,
        nama: `required`,
        email: `required|email|unique:users,email,id,${user.id}`,
        kelamin: `requiredIf:kelamin`,
        no_identitas: `requiredIf:no_identitas|unique:users,no_identitas,id,${user.id}`,
        telepon: `requiredIf:telepon`
      }
    }

    return {
      username: `required|min:5|unique:users,username,id,${user.id}`,
      nama: `required`,
      email: `required|email|unique:users,email,id,${user.id}`,
      kelamin: `required`,
      no_identitas: `required|unique:users,no_identitas,id,${user.id}`,
      telepon: `required`
    }
  }

Here is my test

test('Update user profile', async({ client }) => {
  let updatedUser = user
  updatedUser.nama = 'Updated User'

  const response = await client
    .put('/api/v1/users/profile')
    .send(updatedUser)
    .header('accept', 'application/json')
    .header('Authorization', authorization)
    .accept('json')
    .end()

  // console.log(response.body);
  response.assertStatus(200)
})

Anyone can help me?
Thanks


#2

Here is my rules
Image for rules

This is the test code block
Image for test code block

This is what I got from postman (on development mode)
Image for postman result

This is what I got from terminal (on test mode)
Image for console.log() in terminal


#3

Could you please copy/paste your code here or create gist instead of providing images?


#4

As per your rules, you rely on user.id property on the request body and I don’t see id property being sent from the tests or even from the postman


#5

Sorry, I’m new to forum like this so I think the easiest way to ask is by providing images :smiley:


#6

I got it
I declare a user object from a json file and it has no id

const user = require('../../database/fixtures/user')

Thank you very much


#7

Wait
I’m using JWT and from it I have access for authenticated user
From this user I have proper id

I logged the id and I saw it in terminal


#8

I found the problem
I sent different user object with the authenticated user

  get rules() {
    const { user } = this.ctx.request.post() // this authenticated user has username of 'mukhsin' and if of 1

    if (user.level === consts.USER_LEVEL_CUSTOMER) {
      // here the rules for the validation (user object with username of 'mukhsinn' and id of 8)
      // here the validation fails because it validate unique username of 'mukhsinn' which is available on other row with id 8
      return {
        username: `required|min:5|unique:users,username,id,${user.id}`,
        nama: `required`,
        email: `required|email|unique:users,email,id,${user.id}`,
        kelamin: `requiredIf:kelamin`,
        no_identitas: `requiredIf:no_identitas|unique:users,no_identitas,id,${user.id}`,
        telepon: `requiredIf:telepon`
      }
    }

    return {
      username: `required|min:5|unique:users,username,id,${user.id}`,
      nama: `required`,
      email: `required|email|unique:users,email,id,${user.id}`,
      kelamin: `required`,
      no_identitas: `required|unique:users,no_identitas,id,${user.id}`,
      telepon: `required`
    }
  }

This is all my fault
I didn’t check which user should be validated
The user object I sent should be the same object as the authenticated user

Thanks