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
Validate name upon init fixes #1268 #1293
Conversation
I should also note that eslint is pretty unhappy about aliasing a function for later use in Name configuration: {
key: 'name',
question: 'name',
default: path.basename(config.cwd),
validation: validate.isValidPackageName,
validationError: 'Invalid package name',
} while (!validAnswer) {
answer = (await reporter.question(question)) || def;
/* eslint-disable no-var*/
entry.validation(answer) ? validAnswer = true : reporter.error(entry.validationError);
/* eslint-disable */
} I'm unsure why eslint doesn't seem to be able to ignore this line. As for other approaches, direct / manual validation on the name could be an option, but attaching validation properties to each entry offers a bit more extensibility. |
Yarn uses
|
This will make your code pass the lint:
|
@torifat One of the problems is that I've added a property to the keys that builds the question loop. This property,
|
@olingern did you try replacing your code with the snippet I provided? Use |
@torifat Thanks! I overlooked this
Your comment "Flow doesn't understand hasOwnPropert" helped a bunch! |
@samccone Just made a few updates to tidy things up.
|
@samccone
|
@torifat @samccone I can update the I assume I would translate the Invalid package name. Spaces and the following symbols: " @ + % : " are not allowed isValidName function for reference function isValidName(name) {
return !name.match(/[\/@\s\+%:]/) && encodeURIComponent(name) === name;
} |
if (entry.validation(String(answer))) { | ||
validAnswer = true; | ||
} else { | ||
reporter.error(reporter.lang('invalidPackageName')); |
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.
We're hardcoding the error message here which doesn't seem desirable, can you move this to a validationError
property on keys
?
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 (entry.validation) { | ||
while (!validAnswer) { | ||
answer = (await reporter.question(question)) || def; | ||
//validate answer |
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.
Can you add a space after //
to pad this out? Just to keep consistency with how we do comments elsewhere. eg:
// loop
rather than
//loop
Summary
I made use of the existing package name validation and put this inside a loop to enforce valid names, i.e. no spaces.
Fixes #1268
Test plan
Tests for validating user input will need to be added. Guidance, best practices, advice is welcome.