1- import type { Infer , VineBoolean , VineEnum , VineNumber , VineString } from '@stacksjs/validation'
2- import type { SchemaTypes } from '@vinejs/vine/types'
1+ import type { BooleanValidatorType , EnumValidatorType , NumberValidatorType , StringValidatorType , ValidationType } from '@stacksjs/ts-validation'
32import type { EnvKey } from '../../../env'
43import { schema } from '@stacksjs/validation'
54import env from '~/config/env'
@@ -17,76 +16,79 @@ export const envEnum: EnumObject = {
1716}
1817
1918interface StringEnvConfig {
20- validation : VineString
19+ validation : StringValidatorType
2120 default : string
2221}
2322
2423interface NumberEnvConfig {
25- validation : VineNumber
24+ validation : NumberValidatorType
2625 default : number
2726}
2827
2928interface BooleanEnvConfig {
30- validation : VineBoolean
29+ validation : BooleanValidatorType
3130 default : boolean
3231}
3332
3433interface EnumEnvConfig {
35- validation : VineEnum < any >
34+ validation : EnumValidatorType < string >
3635 default : string
3736}
3837
3938type EnvValueConfig = StringEnvConfig | NumberEnvConfig | BooleanEnvConfig | EnumEnvConfig
4039
4140export type EnvConfig = Partial < Record < EnvKey , EnvValueConfig > >
4241
43- type EnvMap = Record < string , SchemaTypes >
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+ }
4457
4558const envStructure : EnvMap = Object . entries ( env ) . reduce ( ( acc , [ key , value ] ) => {
4659 if ( typeof value === 'object' && value !== null && 'validation' in value ) {
47- acc [ key ] = ( value as EnvValueConfig ) . validation
60+ acc [ key ] = ( value as EnvValueConfig ) . validation . name
4861 return acc
4962 }
5063
51- let validatorType : SchemaTypes
64+ let typeName : string
5265 switch ( typeof value ) {
5366 case 'string' :
54- validatorType = schema . string ( )
67+ typeName = ' string'
5568 break
5669 case 'number' :
57- validatorType = schema . number ( )
70+ typeName = ' number'
5871 break
5972 case 'boolean' :
60- validatorType = schema . boolean ( )
73+ typeName = ' boolean'
6174 break
6275 default :
6376 if ( Array . isArray ( value ) ) {
64- validatorType = schema . enum ( value as string [ ] )
77+ typeName = ' enum'
6578 break
6679 }
6780
6881 // check if is on object
6982 if ( typeof value === 'object' && value !== null ) {
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- }
83+ const schemaName = ( value as { name : string } ) . name
7784
78- if ( schemaName === 'vine.number' ) {
79- validatorType = schema . number ( )
80- break
81- }
82-
83- if ( schemaName === 'vine.boolean' ) {
84- validatorType = schema . boolean ( )
85+ if ( schemaName === 'string' || schemaName === 'number' || schemaName === 'boolean' ) {
86+ typeName = schemaName
8587 break
8688 }
8789
8890 if ( ! schemaName && key in envEnum ) {
89- validatorType = schema . enum ( envEnum [ key as keyof typeof envEnum ] )
91+ typeName = ' enum'
9092 break
9193 }
9294
@@ -96,14 +98,15 @@ const envStructure: EnvMap = Object.entries(env).reduce((acc, [key, value]) => {
9698 throw new Error ( `Invalid env value for ${ key } ` )
9799 }
98100
99- acc [ key ] = validatorType
101+ acc [ key ] = typeName
100102 return acc
101- } , { } as EnvMap )
103+ } , { } as Record < string , string > )
102104
103- export const envSchema : ReturnType < typeof schema . object > = schema . object ( envStructure )
104- export type Env = Infer < typeof envSchema >
105+ export type Env = {
106+ [ K in keyof typeof envStructure ] : typeof envStructure [ K ]
107+ }
105108
106- export type EnvOptions = Env
109+ export type EnvSchema = ReturnType < typeof schema . object < typeof envStructure > >
107110
108111export interface FrontendEnv {
109112 FRONTEND_APP_ENV : 'local' | 'development' | 'staging' | 'production'
0 commit comments