-
Notifications
You must be signed in to change notification settings - Fork 64
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
Custom parsers are typed to receive strings, but spec requires default values to match the type #102
Comments
This is a good point, I don't think the type of Looking at the implementation, I'll try and get to this change in the next week unless anyone raises a reasonable objection. PRs welcome in the meantime |
Huh, the behaviour of After all, defaults are (I hope) known values -- they shouldn't require additional validation, so if So in @novemberborn's case I'd suggest the typings aren't wrong, but that this should work: envalid.cleanEnv({
VALUE: 'decafbad',
}, {
VALUE: hex({ default: Buffer.from('ff', 'hex') })
}) Playing around in the node repl, it looks like it actually does, but only because Personally I'd be in favour of directly returning a default value if it's available and We could even add an overload to allow |
What's nice about specifying strings is that it's easier to see how to use those values in For example I have a validator built on top of |
In your particular case with I thought about it some more and I do think you have a good point, though -- Envalid is meant to work on env vars; that is, it's mostly operating on string inputs anyway. And what I said before about validated values might be backwards -- Unfortunately, the other side of the coin is that for non-string primitive validators (boolean, number) it's a bit more awkward and brittle to define
or: rather than just letting them type the literals |
So going forward, how about this?
|
@lostfictions that sounds great, I like it! |
Hey folks! Just ran into this issue, anything I can do to help implement this? |
Is it maybe a simpler change to have |
The parser is typed as receiving a string value:
envalid/src/envalid.d.ts
Line 116 in ad2f612
However the validator specs infer the validated type and mandate it for the
default
anddevDefault
values. That's nice for numbers and booleans but I suspect it'll trip up thejson
validator. It works for myhex
validator above but only because Node.js doesn't throw an error for the unnecessary encoding argument.Perhaps the default values should always be strings, and be typed as such?
The text was updated successfully, but these errors were encountered: