Closed liaosankai closed 2 years ago
Hey 👋
Thanks for the PR :). I think this should be its own rule alphaNum
It mean in the future there will be new schema types alphaNum
in offical reference or I should use custom validation rules like this ?
node ace make:prldfile validator
// file: start/validator.ts
import { validator } from '@ioc:Adonis/Core/Validator'
const RULE_NAME = 'alphaNum'
const DEFAULT_MESSAGE = 'alphaNum validation failed'
validator.rule(RULE_NAME, (value, [ruleOptions], options) => {
let charactersMatch = 'a-zA-Z0-9'
if (ruleOptions['allow'].includes('space')) {
charactersMatch += '\\s'
}
if (ruleOptions['allow'].includes('space')) {
charactersMatch += '-'
}
if (ruleOptions['allow'].includes('underscore')) {
charactersMatch += '_'
}
const pattern = `^[${charactersMatch}]+$`
if (typeof value !== 'string') {
return
}
if (!new RegExp(pattern).test(value)) {
options.errorReporter.report(
options.pointer,
RULE_NAME,
DEFAULT_MESSAGE,
options.arrayExpressionPointer
)
}
})
// file: contracts/validator.ts
declare module '@ioc:Adonis/Core/Validator' {
interface Rules {
alphaNum(options?: { allow?: ('space' | 'underscore' | 'dash')[] }): Rule
}
}
Just update the PR to have a alphaNum
validation rule. The goal is to be it separate from the alpha
rule, coz alpha-numeric is its own thing :)
@thetutlage Updated. :)
Hey sorry for the delay, was busy lately and now getting back to reviewing PRs :)
Thanks for the contribution 👍 . Really appreciate it
Can you please also open a PR for the documentation to document this rule? https://github.com/adonisjs/docs.adonisjs.com/
Proposed changes
add
numeric
allow for alpha ruleTypes of changes
Checklist