-
-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Treat ""
As Missing Value
#31
Conversation
Hey Adam, thanks so much for the PR! I really like the idea, I actually think it should be the default behaviour and an opt-out rather than an opt-in (breaking behaviour so I'll do a new major). Would you mind updating your PR so the API is something like this? 👇 Other ideas on approaching the problem are welcome as well.. Also, tick the "Allow edits"-thingy, please :) Default behaviourimport { str, envsafe, port, url } from 'envsafe';
export const env = envsafe({
CDN_URL: str({
devDefault: 'http://localhost:3000',
}),
// ...
}) ❌ Fail if CDN_URL is empty string New option
|
Great, looking forward to reviewing it. I've merged Fine if you want to make a fresh PR as well! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See previous comment #31 (comment)
Looking into it more I realised that the issue exists on the validators that parse the string input - e.g. After considering the different ways to do this I still think the best way to handle it is an option on |
Empty string will never be valid on url nor number/port though, will it? |
No, but it does allow defaulting over the missing value instead of it just erroring e.g. // Current: Errors due to "" not being a valid port
port({
input: process.env.PORT, // ""
devDefault: 3000,
default: 80,
})
// New: Empty string is treated as null, falls back to defaults |
Oh, true! I didn't consider defaults, now I'm with you. Maybe an option to I'm still contemplating if it's better to do it granularly on the validators; I reckon it's rare that one would want to do this and it might be useful to do it granularly. |
It'll probably leave the codebase cleaner to do it with the options object though, so do that if you want! Sorry for the back-and-forth! :) |
I agree that having the option on the validators for more granular control is better, and I did finally realize that I just had to change where the option came from in the implementation for it to work... Should I also add a changelog? Just the simple template from https://keepachangelog.com/en/1.0.0/ |
Simplified a bit in f24df97 Released as Thanks! |
This PR treats
""
values the same asundefined
, with an option to use the old behavior.We use dotenv files to show what options are available in our projects, with empty values in them to show that they are optional or not possible to commit to repositories:
This causes problems with envsafe when we have default values for these env vars (e.g. defaulting the URLs to
localhost
) since it results inprocess.env.CDN_URL = ""
.Ensure other validators other thanstr()
don't have theallowEmpty
-propertyREADME.md