@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 possible19 Replies
process.env is an object and shapeshift has
s.object
so from there you define the required keys.Thank you, also is there a helper type to infer the actual object types from the schema
https://github.com/sapphiredev/shapeshift see the readme
GitHub
GitHub - sapphiredev/shapeshift: Blazing fast input validation and ...
Blazing fast input validation and transformation ⚡ - GitHub - sapphiredev/shapeshift: Blazing fast input validation and transformation ⚡
Also is there any way to do coercion?
Should be transform and reshape (again, see readme, it’s very detailed)
the user of the library would do
how to tell what type of validator it is?
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 integersI'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 erroryou need to parse it before validation
I know but is there any way to get the type of validator to determine how to parse the string
there isnt
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
@kyra 🩵🩷🤍🩷🩵 maybe you can help them better
Awaiting answers
need to figure this out to get my library to work
You want to pass in env variable schema to validator, so that it can automatically parse it?
Yup
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:
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:
Also, https://tryitands.ee/
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)