Skip to content

Commit c6de809

Browse files
committed
chore: wip
1 parent 1280a81 commit c6de809

File tree

4 files changed

+57
-7
lines changed

4 files changed

+57
-7
lines changed

app/Actions/EmailSubscribeAction.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,45 @@
11
import { Action } from '@stacksjs/actions'
2-
import { request } from '@stacksjs/router'
3-
import SubscriberEmail from '../../storage/framework/orm/src/models/SubscriberEmail'
2+
// import { request } from '@stacksjs/router'
3+
// import { epmailSubscribeRequest } from '@stacksjs/validation'
44
import { validateField } from '@stacksjs/validation'
5+
import SubscriberEmail from '../../storage/framework/orm/src/models/SubscriberEmail'
56

7+
export interface EmailSubscribeRequest extends Request {
8+
email: string
9+
}
610

711
export default new Action({
812
name: 'EmailSubscribeAction',
913
description: 'Save emails from subscribe page',
1014
method: 'POST',
11-
async handle() {
15+
// validations: {
16+
// email: {
17+
// rule: schema.string().email(),
18+
// message: {
19+
// email: 'Email must be valid',
20+
// required: 'Email is required',
21+
// }
22+
// },
23+
// },
24+
// async handle(request) {
25+
async handle(request: EmailSubscribeRequest) {
1226
// We pass the model name cause how else do we know which fields to validate
27+
request.validate({
28+
email: {
29+
rule: schema.string().email(),
30+
message: {
31+
email: 'Email must be valid',
32+
required: 'Email is required',
33+
},
34+
},
35+
})
1336
await validateField('SubscriberEmail', request.all())
37+
// we know the request is validated
1438

1539
const email = request.get('email')
1640

1741
const model = await SubscriberEmail.create({ email })
1842

1943
return model
2044
},
21-
})
45+
})

app/Models/SubscriberEmail.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ export default {
3535
factory: () => faker.internet.email(),
3636
},
3737
},
38-
38+
3939
} satisfies Model

