Skip to content

Commit b440d02

Browse files
committed
feat: add make:validator command
1 parent d88c6a5 commit b440d02

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

commands/Make/Validator.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* @adonisjs/assembler
3+
*
4+
* (c) Harminder Virk <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
import { join } from 'path'
11+
import { args } from '@adonisjs/ace'
12+
import { RcFile } from '@ioc:Adonis/Core/Application'
13+
import { BaseGenerator } from './Base'
14+
15+
/**
16+
* Command to make a new validator
17+
*/
18+
export default class MakeValidator extends BaseGenerator {
19+
/**
20+
* Required by BaseGenerator
21+
*/
22+
protected $suffix = ''
23+
protected $form = 'singular' as const
24+
protected $pattern = 'pascalcase' as const
25+
protected $resourceName: string
26+
27+
/**
28+
* Command meta data
29+
*/
30+
public static commandName = 'make:validator'
31+
public static description = 'Make a new validator'
32+
33+
@args.string({ description: 'Name of the validator' })
34+
public name: string
35+
36+
/**
37+
* Returns the template stub path
38+
*/
39+
protected $getStub (): string {
40+
return join(
41+
__dirname,
42+
'..',
43+
'..',
44+
'templates',
45+
'validator.txt',
46+
)
47+
}
48+
49+
/**
50+
* Pull path for the `validators` directory declaration from
51+
* the `.adonisrc.json` file or fallback to `app/Validators`
52+
*/
53+
protected $getDestinationPath (rcContents: RcFile): string {
54+
return this.$getPathForNamespace(rcContents, 'validators') || 'app/Validators'
55+
}
56+
57+
public async handle () {
58+
this.$resourceName = this.name
59+
await super.generate()
60+
}
61+
}

index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ new Manifest(__dirname).generate([
1616
'./commands/Make/Controller',
1717
'./commands/Make/Middleware',
1818
'./commands/Make/Provider',
19+
'./commands/Make/Validator',
1920
'./commands/Make/View',
2021
'./commands/Make/Model',
2122
])

templates/validator.txt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { schema, validator } from '@ioc:Adonis/Core/Validator'
2+
3+
class ${filename}Validator {
4+
/**
5+
* Using a pre-compiled schema you can validate the "shape", "type",
6+
* "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({ inTable: 'users', column: 'email' }),
21+
* ])
22+
* ```
23+
*/
24+
public schema = validator.compile(schema.create({
25+
}))
26+
27+
/**
28+
* Custom messages for validation failures. You can make use of dot notation `(.)`
29+
* for targeting nested fields and array expressions `(*)` for targeting all
30+
* children of an array. For example:
31+
*
32+
* {
33+
* 'profile.username.required': 'Username is required',
34+
* 'scores.*.number': 'Define scores as valid numbers'
35+
* }
36+
*/
37+
public messages = {}
38+
}
39+
40+
export default new ${filename}Validator()

0 commit comments

Comments
 (0)