29 lines
1.0 KiB
TypeScript
29 lines
1.0 KiB
TypeScript
import {z, type ZodObject, type ZodRawShape} from 'zod'
|
|
import logger from 'log'
|
|
|
|
import {read_object} from './src/read_type'
|
|
|
|
const log = logger('config')
|
|
// 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 ZodRawShape>(scheme: ZodObject<T>): Promise<z.infer<ZodObject<T>> | null> {
|
|
log.info("Parse configuration from env")
|
|
|
|
log.debug("Read configuration from env")
|
|
const config = await read_object(scheme)
|
|
if (!config.ok) return null
|
|
|
|
// This check is kind of a duplicate, is it useful ?
|
|
log.debug("Validate config against scheme")
|
|
const config_parsed = await scheme.safeParseAsync(config.data)
|
|
if (config_parsed.success) {
|
|
log.trace("Config is valid")
|
|
return config_parsed.data
|
|
} else {
|
|
log.warn("Invalid config, check failed")
|
|
log.debug("Error :", config_parsed.error)
|
|
return null
|
|
}
|
|
}
|