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

Validate name upon init fixes #1268 #1293

Closed
wants to merge 3 commits into from
Closed

Validate name upon init fixes #1268 #1293

wants to merge 3 commits into from

Conversation

olingern
Copy link
Contributor

@olingern olingern commented Oct 20, 2016

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.

screen shot 2016-10-20 at 5 51 19 am

@olingern olingern changed the title Validate name upon init #1268 Validate name upon init Oct 20, 2016
@olingern olingern changed the title Validate name upon init Validate name upon init fixes #1268 Oct 20, 2016
@olingern
Copy link
Contributor Author

olingern commented Oct 20, 2016

I should also note that eslint is pretty unhappy about aliasing a function for later use in src/cli/commands/

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.

@torifat
Copy link
Member

torifat commented Oct 20, 2016

Yarn uses flow. So, beside checking with eslint also check your code with flow. Just run yarn run lint. BTW, you might want you provide a more meaningful feedback to user as npm does.

Sorry, name can only contain URL-friendly characters and name can no longer contain capital letters.

@torifat
Copy link
Member

torifat commented Oct 20, 2016

This will make your code pass the lint:

    let answer;

    if (yes) {
      answer = def;
    } else {
      let validAnswer = false;
      // loop until a valid answer is provided, if validation is on entry
      if (entry.validation) {
        while (!validAnswer) {
          answer = (await reporter.question(question)) || def;
          answer && entry.validation(answer) ? validAnswer = true : reporter.error(entry.validationError);
        }
      } else {
        answer = (await reporter.question(question)) || def;
      }
    }

@olingern
Copy link
Contributor Author

olingern commented Oct 20, 2016

@torifat One of the problems is that I've added a property to the keys that builds the question loop. This property, validation, holds a function that is later executed. Flow doesn't like it because it has no idea what entry.validation( ) is.

src/cli/commands/init.js:116
116:           entry.validation(answer) ? validAnswer = true : reporter.error(entry.validationError);
                      ^^^^^^^ property `validation`. Property not found in
116:           entry.validation(answer) ? validAnswer = true : reporter.error(entry.validationError);
                      ^^^^^ object literal

src/cli/commands/init.js:116
116:           entry.validation(answer) ? validAnswer = true : reporter.error(entry.validationError);
                                           ^^^^^^ possibly uninitialized variable. This type is incompatible with
 41: export function isValidPackageName(name: string): boolean {
                                         ^^^^^^ string. See: src/util/normalize-manifest/validate.js:41

src/cli/commands/init.js:116
116:           entry.validation(answer) ? validAnswer = true : reporter.error(entry.validationError);
                                                                                    ^^^^^^^^^^^^^^^ property `validationError`. Property not found in
116:           entry.validation(answer) ? validAnswer = true : reporter.error(entry.validationError);
                                                                              ^^^^^ object literal

@torifat
Copy link
Member

torifat commented Oct 20, 2016

@olingern did you try replacing your code with the snippet I provided? Use if (entry.validation) { instead of if (entry.hasOwnProperty) {. Flow doesn't understand hasOwnProperty -> facebook/flow#2008

@olingern
Copy link
Contributor Author

@torifat Thanks! I overlooked this

if (entry.validation)

Your comment "Flow doesn't understand hasOwnPropert" helped a bunch!

@olingern
Copy link
Contributor Author

@samccone Just made a few updates to tidy things up.

  • Validation uses the pre-existing language file to generate the error message
  • Condensed git history

@torifat
Copy link
Member

torifat commented Oct 20, 2016

@samccone Invalid package name. didn't seem like a user friendly message. npm uses the following:

Sorry, name can only contain URL-friendly characters and name can no longer contain capital letters.

@olingern
Copy link
Contributor Author

@torifat @samccone I can update the invalidPackageName key in src/reporters/lang/en.js

I assume I would translate the validateName function in validate.js into something meaningful such as:

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'));
Copy link
Contributor

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?

Copy link
Contributor Author

@olingern olingern Oct 25, 2016

Choose a reason for hiding this comment

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

@kittens I opened a new PR #1425. Issues with my local env. Your suggest changes are in there as well as a cleaner git history. :)

if (entry.validation) {
while (!validAnswer) {
answer = (await reporter.question(question)) || def;
//validate answer
Copy link
Contributor

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

@olingern olingern closed this Oct 25, 2016
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.

None yet

4 participants