UserRegisterValidator.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { schema, CustomMessages, rules } from '@ioc:Adonis/Core/Validator'
  2. import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
  3. export default class UserRegisterValidator {
  4. constructor(protected ctx: HttpContextContract) {}
  5. /*
  6. * Define schema to validate the "shape", "type", "formatting" and "integrity" of data.
  7. *
  8. * For example:
  9. * 1. The username must be of data type string. But then also, it should
  10. * not contain special characters or numbers.
  11. * ```
  12. * schema.string([ rules.alpha() ])
  13. * ```
  14. *
  15. * 2. The email must be of data type string, formatted as a valid
  16. * email. But also, not used by any other user.
  17. * ```
  18. * schema.string([
  19. * rules.email(),
  20. * rules.unique({ table: 'users', column: 'email' }),
  21. * ])
  22. * ```
  23. */
  24. public schema = schema.create({
  25. username: schema.string([rules.minLength(6)]),
  26. password: schema.string([rules.minLength(6)])
  27. })
  28. /**
  29. * Custom messages for validation failures. You can make use of dot notation `(.)`
  30. * for targeting nested fields and array expressions `(*)` for targeting all
  31. * children of an array. For example:
  32. *
  33. * {
  34. * 'profile.username.required': 'Username is required',
  35. * 'scores.*.number': 'Define scores as valid numbers'
  36. * }
  37. *
  38. */
  39. public messages: CustomMessages = {}
  40. }