Skip to content
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

Merged
merged 13 commits into from
Jan 25, 2021
Merged

Conversation

af
Copy link
Owner

@af af commented Oct 5, 2020

See this comment for an overview of the changes (and the rest of that thread for context).

Todo

  • Still a few type errors to sort out
  • Add a browser-friendly reporter (no colorized output, no process.exit())
  • New examples (usage w/ dotenv, emulating the old non-strict mode with middleware)

Copy link

@KATT KATT left a 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
Comment on lines 28 to 33
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}]`)
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 Show resolved Hide resolved
src/envalid.ts Outdated Show resolved Hide resolved
src/envalid.ts Outdated Show resolved Hide resolved
src/envalid.ts Outdated
Comment on lines 94 to 98
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
}
Copy link

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?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I agree 👍

src/validators.ts Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
src/reporter.ts Outdated Show resolved Hide resolved
src/types.ts Outdated Show resolved Hide resolved
src/types.ts Outdated Show resolved Hide resolved
Copy link
Collaborator

@SimenB SimenB left a 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

package.json Outdated Show resolved Hide resolved
package.json Show resolved Hide resolved
src/envalid.ts Outdated Show resolved Hide resolved
src/envalid.ts Outdated Show resolved Hide resolved
src/envalid.ts Outdated Show resolved Hide resolved
tsconfig.json Show resolved Hide resolved
@af af force-pushed the v7 branch 3 times, most recently from d4bb625 to 9eb3486 Compare October 18, 2020 23:28
@af af force-pushed the v7 branch 5 times, most recently from 5d64527 to a0a547b Compare January 3, 2021 01:05
@af af changed the title WIP: V7 v7: Rewrite in TypeScript, and increased modularity with middleware Jan 3, 2021
@af af changed the title v7: Rewrite in TypeScript, and increased modularity with middleware v7: Rewrite in TypeScript, increased modularity with middleware Jan 3, 2021
@af af merged commit 541fa51 into master Jan 25, 2021
@af af deleted the v7 branch January 25, 2021 02:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants