63 lines
1.9 KiB
TypeScript
63 lines
1.9 KiB
TypeScript
import { schema, rules, validator } from '@ioc:Adonis/Core/Validator'
|
|
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
|
|
|
export default class SetSettingsValidator {
|
|
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({
|
|
settings: schema.array().members(
|
|
schema.object().members({
|
|
key: schema.string({
|
|
trim: true,
|
|
}, [
|
|
rules.alpha({
|
|
allow: ['dash', 'underscore']
|
|
})
|
|
]),
|
|
|
|
value: schema.string({
|
|
trim: true
|
|
}),
|
|
})
|
|
),
|
|
})
|
|
|
|
/**
|
|
* 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 = {
|
|
'settings.required': 'Settings are required',
|
|
'settings.key.required': 'Key is required',
|
|
'settings.key.alpha': 'Key must be alphabetic',
|
|
'settings.value.required': 'Value is required',
|
|
'settings.value.alpha': 'Value must be alphabetic',
|
|
}
|
|
}
|