validatable 0.1.2

  • README.md
  • Installing
  • Versions
  • 35

Validatable

Adds validation capabilities for attributes to a class it is mixed into. It works together with the 'attributable' library and you should first mix Attributable module into the class before mixing in this one. Then define attributes and only after that define validations for them.

Validations work much the same way as they do in the Ruby On Rails framework. When the validate() method is called, it looks at all the attributes mentioned in the validations Map in your class and check if they pass the defined validations. If not, errors are added into the validation_errors Map.

IMPORTANT: please don't forget to check with the documentation for the 'attributable' library before using this one.

Usage example

  1. Mix the Validatable module into your class, and make sure Attributable is also mixed in before it:

     class Employee extends Object with Attributable, Validatable {}
    
  2. Define attributes using the attribute_names instance variable (attributable library feature):

     class Employee extends Object with Attributable, Validatable {
       final List attribute_names = ['first_name', 'family_name', 'age', 'sex', 'email'];
     }
    
  3. Define validations for these attributes:

     class Employee extends Object with Attributable, Validatable {
    
       final List attribute_names = ['first_name', 'family_name', 'age', 'sex', 'email'];
    
       final Map validations = {
    
         'first_name'  : { 'isShorterThan' : 30,     'isLongerThan' : 1  },
         'family_name' : { 'isShorterThan' : 50,     'isLongerThan' : 2  },
         'age'         : { 'isNumeric'     : true,   'isMoreThan'   : 18 },
         'sex'         : { 'isOneOf' : ['f', 'm'],   'isNotEmpty' : true },
    
         'email' : { 'matches' : { 'value': new RegExp(r"@"), 'message' : 'this isn\'t an email, bro' }}
    
       };
    
     }
    
  4. Create a new instance of your class, set attribute values on it and run the validate() method:

     var employee = new Employee();
     employee.first_name  = "Vincent";
     employee.family_name = "Vega";
     employee.validate();
    
  5. See what the validation errors contain:

     print(employee.validation_errors);
    

You can find and run this example in the /example/employee_validations.dart. Don't hesitate to play with various validations and attribute values.

Defining a validation

A validation in the validations Map consists of the following parts:

[String attribute_name]: { [String validation_name] : [validation_argument] }

An attribute_name is simply the name of the attribute that is being validated.

A validation_name is the name of the validation that is being applied to it (for a list of all available validations, see section "List of available validations" of this README).

A validation_argument can be anything that this particular validation accepts: a String, a number or a List (for instructions on what type of agruments are accepted for which validations, see section "List of available validations" of this README)

Any validation can accept a Map as a validation_argument. In that case, the actual validation argument should move under the value key of that Map. There is another key in this Map that may be used called message. It allows you to specify a custom message for the cases in which the validation would fail. From the example above:

'email' : { 'matches' : { value: new RegExp(r"@"), 'message' : 'this isn't an email, bro' }}

We had to write it this way in order to specify a custom message. If we didn't need a custom message, we could've written this validation like this:

'email' : { 'matches' : new RegExp(r"@") }

Custom validation functions

You can define a custom validation function to check whatever conditions you desire:

final Map validations = {
  'email' : { 'function' : { 'name': 'validateEmail', 'message': "doesn't look like an email" }}
};

prvt_validateEmail() => this.email.contains("@");

If the function returns false, it means the validation didn't pass and an error message is added. You can also pass a custom object on which the function will be called. This is useful when you want to have your validation function in a separate object:

class MyValidator {
  validateEmail() => this.email.contains("@");
}

var obj = new MyVlaidator();

...

final Map validations = {
  'email' : { "object": obj, 'function' : { 'name': 'validateEmail', 'message': "doesn't look like an email" }}
};

List of available validations and their descriptions

  • isNumeric
    • Attribute type: String or num
    • Argument: Boolean
    • Behavior : checks whether the value only contains numbers from 0-9. It doesn't matter if the actual type of the attribute value is String, rather it's important that this string contains only numbers.
  • isLessThen
    • Attribute type: num
    • Argument: num
    • Behavior: checks whether the value is less than a certain number.
  • isMoreThen
    • Attribute type: num
    • Argument: num
    • Behavior: checks whether the value is more than a certain number.
  • isOneOf
    • Attribute type: any
    • Argument: List
    • Behavior: checks whether the value is present in the List passed as an argument.
  • isNotOneOf
    • Attribute type: any
    • Argument: List
    • Behavior: checks whether the value is NOT present in the List passed as an argument.
  • isLongerThan
    • Attribute type: String
    • Argument: num
    • Behavior: checks whether the value is longer than the specified number of charcaters.
  • isShorterThan
    • Attribute type: String
    • Argument: num
    • Behavior: checks whether the value is shorter than the specified number of charcaters.
  • hasExactLengthOf
    • Attribute type: String
    • Argument: num
    • Behavior: checks whether the value has the exact length specified in the passed argument.
  • matches
    • Attribute type: String
    • Argument: RegExp
    • Behavior: checks whether the value matches the RegExp passed as an argument.
  • isNotNull
    • Attribute type: any
    • Argument: Boolean
    • Behavior: checks whether the value is not null.
  • isNotEmpty
    • Attribute type: String
    • Argument: Boolean
    • Behavior: checks whether the value is not empty (ignores if it's null).

Use this package as a library

1. Depend on it

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


dependencies:
  validatable: "^0.1.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:validatable/validatable.dart';
  
Version Uploaded Documentation Archive
0.1.2 Aug 12, 2016 Go to the documentation of validatable 0.1.2 Download validatable 0.1.2 archive
0.1.1 Aug 10, 2016 Go to the documentation of validatable 0.1.1 Download validatable 0.1.1 archive
0.1.0 May 21, 2016 Go to the documentation of validatable 0.1.0 Download validatable 0.1.0 archive

Analysis

We analyzed this package on Jun 19, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0-dev.63.0
  • pana: 0.11.3

Scores

Popularity:
Describes how popular the package is relative to other packages. [more]
5 / 100
Health:
Code health derived from static analysis. [more]
99 / 100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
12 / 100
Overall score:
Weighted score of the above. [more]
35
Learn more about scoring.

Platforms

Detected platforms: web, other

Primary library: package:validatable/validatable.dart with components: mirrors.

Suggestions

  • Maintain CHANGELOG.md.

    Changelog entries help clients to follow the progress in your code.

  • Package is getting outdated.

    The package was released 96 weeks ago.

  • Package is pre-v1 release.

    While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction API.

  • Maintain an example.

    Create a short demo in the example/ directory to show how to use this package. Common file name patterns include: main.dart, example.dart or you could also use validatable.dart.

  • Fix analysis and formatting issues.

    Analysis or formatting checks reported 1 hint.

    Run dartfmt to format lib/validatable.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
attributable >=0.1.0 <0.2.0 0.1.2
Dev dependencies
test >=0.12.0