1- import type { BooleanValidatorType , EnumValidatorType , NumberValidatorType , StringValidatorType , ValidationType } from '@stacksjs/ts-validation'
1+ import type { VineBoolean , VineEnum , VineNumber , VineString } from '@vinejs/vine'
2+ import type { SchemaTypes , Infer } from '@vinejs/vine/types'
23import type { EnvKey } from '../../../env'
3- import { schema } from '@stacksjs/validation '
4+ import schema from '@vinejs/vine '
45import env from '~/config/env'
56
67interface EnumObject {
@@ -16,79 +17,76 @@ export const envEnum: EnumObject = {
1617}
1718
1819interface StringEnvConfig {
19- validation : StringValidatorType
20+ validation : VineString
2021 default : string
2122}
2223
2324interface NumberEnvConfig {
24- validation : NumberValidatorType
25+ validation : VineNumber
2526 default : number
2627}
2728
2829interface BooleanEnvConfig {
29- validation : BooleanValidatorType
30+ validation : VineBoolean
3031 default : boolean
3132}
3233
3334interface EnumEnvConfig {
34- validation : EnumValidatorType < string >
35+ validation : VineEnum < any >
3536 default : string
3637}
3738
3839type EnvValueConfig = StringEnvConfig | NumberEnvConfig | BooleanEnvConfig | EnumEnvConfig
3940
4041export type EnvConfig = Partial < Record < EnvKey , EnvValueConfig > >
4142
42- type EnvMap = Record < string , string >
43-
44- type TypeFromString < T extends string > = T extends 'string'
45- ? string
46- : T extends 'number'
47- ? number
48- : T extends 'boolean'
49- ? boolean
50- : T extends 'enum'
51- ? string
52- : never
53-
54- type Infer < T extends Record < string , string > > = {
55- [ K in keyof T ] : TypeFromString < T [ K ] >
56- }
43+ type EnvMap = Record < string , SchemaTypes >
5744
5845const envStructure : EnvMap = Object . entries ( env ) . reduce ( ( acc , [ key , value ] ) => {
5946 if ( typeof value === 'object' && value !== null && 'validation' in value ) {
60- acc [ key ] = ( value as EnvValueConfig ) . validation . name
47+ acc [ key ] = ( value as EnvValueConfig ) . validation
6148 return acc
6249 }
6350
64- let typeName : string
51+ let validatorType : SchemaTypes
6552 switch ( typeof value ) {
6653 case 'string' :
67- typeName = ' string'
54+ validatorType = schema . string ( )
6855 break
6956 case 'number' :
70- typeName = ' number'
57+ validatorType = schema . number ( )
7158 break
7259 case 'boolean' :
73- typeName = ' boolean'
60+ validatorType = schema . boolean ( )
7461 break
7562 default :
7663 if ( Array . isArray ( value ) ) {
77- typeName = ' enum'
64+ validatorType = schema . enum ( value as string [ ] )
7865 break
7966 }
8067
8168 // check if is on object
8269 if ( typeof value === 'object' && value !== null ) {
83- const schemaName = ( value as { name : string } ) . name
70+ const schemaNameSymbol = Symbol . for ( 'schema_name' )
71+ const schemaName = ( value as { [ key : symbol ] : string } ) [ schemaNameSymbol ]
72+
73+ if ( schemaName === 'vine.string' ) {
74+ validatorType = schema . string ( )
75+ break
76+ }
8477
85- if ( schemaName === 'string' || schemaName === 'number' || schemaName === 'boolean' ) {
86- typeName = schemaName
78+ if ( schemaName === 'vine.number' ) {
79+ validatorType = schema . number ( )
80+ break
81+ }
82+
83+ if ( schemaName === 'vine.boolean' ) {
84+ validatorType = schema . boolean ( )
8785 break
8886 }
8987
9088 if ( ! schemaName && key in envEnum ) {
91- typeName = ' enum'
89+ validatorType = schema . enum ( envEnum [ key as keyof typeof envEnum ] )
9290 break
9391 }
9492
@@ -98,15 +96,14 @@ const envStructure: EnvMap = Object.entries(env).reduce((acc, [key, value]) => {
9896 throw new Error ( `Invalid env value for ${ key } ` )
9997 }
10098
101- acc [ key ] = typeName
99+ acc [ key ] = validatorType
102100 return acc
103- } , { } as Record < string , string > )
101+ } , { } as EnvMap )
104102
105- export type Env = {
106- [ K in keyof typeof envStructure ] : typeof envStructure [ K ]
107- }
103+ export const envSchema : ReturnType < typeof schema . object > = schema . object ( envStructure )
104+ export type Env = Infer < typeof envSchema >
108105
109- export type EnvSchema = ReturnType < typeof schema . object < typeof envStructure > >
106+ export type EnvOptions = Env
110107
111108export interface FrontendEnv {
112109 FRONTEND_APP_ENV : 'local' | 'development' | 'staging' | 'production'
0 commit comments