asynchronous_validator 1.0.2

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 34

asynchronous-validator

String validation and sanitization for Dart.

Fork of karan's validator.dart package

Port of chriso's library.

Usage

Documentation

Complete documentation of the package is available at http://www.dartdocs.org/documentation/validator/0.0.7/index.html#validator/validator

For a list of methods, refer to the sections below.

1. Depend on it

Add this to your package's pubspec.yaml file:

dependencies:
  asynchronous_validator: ^1.0.2

2. Install it

You can install packages from the command line:

$ pub get

Or if you use Flutter:

$ flutter packages get

3. Import it

Now in your Dart code, you can use:

import 'package:asynchronous_validator/validator.dart';

Validators

  • equals(String str, comparison) - check if the string matches the comparison.
  • contains(String str, seed) - check if the string contains the seed.
  • matches(String str, pattern) - check if string matches the pattern. matches('foobar', 'foo').
  • isEmail(String str) - check if the string is an email.
  • isURL(String str [, options]) - check if the string is an URL. options is an object which defaults to { protocols: ['http','https','ftp'], require_tld: true, require_protocol: false, allow_underscores: false, host_whitelist: false, host_blacklist: false }.
  • isFQDN(String str [, options]) - check if the string is a fully qualified domain name (e.g. domain.com). options is an object which defaults to { require_tld: true, allow_underscores: false }.
  • isIP(String str [, version]) - check if the string is an IP (version 4 or 6).
  • isAlpha(String str) - check if the string contains only letters (a-zA-Z).
  • isNumeric(String str) - check if the string contains only numbers.
  • isAlphanumeric(String str) - check if the string contains only letters and numbers.
  • isBase64(String str) - check if a string is base64 encoded.
  • isHexadecimal(String str) - check if the string is a hexadecimal number.
  • isHexColor(String str) - check if the string is a hexadecimal color.
  • isLowercase(String str) - check if the string is lowercase.
  • isUppercase(String str) - check if the string is uppercase.
  • isInt(String str) - check if the string is an integer.
  • isFloat(String str) - check if the string is a float.
  • isDivisibleBy(String str, number) - check if the string is a number that's divisible by another.
  • isNull(String str) - check if the string is null.
  • isLength(String str, min [, max]) - check if the string's length falls in a range. Note: this function takes into account surrogate pairs.
  • isByteLength(String str, min [, max]) - check if the string's length (in bytes) falls in a range.
  • isUUID(String str [, version]) - check if the string is a UUID (version 3, 4 or 5).
  • isDate(String str) - check if the string is a date.
  • isAfter(String str [, date]) - check if the string is a date that's after the specified date (defaults to now).
  • isBefore(String str [, date]) - check if the string is a date that's before the specified date.
  • isIn(String str, values) - check if the string is in a array of allowed values.
  • isCreditCard(String str) - check if the string is a credit card.
  • isISBN(String str [, version]) - check if the string is an ISBN (version 10 or 13).
  • isJSON(String str) - check if the string is valid JSON (note: uses JSON.parse).
  • isMultibyte(String str) - check if the string contains one or more multibyte chars.
  • isAscii(String str) - check if the string contains ASCII chars only.
  • isFullWidth(String str) - check if the string contains any full-width chars.
  • isHalfWidth(String str) - check if the string contains any half-width chars.
  • isVariableWidth(String str) - check if the string contains a mixture of full and half-width chars.
  • isSurrogatePair(String str) - check if the string contains any surrogate pairs chars.
  • isMongoId(String str) - check if the string is a valid hex-encoded representation of a [MongoDB ObjectId][mongoid].

Asynchronous Validator

Replace is prefix with validate. For example, replace isEmail with validateEmail to use asynchronous validator that returns a Future object.

validator.validateEmail('username@domain.io').then((validEmail) {
  print('$validEmail is valid email address.');
}).catchError((error) {
  print(error);
});

Custom Error Message

You can use custom error message when using asynchronous validator. For example:

validator.validateEmail('not a valid email', errorMessage: 'Please insert a valid email address.').then((validEmail) {
  print('$validEmail is valid email address.');
}).catchError((error) {
  print(error); // print : Please insert a valid email address.
});

