The validator package comes with several common validations and removes the boiler plate code from your project.

Features

  • Contains several common validations
  • Supports grouping of validators to write concise code
  • Zero dependency
  • Well tested
  • Easy to use and removes boiler plate code from your project

How to use

Install

flutter pub add pro_validator

Example

NOTE: if validate value contains is a null value, the result is null, not an error.

import 'package:pro_validator/pro_validator.dart';

void main() {
  const emailValidator = MultiValidator(
    validators: [
      RequiredValidator(error: 'Required field'),
      EmailValidator(error: 'Invalid email'),
    ],
  );

  print('null email validation ${emailValidator(null)}');
  print('empty email validation ${emailValidator('')}');
  print('invalid email validation ${emailValidator('mail@com')}');
  print('valid email validation ${emailValidator('mail@mail.com')}');

  const passwordValidator = MultiValidator(
    validators: [
      RequiredValidator(error: 'Required field'),
      MinLengthValidator(min: 8, error: 'Min length 8'),
      HasUppercaseValidator(error: 'Must contain at least one uppercase'),
      HasLowercaseValidator(error: 'Must contain at least one lowercase'),
    ],
  );

  print('null password validation ${passwordValidator(null)}');
  print('empty password validation ${passwordValidator('')}');
  print('min length password validation ${passwordValidator('1232')}');
  print('invalid password validation ${passwordValidator('12345678')}');
  print('invalid password validation ${passwordValidator('12345678A')}');
  print('valid password validation ${passwordValidator('a12345678A')}');

  final matchValidator = MatchValidator(error: 'Do not match');

  print('match validation ${matchValidator('a', 'b')}');
}

Available Validators

ValidatorDescription
RequiredValidatorEnsures the value is not empty, not white space only.
MaxLengthValidatorEnsures the value length contains no more than a set [max] of characters.
MinLengthValidatorEnsures the value length contains no fewer than a set [min] of characters.
HasUppercaseValidatorEnsures the value contains a minimum of one uppercase character.
HasLowercaseValidatorEnsures the value contains a minimum of one lowercase character.
HasANumberValidatorEnsures the value contains a minimum of one numeric character.
LengthRangeValidatorEnsures the value length is contained in the range [min, max].
NumRangeValidatorEnsures the num value is contained in the range [min, max].
EmailValidatorEnsures the value is a validly formatted email address.
PhoneValidatorEnsures the value is a validly formatted phone number.
UrlValidatorEnsures the value is a validly formatted URL.
PatternValidatorEnsures a custom regular expression string.
MatchValidatorA special match validator to check if the v1 equals v2 value.
MultiValidatorGroup together and validate the basic validators.