-
Notifications
You must be signed in to change notification settings - Fork 17
/
CreateUserValidator.ts
57 lines (54 loc) · 1.72 KB
/
CreateUserValidator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { schema, rules } from '@ioc:Adonis/Core/Validator'
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
export default class CreateUserValidator {
constructor(protected ctx: HttpContextContract) {}
/*
* 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({
fullName: schema.string(),
email: schema.string({}, [
rules.email(),
rules.unique({
table: 'users',
column: 'email',
}),
]),
password: schema.string({}, [rules.minLength(6)]),
})
/**
* 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 = {
'fullName.required': 'Enter your full name',
'email.required': 'Enter your email',
'email.unique': 'Email is already in use',
'email.email': 'Invalid email address',
'password.required': 'Enter account password',
'password.minLength': 'Password too short',
}
}