-
Notifications
You must be signed in to change notification settings - Fork 65
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
v7: Rewrite in TypeScript, increased modularity with middleware #127
Conversation
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.
A lot of comments in-line. Haven't reviewed the tests.
Main points I have
- Not sold on the middlewares concept unless the return value can be inferred reliably
- I think
process.env
should be optional in the options object - I don't get the point of the second param in makeValidator
src/envalid.ts
Outdated
if (spec.choices) { | ||
if (!Array.isArray(spec.choices)) { | ||
throw new TypeError(`"choices" must be an array (in spec for "${name}")`) | ||
} else if (!spec.choices.includes(value)) { | ||
throw new EnvError(`Value "${value}" not in choices [${spec.choices}]`) | ||
} |
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.
if (spec.choices) { | |
if (!Array.isArray(spec.choices)) { | |
throw new TypeError(`"choices" must be an array (in spec for "${name}")`) | |
} else if (!spec.choices.includes(value)) { | |
throw new EnvError(`Value "${value}" not in choices [${spec.choices}]`) | |
} | |
if (spec.choices && !spec.choices.includes(value)) { | |
throw new EnvError(`Value "${value}" not in choices [${spec.choices}]`) | |
} |
I think validating the spec itself (checking it's an array) should be done in the caller and not in validateVar
-fn
src/envalid.ts
Outdated
try { | ||
// @ts-ignore FIXME | ||
if (rawValue === testOnlySymbol) { | ||
throw new EnvMissingError(formatSpecDescription(spec)) | ||
} | ||
|
||
if (rawValue === undefined) { | ||
if (!usingFalsyDefault) { | ||
throw new EnvMissingError(formatSpecDescription(spec)) | ||
} | ||
|
||
output[k] = undefined | ||
} else { | ||
// @ts-ignore FIXME | ||
output[k] = validateVar({ name: k, spec, rawValue }) | ||
} | ||
} catch (err) { | ||
if (options.reporter === null) throw err | ||
errors[k] = err | ||
} |
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.
This block is quite hard to follow, could probably be refactored?
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.
Yes I agree 👍
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.
exciting 👍 Just skimmed it, but nothing much jumped out.
Could consider migrating to Jest in a separate commit to keep the diff down (and possibly make changes in implementation highlighted by more narrow diff in tests), but not sure if it's worth it or not
d4bb625
to
9eb3486
Compare
5d64527
to
a0a547b
Compare
Typing updates, move Object.freeze() out from middleware Readme update Fix testOnly type error Workaround for testOnly type error
See this comment for an overview of the changes (and the rest of that thread for context).
Todo