You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello! We're exploring migrating our zod based DTOs to typia/nestia, but while doing a bit of exploring I ran into the following case which I couldn't quite solve yet.
We have an input model that requires a chainId to be passed. chainId is a number, but only valid numbers from an environment variable are acceptable. We currently handling that like so and it works perfect!
type ChainId = tags.TagBase<{
kind: 'chainId';
target: 'number';
value: undefined;
validate: `String(process.env.SUPPORTED_IDS).split(',').includes(String($input))`;
}>;
export type AddChainToGameRequestInput = {
chainId: number & tags.Default<13337> & ChainId;
};
Question
What I couldn't figure out however, is how to pass a default value to the chainId that also comes from a runtime environment, for example: an environment variable. The reason we would want this, is because process.env.SUPPORTED_IDS will be completely different per environment we run our application.
In the snippet above, you can see that we simply pass a hardcoded 13337 number now:
export type AddChainToGameRequestInput = {
chainId: number & tags.Default<13337> & ChainId;
};
Is it somehow possible for typia to process default values based on a runtime variable? or is that outside of the scope for this project? Sorry if it has been asked before, but I couldn't quite find an issue related.
To clarify, we would want something like this, which currently (for obvious reasons) wouldn't quite work
chainId: number & tags.Default<process.env.DEFAULT_CHAIN_ID> & ChainId;
I could technically see it work in similar fashion to how tags.TagBase handles validate, so something like a default option to call during runtime, of which the return value gets used as the default value.
Something like:
type Model = {
chainId: number & tags.Default<{
dynamic: `process.env.DEFAULT_CHAIN_ID`;
}>;
}
Let me know if you like this idea, I could give it a swing at developing a PR.
The text was updated successfully, but these errors were encountered:
Would there be another way to get a runtime variable to be a default value to a property in a type? We want it to be coming from the runtime, as the default value is different depending on the environment.
With zod, we do it like so:
const chainIdSchema = z
.number()
.default(Number(process.env.CHAIN_ID));
Is it something you would consider a good addition? Due to the runtime validation aspect of Typia it does feel logically to me to also have runtime default values instead of just static ones, but it might just be me or just out of scope
Hello! We're exploring migrating our zod based DTOs to typia/nestia, but while doing a bit of exploring I ran into the following case which I couldn't quite solve yet.
We have an input model that requires a
chainId
to be passed.chainId
is a number, but only valid numbers from an environment variable are acceptable. We currently handling that like so and it works perfect!Question
What I couldn't figure out however, is how to pass a default value to the
chainId
that also comes from a runtime environment, for example: an environment variable. The reason we would want this, is becauseprocess.env.SUPPORTED_IDS
will be completely different per environment we run our application.In the snippet above, you can see that we simply pass a hardcoded
13337
number now:Is it somehow possible for typia to process default values based on a runtime variable? or is that outside of the scope for this project? Sorry if it has been asked before, but I couldn't quite find an issue related.
To clarify, we would want something like this, which currently (for obvious reasons) wouldn't quite work
I could technically see it work in similar fashion to how
tags.TagBase
handlesvalidate
, so something like adefault
option to call during runtime, of which the return value gets used as the default value.Something like:
Let me know if you like this idea, I could give it a swing at developing a PR.
The text was updated successfully, but these errors were encountered: