0

I want the $beforeValidate to wait for the async operation to complete as it updates the object to make it pass validation. But currently the $beforeValidate completes and rejects the record as it is not waiting for the async operation to complete.

class Label extends Model {
 async $beforeValidate() {
  if(this.name === undefined){
    const res = await axios.get('/getSomeName')
    console.log(res.body)
    this.name = res.body
  }
 }
 static get jsonSchema () {
    return {
      type: 'object',
      required: ['name'],
      properties: {
        id: { type: 'integer' },
        name: { type: 'string' }
      }
    }
 }
}

Now when i insert a label with name undefined I can see that the validation error is raised before the async API call has even finished

await Label.query().insert({name: undefined})
user566245
  • 4,011
  • 1
  • 30
  • 36

1 Answers1

1

Unfortunately $beforeValidate is a synchronous operation.

You can check it on the Official Documentation. Even there is an issue about it.

Rashomon
  • 5,962
  • 4
  • 29
  • 67