@sapphire/shapeshift validate env library

How would I go about making a library which validates the process.env similar to that of znv but using @sapphire/shapeshift. Was thinking of doing that so I keep my libraries within sapphire if possible
19 Replies
Favna
Favna3y ago
process.env is an object and shapeshift has s.object so from there you define the required keys.
.
.OP3y ago
Thank you, also is there a helper type to infer the actual object types from the schema
Favna
Favna3y ago
GitHub
GitHub - sapphiredev/shapeshift: Blazing fast input validation and ...
Blazing fast input validation and transformation ⚡ - GitHub - sapphiredev/shapeshift: Blazing fast input validation and transformation ⚡
.
.OP3y ago
Also is there any way to do coercion?
Favna
Favna3y ago
Should be transform and reshape (again, see readme, it’s very detailed)
.
.OP3y ago
the user of the library would do
parseEnv(process.env, {
DISCORD_TOKEN: s.string,
OWNERS: s.array(s.string);
})
parseEnv(process.env, {
DISCORD_TOKEN: s.string,
OWNERS: s.array(s.string);
})
how to tell what type of validator it is?
Favna
Favna3y ago
wdym "type of validator"? I dont even know what you're trying to achieve tbh also process.env vars are always strings, not numbers, not arrays. always strings. If you want to support arrays you need to parse them yourself beforehand. For example @Skyra/env-utilities does that by splitting them on space and calling Number for integers
.
.OP3y ago
I'm aware, the library would parse the string depending on what type of validator if process.env is { PROCESS_ID: "1" } and the schema passed is { PROCESS_ID: s.number.int }. How would the library know what type of validator it is so that the parsing can be done so that the env value can match the validator All parseEnv does is parses env strings to the type passed (s.string, s.number, etc) and if it cannot be successfully parsed throws an error
Favna
Favna3y ago
you need to parse it before validation
.
.OP3y ago
I know but is there any way to get the type of validator to determine how to parse the string
Favna
Favna3y ago
there isnt
.
.OP3y ago
so if a function could be passed either s.string or s.number there would be no way within the function to determine if the parameter is s.string or s.number
Favna
Favna3y ago
@kyra 🩵🩷🤍🩷🩵 maybe you can help them better
.
.OP3y ago
Awaiting answers need to figure this out to get my library to work
MRDGH2821
MRDGH28213y ago
You want to pass in env variable schema to validator, so that it can automatically parse it?
.
.OP3y ago
Yup
MRDGH2821
MRDGH28213y ago
I e. A dynamic schema? I think it's not possible You will have to define it first, then parse it with validator object itself Let me bring up my approach on parsing JSON data Ok so I had an array of strings which I wanted in array of JS objects like this:
{
id: string,
reason: "some reason"
}
{
id: string,
reason: "some reason"
}
So I did this: https://github.com/MRDGH2821/Discord-Ban-Utils-Bot/blob/rewrite-sapphire/src/lib/utils.ts#L217 For those which were already in JSON format, i used this https://github.com/MRDGH2821/Discord-Ban-Utils-Bot/blob/rewrite-sapphire/src/commands/import-ban-list.ts#L68 For 2 in 1 data:
s.union(s.number, s.string)
s.union(s.number, s.string)
MRDGH2821
MRDGH28213y ago
I also have this, which you might be interested in: https://github.com/MRDGH2821/Discord-Ban-Utils-Bot/blob/rewrite-sapphire/src/lib/SettingsData.ts This one converts Boolean string (stored in database) into Boolean Boolean (for actual use in code)

Did you find this page helpful?