Sanitizers

  • toString(String input) - convert the input to a string.
  • toDate(String input) - convert the input to a date, or null if the input is not a date.
  • toFloat(String input) - convert the input to a float, or NaN if the input is not a float.
  • toInt(String input [, radix]) - convert the input to an integer, or NaN if the input is not an integer.
  • toBoolean(String input [, strict]) - convert the input to a boolean. Everything except for '0', 'false' and '' returns true. In strict mode only '1' and 'true' return true.
  • trim(String input [, chars]) - trim characters (whitespace by default) from both sides of the input.
  • ltrim(String input [, chars]) - trim characters from the left-side of the input.
  • rtrim(String input [, chars]) - trim characters from the right-side of the input.
  • escape(String input) - replace <, >, &, ' and " with HTML entities.
  • stripLow(String input [, keep_new_lines]) - remove characters with a numerical value < 32 and 127, mostly control characters. If keep_new_lines is true, newline characters are preserved (\n and \r, hex 0xA and 0xD). Unicode-safe in JavaScript.
  • whitelist(String input, chars) - remove characters that do not appear in the whitelist. The characters are used in a RegExp and so you will need to escape some chars, e.g. whitelist(String input, '\[\]').
  • blacklist(String input, chars) - remove characters that appear in the blacklist. The characters are used in a RegExp and so you will need to escape some chars, e.g. blacklist(String input, '\[\]').
  • normalizeEmail(String email [, options]) - canonicalize an email address. options is an object which defaults to { lowercase: true }. With lowercase set to true, the local part of the email address is lowercased for all domains; the hostname is always lowercased and the local part of the email address is always lowercased for hosts that are known to be case-insensitive (currently only GMail). Normalization follows special rules for known providers: currently, GMail addresses have dots removed in the local part and are stripped of tags (e.g. some.one+tag@gmail.com becomes someone@gmail.com) and all @googlemail.com addresses are normalized to @gmail.com.

Tests

To test the package, run:

$ ./tool/run_tests.sh

1.0.2

  • Remove dependency to unittest

1.0.1

  • Add CHANGELOG.md
  • Format and move example to separate package
  • Fix code style

1.0.0

  • Initial release

example/main.dart

import 'package:asynchronous_validator/asynchronous_validator.dart'
    as validator;

main() {
  validator
      .validateEmail('not a valid email',
          errorMessage: 'Please insert a valid email address.')
      .then((validEmail) {
    print('$validEmail is valid email address.');
  }).catchError((error) {
    print(error);
  });

  validator.validateURL('hello.com').then((validURL) {
    print(validURL);
  }).catchError((error) {
    print(error);
  });

  validator.validateIP('192.168.10.01', version: 6).then((validIP) {
    print(validIP);
  }).catchError((error) {
    print(error);
  });

  validator.validateEmail('good@example.co.id').then((validEmail) {
    print('$validEmail is a valid email address.');
    return validator.validateFloat('43.022');
  }).then((number) {
    print(number + 100);
    return validator.validateIP('100.12.23.10', version: 90);
  }).then((ipAddress) {
    print(ipAddress);
  }).catchError((error) {
    print(error);
  });

  validator
      .validateURL('https://rest-api.io/posts?limit=10&filter[status]=current')
      .then((validUri) {
    print(validUri.toString());
  }).catchError((error) {
    print(error);
  });

  validator.validateDate('1995-10-05').then((date) {
    print(date.toIso8601String());
    return validator.validateAfter('1996-12-28', after: date);
  }).then((date) {
    print(date.toIso8601String());
  }).catchError((error) {
    print(error);
  });

  validator.validateAfter('2010-10-04').then((date) {
    print('${date.toIso8601String()} is after today.');
  }).catchError((error) {
    print(error);
  });

  validator.validateISBN('979-97221-0-1').then((validISBN) {
    print('$validISBN is a valid ISBN.');
  }).catchError((error) {
    print(error);
  });
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  asynchronous_validator: ^1.0.2

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:asynchronous_validator/asynchronous_validator.dart';
  
Version Uploaded Documentation Archive
1.0.2 Mar 23, 2018 Go to the documentation of asynchronous_validator 1.0.2 Download asynchronous_validator 1.0.2 archive
1.0.1 Mar 23, 2018 Go to the documentation of asynchronous_validator 1.0.1 Download asynchronous_validator 1.0.1 archive
1.0.0 Mar 23, 2018 Go to the documentation of asynchronous_validator 1.0.0 Download asynchronous_validator 1.0.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
69
Health:
Code health derived from static analysis. [more]
0
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
0
Overall:
Weighted score of the above. [more]
34
Learn more about scoring.

The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.

Analysis issues and suggestions

Support Dart 2 in pubspec.yaml.

The SDK constraint in pubspec.yaml doesn't allow the Dart 2.0.0 release. For information about upgrading it to be Dart 2 compatible, please see https://www.dartlang.org/dart-2#migration.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.8.0 <2.0.0