Files
duty-schedule-api/app/Validators/UpdateEmployeeValidator.ts

87 lines
1.9 KiB
TypeScript

import { schema, rules } from '@ioc:Adonis/Core/Validator'
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
export default class UpdateEmployeeValidator {
id : number
constructor (protected ctx: HttpContextContract) {
this.id = ctx.params.id
}
/*
* Define schema to validate the "shape", "type", "formatting" and "integrity" of data.
*
* For example:
* 1. The username must be of data type string. But then also, it should
* not contain special characters or numbers.
* ```
* schema.string({}, [ rules.alpha() ])
* ```
*
* 2. The email must be of data type string, formatted as a valid
* email. But also, not used by any other user.
* ```
* schema.string({}, [
* rules.email(),
* rules.unique({ table: 'users', column: 'email' }),
* ])
* ```
*/
public schema = schema.create({
firstName: schema.string({
trim: true
}),
lastName: schema.string.optional({
trim: true
}),
shorthand: schema.string({
trim: true
},
[
rules.unique({
table: 'employees',
column: 'shorthand',
whereNot: {
id: this.ctx.params.id
}
})
]),
email: schema.string.optional({
trim: true
},
[
rules.email({
sanitize: true
})
]),
phone: schema.string.optional(),
mobile: schema.string.optional(),
contractHours: schema.number.optional()
})
/**
* Custom messages for validation failures. You can make use of dot notation `(.)`
* for targeting nested fields and array expressions `(*)` for targeting all
* children of an array. For example:
*
* {
* 'profile.username.required': 'Username is required',
* 'scores.*.number': 'Define scores as valid numbers'
* }
*
*/
public messages = {
'shorthand.unique': 'Shorthand has to be unique.',
required: '{{ field }} is required.'
}
}