storage/framework/core/router/src/router.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ export class Router implements RouterInterface {
228228
public async getRoutes(): Promise<Route[]> {
229229
await import(routesPath('api.ts'))
230230
await import(projectStoragePath('framework/orm/routes.ts'))
231-
231+
232232
return this.routes
233233
}
234234

@@ -296,7 +296,18 @@ export class Router implements RouterInterface {
296296
const newPath = actionModule.default.path ?? originalPath
297297
this.updatePathIfNeeded(newPath, originalPath)
298298

299-
return await actionModule.default.handle()
299+
// we need to make sure the validation happens here
300+
// to do so, we need to:
301+
// find the ./app/Models/* file
302+
// then check via a regex which model attributes validations to utilize by checking what's in between t
303+
// then validate
304+
// if succeeds, run the handle
305+
// if fails, return validation error
306+
307+
if (condition)
308+
return await actionModule.default.handle()
309+
310+
return await actionModule.default.handle(request)
300311
}
301312

302313
private normalizePath(path: string): string {

storage/framework/types/auto-imports.d.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ declare global {
7070
const Schedule: typeof import('../core/scheduler/src/schedule')['Schedule']
7171
const ScheduleImpl: typeof import('../core/scheduler/src/index')['ScheduleImpl']
7272
const Schema: typeof import('../core/database/src/schema')['Schema']
73+
const SimpleMessagesProvider: typeof import('../core/validation/src/schema')['SimpleMessagesProvider']
7374
const SocialLinkIcon: typeof import('../core/types/src/docs')['SocialLinkIcon']
7475
const StacksError: typeof import('../core/error-handling/src/handler')['StacksError']
7576
const Store: typeof import('../core/ui/src/index')['Store']
@@ -93,6 +94,7 @@ declare global {
9394
const VineBoolean: typeof import('../core/validation/src/schema')['VineBoolean']
9495
const VineDate: typeof import('../core/validation/src/schema')['VineDate']
9596
const VineEnum: typeof import('../core/validation/src/schema')['VineEnum']
97+
const VineError: typeof import('../core/validation/src/schema')['VineError']
9698
const VineNumber: typeof import('../core/validation/src/schema')['VineNumber']
9799
const VineString: typeof import('../core/validation/src/schema')['VineString']
98100
const _dirname: typeof import('../core/storage/src/helpers')['_dirname']
@@ -382,6 +384,7 @@ declare global {
382384
const getCurrentMigrationFields: typeof import('../core/database/src/migrations')['getCurrentMigrationFields']
383385
const getCurrentScope: typeof import('vue')['getCurrentScope']
384386
const getDialect: typeof import('../core/database/src/utils')['getDialect']
387+
const getErrors: typeof import('../core/validation/src/reporter')['getErrors']
385388
const getExecutedMigrations: typeof import('../core/database/src/migrations')['getExecutedMigrations']
386389
const getExportsSize: typeof import('../core/utils/src/export-size')['getExportsSize']
387390
const getFiles: typeof import('../core/storage/src/files')['getFiles']
@@ -659,6 +662,7 @@ declare global {
659662
const remove: typeof import('../core/orm/src/generated/User')['remove']
660663
const renderHeadToString: typeof import('../core/utils/src/vendors')['renderHeadToString']
661664
const replPath: typeof import('../core/path/src/paths')['replPath']
665+
const reportError: typeof import('../core/validation/src/reporter')['reportError']
662666
const request: typeof import('../core/router/src/request')['request']
663667
const resetDatabase: typeof import('../core/database/src/migrations')['resetDatabase']
664668
const resolve: typeof import('../core/path/src/paths')['resolve']
@@ -1008,6 +1012,7 @@ declare global {
10081012
const userViewsPath: typeof import('../core/path/src/paths')['userViewsPath']
10091013
const utilsPath: typeof import('../core/path/src/paths')['utilsPath']
10101014
const validate: typeof import('../core/validation/src/schema')['validate']
1015+
const validateField: typeof import('../core/validation/src/validator')['validateField']
10111016
const validationPath: typeof import('../core/path/src/paths')['validationPath']
10121017
const validator: typeof import('../core/validation/src/validate')['validator']
10131018
const verifyHash: typeof import('../core/security/src/hash')['verifyHash']
@@ -1099,6 +1104,7 @@ declare module 'vue' {
10991104
readonly Router: UnwrapRef<typeof import('../core/router/src/router')['Router']>
11001105
readonly Schedule: UnwrapRef<typeof import('../core/scheduler/src/schedule')['Schedule']>
11011106
readonly Schema: UnwrapRef<typeof import('../core/database/src/schema')['Schema']>
1107+
readonly SimpleMessagesProvider: UnwrapRef<typeof import('../core/validation/src/schema')['SimpleMessagesProvider']>
11021108
readonly SocialLinkIcon: UnwrapRef<typeof import('../core/types/src/docs')['SocialLinkIcon']>
11031109
readonly StacksError: UnwrapRef<typeof import('../core/error-handling/src/handler')['StacksError']>
11041110
readonly Store: UnwrapRef<typeof import('../core/ui/src/index')['Store']>
@@ -1112,6 +1118,7 @@ declare module 'vue' {
11121118
readonly VineBoolean: UnwrapRef<typeof import('../core/validation/src/schema')['VineBoolean']>
11131119
readonly VineDate: UnwrapRef<typeof import('../core/validation/src/schema')['VineDate']>
11141120
readonly VineEnum: UnwrapRef<typeof import('../core/validation/src/schema')['VineEnum']>
1121+
readonly VineError: UnwrapRef<typeof import('../core/validation/src/schema')['VineError']>
11151122
readonly VineNumber: UnwrapRef<typeof import('../core/validation/src/schema')['VineNumber']>
11161123
readonly VineString: UnwrapRef<typeof import('../core/validation/src/schema')['VineString']>
11171124
readonly _dirname: UnwrapRef<typeof import('../core/storage/src/helpers')['_dirname']>
@@ -1377,6 +1384,7 @@ declare module 'vue' {
13771384
readonly getCurrentMigrationFields: UnwrapRef<typeof import('../core/database/src/migrations')['getCurrentMigrationFields']>
13781385
readonly getCurrentScope: UnwrapRef<typeof import('vue')['getCurrentScope']>
13791386
readonly getDialect: UnwrapRef<typeof import('../core/database/src/utils')['getDialect']>
1387+
readonly getErrors: UnwrapRef<typeof import('../core/validation/src/reporter')['getErrors']>
13801388
readonly getExecutedMigrations: UnwrapRef<typeof import('../core/database/src/migrations')['getExecutedMigrations']>
13811389
readonly getExportsSize: UnwrapRef<typeof import('../core/utils/src/export-size')['getExportsSize']>
13821390
readonly getFiles: UnwrapRef<typeof import('../core/storage/src/files')['getFiles']>
@@ -1643,6 +1651,7 @@ declare module 'vue' {
16431651
readonly relativeActionsPath: UnwrapRef<typeof import('../core/path/src/paths')['relativeActionsPath']>
16441652
readonly renderHeadToString: UnwrapRef<typeof import('../core/utils/src/vendors')['renderHeadToString']>
16451653
readonly replPath: UnwrapRef<typeof import('../core/path/src/paths')['replPath']>
1654+
readonly reportError: UnwrapRef<typeof import('../core/validation/src/reporter')['reportError']>
16461655
readonly request: UnwrapRef<typeof import('../core/router/src/request')['request']>
16471656
readonly resetDatabase: UnwrapRef<typeof import('../core/database/src/migrations')['resetDatabase']>
16481657
readonly resolve: UnwrapRef<typeof import('../core/path/src/paths')['resolve']>
@@ -1972,6 +1981,7 @@ declare module 'vue' {
19721981
readonly userViewsPath: UnwrapRef<typeof import('../core/path/src/paths')['userViewsPath']>
19731982
readonly utilsPath: UnwrapRef<typeof import('../core/path/src/paths')['utilsPath']>
19741983
readonly validate: UnwrapRef<typeof import('../core/validation/src/schema')['validate']>
1984+
readonly validateField: UnwrapRef<typeof import('../core/validation/src/validator')['validateField']>
19751985
readonly validationPath: UnwrapRef<typeof import('../core/path/src/paths')['validationPath']>
19761986
readonly verifyHash: UnwrapRef<typeof import('../core/security/src/hash')['verifyHash']>
19771987
readonly version: UnwrapRef<typeof import('../core/utils/src/versions')['version']>
@@ -2046,6 +2056,7 @@ declare module '@vue/runtime-core' {
20462056
readonly Router: UnwrapRef<typeof import('../core/router/src/router')['Router']>
20472057
readonly Schedule: UnwrapRef<typeof import('../core/scheduler/src/schedule')['Schedule']>
20482058
readonly Schema: UnwrapRef<typeof import('../core/database/src/schema')['Schema']>
2059+
readonly SimpleMessagesProvider: UnwrapRef<typeof import('../core/validation/src/schema')['SimpleMessagesProvider']>
20492060
readonly SocialLinkIcon: UnwrapRef<typeof import('../core/types/src/docs')['SocialLinkIcon']>
20502061
readonly StacksError: UnwrapRef<typeof import('../core/error-handling/src/handler')['StacksError']>
20512062
readonly Store: UnwrapRef<typeof import('../core/ui/src/index')['Store']>
@@ -2059,6 +2070,7 @@ declare module '@vue/runtime-core' {
20592070
readonly VineBoolean: UnwrapRef<typeof import('../core/validation/src/schema')['VineBoolean']>
20602071
readonly VineDate: UnwrapRef<typeof import('../core/validation/src/schema')['VineDate']>
20612072
readonly VineEnum: UnwrapRef<typeof import('../core/validation/src/schema')['VineEnum']>
2073+
readonly VineError: UnwrapRef<typeof import('../core/validation/src/schema')['VineError']>
20622074
readonly VineNumber: UnwrapRef<typeof import('../core/validation/src/schema')['VineNumber']>
20632075
readonly VineString: UnwrapRef<typeof import('../core/validation/src/schema')['VineString']>
20642076
readonly _dirname: UnwrapRef<typeof import('../core/storage/src/helpers')['_dirname']>
@@ -2324,6 +2336,7 @@ declare module '@vue/runtime-core' {
23242336
readonly getCurrentMigrationFields: UnwrapRef<typeof import('../core/database/src/migrations')['getCurrentMigrationFields']>
23252337
readonly getCurrentScope: UnwrapRef<typeof import('vue')['getCurrentScope']>
23262338
readonly getDialect: UnwrapRef<typeof import('../core/database/src/utils')['getDialect']>
2339+
readonly getErrors: UnwrapRef<typeof import('../core/validation/src/reporter')['getErrors']>
23272340
readonly getExecutedMigrations: UnwrapRef<typeof import('../core/database/src/migrations')['getExecutedMigrations']>
23282341
readonly getExportsSize: UnwrapRef<typeof import('../core/utils/src/export-size')['getExportsSize']>
23292342
readonly getFiles: UnwrapRef<typeof import('../core/storage/src/files')['getFiles']>
@@ -2590,6 +2603,7 @@ declare module '@vue/runtime-core' {
25902603
readonly relativeActionsPath: UnwrapRef<typeof import('../core/path/src/paths')['relativeActionsPath']>
25912604
readonly renderHeadToString: UnwrapRef<typeof import('../core/utils/src/vendors')['renderHeadToString']>
25922605
readonly replPath: UnwrapRef<typeof import('../core/path/src/paths')['replPath']>
2606+
readonly reportError: UnwrapRef<typeof import('../core/validation/src/reporter')['reportError']>
25932607
readonly request: UnwrapRef<typeof import('../core/router/src/request')['request']>
25942608
readonly resetDatabase: UnwrapRef<typeof import('../core/database/src/migrations')['resetDatabase']>
25952609
readonly resolve: UnwrapRef<typeof import('../core/path/src/paths')['resolve']>
@@ -2919,6 +2933,7 @@ declare module '@vue/runtime-core' {
29192933
readonly userViewsPath: UnwrapRef<typeof import('../core/path/src/paths')['userViewsPath']>
29202934
readonly utilsPath: UnwrapRef<typeof import('../core/path/src/paths')['utilsPath']>
29212935
readonly validate: UnwrapRef<typeof import('../core/validation/src/schema')['validate']>
2936+
readonly validateField: UnwrapRef<typeof import('../core/validation/src/validator')['validateField']>
29222937
readonly validationPath: UnwrapRef<typeof import('../core/path/src/paths')['validationPath']>
29232938
readonly verifyHash: UnwrapRef<typeof import('../core/security/src/hash')['verifyHash']>
29242939
readonly version: UnwrapRef<typeof import('../core/utils/src/versions')['version']>

0 commit comments

Comments
 (0)