-
-
Notifications
You must be signed in to change notification settings - Fork 622
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5070281
commit 693f601
Showing
4 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'")) | ||
}) | ||
}) |