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

[Feature request]Validation String extension #86

Open
shinriyo opened this issue Jun 14, 2020 · 5 comments
Open

[Feature request]Validation String extension #86

shinriyo opened this issue Jun 14, 2020 · 5 comments

Comments

@shinriyo
Copy link

validation extension is also concept?

like this

extension EmailValidator on String {
  bool isValidEmail() {
    return RegExp(
        r'^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$')
        .hasMatch(this);
  }

  bool isValidHoge() {
     // blah-blah-blah
  }

  // ....
}

If it is. I will do pull-request.

@simc
Copy link
Owner

simc commented Jul 4, 2020

@shinriyo I like the idea. What else could we validate? Emails, URLs, ...

@shinriyo
Copy link
Author

shinriyo commented Jul 4, 2020

@leisim both of them.
and telephone numbers also

@simc
Copy link
Owner

simc commented Jul 4, 2020

@shinriyo Phone numbers are super complicated to validate. I think this should be a separate package (see github.com/google/libphonenumber)

@shinriyo
Copy link
Author

shinriyo commented Jul 6, 2020

@leisim Thank you. I checked the URL's contents. but, there are not dart languages though it is google official language.

@passsy
Copy link
Collaborator

passsy commented Jul 9, 2020

We should avoid adding too many extensions on String. Strings are widely used and most don't represent a phone number or an email address.

For such cases I suggest creating new packages, like an email validator which could also contain validation logic.

final String mailAddress = json["email"] as String;
final EmailAddress email = EmailAddress.tryParse(mailAddress);
if (email != null) {
  print("Email is valid");
}

Side note: Email validation is as tricky as phone number validation. Probably impossible to cover in a single regex. I'd be happy if there is a package with like 100 tests for it 😉

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

No branches or pull requests

3 participants