A
arktype2w ago
cusx

How do I get the input type for `type`?

export function validateEnv<T extends Record<string, unknown>>(
def: ?,
) {
const schema = type(def)
const envVars = Object.fromEntries(
Object.keys(schemaDefinition).map((key) => [key, process.env[key]]),
)
const result = schema(envVars)

if (result instanceof type.errors) {
throw new TypeError(result.summary)
}

return result
}

validateEnv({
DATABASE_URL: 'string'
})
export function validateEnv<T extends Record<string, unknown>>(
def: ?,
) {
const schema = type(def)
const envVars = Object.fromEntries(
Object.keys(schemaDefinition).map((key) => [key, process.env[key]]),
)
const result = schema(envVars)

if (result instanceof type.errors) {
throw new TypeError(result.summary)
}

return result
}

validateEnv({
DATABASE_URL: 'string'
})
I want to create a function to validate the process.env, but I'm having trouble typing the input def into arktype.
3 Replies
ssalbdivad
ssalbdivad2w ago
Take a look at the second example here for accepting a definition in your generic API: https://arktype.io/docs/generics#external
ArkType
ArkType Docs
TypeScript's 1:1 validator, optimized from editor to runtime
kunet
kunet3d ago
GitHub
mygourd/lib/utils/env.ts at 038daceee1fa468c7420d53471f355989eb3544...
discord utility functions. Contribute to imkunet/mygourd development by creating an account on GitHub.
ssalbdivad
ssalbdivad3d ago
Yeah could be a nice utility package for sure!

Did you find this page helpful?