Merge to support union schemas #299
-
Hey, I'm doing some validation on environment variables. Attempting something like the following: const DevSchema = object({
NODE_ENV: literal('production'),
DATABASE_URL: string([toTrimmed(), minLength(1), url(), toLowerCase(), excludes('?tls=0')])
});
const ProdSchema = object({
NODE_ENV: literal('development'),
DATABASE_URL: string([toTrimmed(), minLength(1), url(), toLowerCase(), endsWith('?tls=0')])
});
const EnvSchema = merge([
union([ DevSchema, ProdSchema ]),
object({
// shared config
FOO_BAR: string()
}),
]) Throws a lengthy TypeScript error that boils down to merge not supporting union schemas (and that's shown in the types right now.) I looked and while I found references of it being implemented in 0.20.0. I still unsure even what code to reference or how to build it. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
I ended up forcing const EnvSchema = merge([
object({ AUTH_SIGNED_COOKIE: string([toTrimmed(), minLength(1)]) }),
variant('NODE_ENV', [DevEnvSchema, ProdEnvSchema])
// union([DevEnvSchema, ProdEnvSchema]),
]); |
Beta Was this translation helpful? Give feedback.
-
Please try to use const EnvSchema = intersect([
union([DevSchema, ProdSchema]),
object({
// shared config
FOO_BAR: string()
}),
]); |
Beta Was this translation helpful? Give feedback.
Please try to use
intersect
for this use case.merge
does only work for pure objects because it is able to truly merge objects together. For example,merge
is able to overwrite individual entries.