dartx icon indicating copy to clipboard operation
dartx copied to clipboard

[Feature request]Validation String extension

Open shinriyo opened this issue 5 years ago • 5 comments

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.

shinriyo avatar Jun 14 '20 00:06 shinriyo

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

simc avatar Jul 04 '20 08:07 simc

@leisim both of them. and telephone numbers also

shinriyo avatar Jul 04 '20 08:07 shinriyo

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

simc avatar Jul 04 '20 08:07 simc

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

shinriyo avatar Jul 06 '20 01:07 shinriyo

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 😉

passsy avatar Jul 09 '20 14:07 passsy