This commit is contained in:
2025-08-14 17:31:04 +02:00
parent 5fc177a1e7
commit 95be9d8d4a
4 changed files with 42 additions and 73 deletions
+8 -8
View File
@@ -1,14 +1,13 @@
import {type Static, type TObject, type TProperties} from '@sinclair/typebox'
import {Value} from '@sinclair/typebox/value'
import {z, type ZodObject, type ZodRawShape} from 'zod'
import logger from 'log'
import {read_object} from './src/read_type'
const log = logger('config')
export {Type, type Static, type StaticDecode} from '@sinclair/typebox' // Re-export Type so users can describe config
export {Value} from '@sinclair/typebox/value'
// TODO : export {Type, type Static, type StaticDecode} from '@sinclair/typebox' // Re-export Type so users can describe config
// TODO : export {Value} from '@sinclair/typebox/value'
export async function parse<T extends TProperties>(scheme: TObject<T>): Promise<Static<TObject<T>> | null> {
export async function parse<T extends ZodRawShape>(scheme: ZodObject<T>): Promise<z.infer<ZodObject<T>> | null> {
log.info("Parse configuration from env")
log.debug("Read configuration from env")
@@ -17,12 +16,13 @@ export async function parse<T extends TProperties>(scheme: TObject<T>): Promise<
// This check is kind of a duplicate, is it useful ?
log.debug("Validate config against scheme")
const config_parsed = Value.Check(scheme, config.data)
if (config_parsed) {
const config_parsed = await scheme.safeParseAsync(config.data)
if (config_parsed.success) {
log.trace("Config is valid")
return config.data
return config_parsed.data
} else {
log.warn("Invalid config, check failed")
log.debug("Error :", config_parsed.error)
return null
}
}