Skip to content

Commit

Permalink
feat: add make:validator command
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jul 20, 2023
1 parent 5070281 commit 693f601
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
33 changes: 33 additions & 0 deletions commands/make/validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* @adonisjs/core
*
* (c) AdonisJS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import BaseCommand from './_base.js'
import { args } from '../../modules/ace/main.js'

/**
* Make a new VineJS validator
*/
export default class MakeValidator extends BaseCommand {
static commandName = 'make:validator'
static description = 'Create a new VineJS validator'

@args.string({ description: 'Name of the validator' })
declare name: string

/**
* The stub to use for generating the validator
*/
protected stubPath: string = 'make/validator/main.stub'

async run() {
await this.generate(this.stubPath, {
entity: this.app.generators.createEntity(this.name),
})
}
}
5 changes: 5 additions & 0 deletions stubs/make/validator/main.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{{#var validatorFileName = generators.validatorFileName(entity.name)}}
---
to: {{ app.validatorsPath(entity.path, validatorFileName) }}
---
import vine from '@vinejs/vine'
39 changes: 39 additions & 0 deletions tests/commands/make_validator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* @adonisjs/core
*
* (c) AdonisJS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import { test } from '@japa/runner'
import { AceFactory } from '../../factories/core/ace.js'
import { StubsFactory } from '../../factories/stubs.js'
import MakeValidator from '../../commands/make/validator.js'

test.group('Make validator', () => {
test('create validator file', async ({ assert, fs }) => {
const ace = await new AceFactory().make(fs.baseUrl, {
importer: (filePath) => import(filePath),
})
await ace.app.init()
ace.ui.switchMode('raw')

const command = await ace.create(MakeValidator, ['invoice'])
await command.exec()

const { contents } = await new StubsFactory().prepare('make/validator/main.stub', {
entity: ace.app.generators.createEntity('invoice'),
})

await assert.fileEquals('app/validators/invoice_validator.ts', contents)

assert.deepEqual(ace.ui.logger.getLogs(), [
{
message: 'green(DONE:) create app/validators/invoice_validator.ts',
stream: 'stdout',
},
])
})
})
35 changes: 35 additions & 0 deletions tests/stubs/make_validator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* @adonisjs/core
*
* (c) AdonisJS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import { join } from 'node:path'
import { test } from '@japa/runner'
import { fileURLToPath } from 'node:url'
import { AppFactory } from '@adonisjs/application/factories'

import { stubsRoot } from '../../stubs/index.js'

const BASE_URL = new URL('./tmp/', import.meta.url)
const BASE_PATH = fileURLToPath(BASE_URL)

test.group('Make validator', () => {
test('prepare validator stub', async ({ assert }) => {
const app = new AppFactory().create(BASE_URL, () => {})
await app.init()

const stub = await app.stubs.build('make/validator/main.stub', {
source: stubsRoot,
})
const { contents, destination } = await stub.prepare({
entity: app.generators.createEntity('posts'),
})

assert.equal(destination, join(BASE_PATH, 'app/validators/post_validator.ts'))
assert.match(contents, new RegExp("import vine from '@vinejs/vine'"))
})
})

0 comments on commit 693f601

Please sign in to comment.