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

feat: add validation for readOptions with zod #826

Merged
merged 21 commits into from
Sep 18, 2023

Conversation

nowyDEV
Copy link
Contributor

@nowyDEV nowyDEV commented Sep 13, 2023

PR Checklist

Overview

Removed type assertions and replaced them with zod validation.

@codecov
Copy link

codecov bot commented Sep 13, 2023

Codecov Report

Merging #826 (bcb68aa) into main (c66fdda) will increase coverage by 0.56%.
Report is 2 commits behind head on main.
The diff coverage is 80.50%.

@@            Coverage Diff             @@
##             main     #826      +/-   ##
==========================================
+ Coverage   91.61%   92.18%   +0.56%     
==========================================
  Files          84       84              
  Lines        4161     4220      +59     
  Branches      248      261      +13     
==========================================
+ Hits         3812     3890      +78     
+ Misses        349      330      -19     
Flag Coverage Δ
create 71.95% <78.12%> (-0.09%) ⬇️
initialize 34.61% <81.25%> (+0.52%) ⬆️
migrate 73.27% <79.16%> (-0.08%) ⬇️
unit 52.51% <78.81%> (+4.81%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Changed Coverage Δ
src/shared/types.ts 0.00% <0.00%> (ø)
src/shared/options/augmentOptionsWithExcludes.ts 62.09% <50.00%> (-0.49%) ⬇️
src/bin/index.ts 100.00% <100.00%> (ø)
src/bin/mode.ts 100.00% <100.00%> (ø)
src/create/index.ts 89.04% <100.00%> (+0.15%) ⬆️
src/initialize/index.ts 97.77% <100.00%> (+0.05%) ⬆️
src/migrate/index.ts 97.56% <100.00%> (+0.06%) ⬆️
src/shared/options/readOptions.ts 89.23% <100.00%> (+15.89%) ⬆️
src/steps/uninstallPackages.ts 100.00% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@nowyDEV
Copy link
Contributor Author

nowyDEV commented Sep 14, 2023

Looks like adding zod breaks migration script, not sure how to deal with this 😿

Copy link
Owner

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

Great stuff! Really excited about this - it's a nice and clean solution that removes a bunch of type assertions. 🔥

Left some comments, but I'm not super picky on any of them. What do you think?

@@ -7656,4 +7659,3 @@ packages:

/[email protected]:
resolution: {integrity: sha512-wvWkphh5WQsJbVk1tbx1l1Ly4yg+XecD+Mq280uBGt9wa5BKSWf4Mhp6GmrkPixhMxmabYY7RbzlwVP32pbGCg==}
dev: true
Copy link
Owner

@JoshuaKGoldberg JoshuaKGoldberg Sep 14, 2023

Choose a reason for hiding this comment

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

[Comment] Haha, nice to see zod already included!

src/shared/options/readOptions.test.ts Outdated Show resolved Hide resolved
src/shared/options/readOptions.test.ts Outdated Show resolved Hide resolved
src/shared/options/readOptions.ts Outdated Show resolved Hide resolved
src/shared/options/readOptions.ts Outdated Show resolved Hide resolved
@JoshuaKGoldberg JoshuaKGoldberg added the status: waiting for author Needs an action taken by the original poster label Sep 15, 2023
@github-actions github-actions bot removed the status: waiting for author Needs an action taken by the original poster label Sep 15, 2023
src/bin/index.ts Outdated
@@ -50,7 +51,9 @@ export async function bin(args: string[]) {
return 1;
}

const { code, options } = await { create, initialize, migrate }[mode](args);
const { code, options, zodError } = await { create, initialize, migrate }[
Copy link
Owner

Choose a reason for hiding this comment

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

[Style] As a total non-blocking aside, I went back and forth between making an object like const runners = { create, ... } in the root of the file or in the function. It might look cleaner here? Not a blocker whichever way you prefer 😄 just raising that I'm not picky.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think making a separate variable might make it a little bit more readable.

},
});
});
});
Copy link
Owner

Choose a reason for hiding this comment

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

[Praise] 💯 love it! No notes!

Copy link
Owner

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

Looking great! I think the only blocking request on my end is on the prompts.outro. Otherwise everything is either praise or a nitpick. Nice! 🔥

mockInitialize.mockResolvedValue({
code: 2,
options: {},
zodError: !validationResult.success ? validationResult.error : null,
Copy link
Owner

Choose a reason for hiding this comment

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

[Cleanup] Nitpicking: no need for a runtime check, we can use a type assertion:

Suggested change
zodError: !validationResult.success ? validationResult.error : null,
zodError: (validationResult as z.SafeParseError<{ email: string }>).error,

A little more code, but IMO more true to what the code is trying to do.

src/bin/index.ts Outdated

if (zodError) {
const validationError = fromZodError(zodError);
prompts.outro(chalk.red(validationError));
Copy link
Owner

Choose a reason for hiding this comment

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

prompts.outro

[Bug] The terminal output looks a little buggy with two outros:

┌  Welcome to create-typescript-app ! 🎉
│
│  ⚠️ This template is early stage, opinionated, and not endorsed by the TypeScript team. ⚠️
│  ⚠️ If any tooling it sets displeases you, you can always remove that portion manually. ⚠️
│
●  Tip: to run again with the same input values, use: npx create-typescript-app --mode create --base everything --email wat
│
│
└  Validation error: Invalid email at "email"

└  Operation cancelled. Exiting - maybe another time? 👋

How about switching to a logLine? I think the ideal would be a blank line around the validatione rror:

┌  Welcome to create-typescript-app ! 🎉
│
│  ⚠️ This template is early stage, opinionated, and not endorsed by the TypeScript team. ⚠️
│  ⚠️ If any tooling it sets displeases you, you can always remove that portion manually. ⚠️
│
●  Tip: to run again with the same input values, use: npx create-typescript-app --mode create --base everything --email wat
│
│  Validation error: Invalid email at "email"
│
└  Operation cancelled. Exiting - maybe another time? 👋

@JoshuaKGoldberg JoshuaKGoldberg added the status: waiting for author Needs an action taken by the original poster label Sep 18, 2023
@github-actions github-actions bot removed the status: waiting for author Needs an action taken by the original poster label Sep 18, 2023
Copy link
Owner

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

Super! This is great - really happy about the reduction of assertions in the codebase. Thanks! ✨

@JoshuaKGoldberg JoshuaKGoldberg merged commit f4e5830 into JoshuaKGoldberg:main Sep 18, 2023
15 of 16 checks passed
@github-actions
Copy link

🎉 This is included in version v1.29.40 🎉

The release is available on:

Cheers! 📦🚀

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.

🚀 Feature: Validate parseArgs values with Zod
2 participants