drails_validator 0.2.0

  • README.md
  • Installing
  • Versions
  • 0


Build Status

Library for validate models similar to JPA Bean validation. It provides a way to validate a dart object using constraints which we can use for validation. To check if the object is valid we need to invoke the function validate this one returns an Object of type ValidationResult which contains a map of errors for every attribute of the object, for example:

  1. Create a new dart project

  2. Add dependencies to pubspec.yaml

  drails_validator: ^0.1.0 #change it for the latest version

3.Create/edit the file main.dart in the folder bin and put next code on it:

// Copyright (c) 2015, Luis Vargas. All rights reserved. Use of this source code
// is governed by a BSD-style license that can be found in the LICENSE file.

library Validator.example;

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

part 'drails_validator_sample.g.dart';

bool lowerThanOrEqualNow(DateTime dateOfBirth) =>
  !dateOfBirth.isAfter(new DateTime.now());

bool isSSN(String ssn) =>
  validator.matches(ssn, r'\d\d\d-\d\d-\d\d\d\d');

bool isEmail(String email) =>
    isNotNull(email) && validator.isEmail(email);

_fieldAGt123(Person p) => p.fieldA != null && p.fieldA > 123;

class Person extends _$PersonSerializable {
  int id;

  @Length(min: 2)
  String firstName;

  @Length(min: 2)
  String lastName;

  @ValidIf(isEmail, description: 'The entered email is invalid')
  String email;

  @ValidIf(lowerThanOrEqualNow, description: 'Values after now are not allowed')
  DateTime dateOfBirth;

  @ValidIf(isSSN, description: 'The entered SSN is invalid')
  String ssn;

  int fieldA;

  @NotNull(iff: _fieldAGt123, description: "fieldB should not be null if fieldA is greater than 123")
  int fieldB;

main() {

  var invalidPerson = new Person()
        ..id = 1
        ..firstName = ''
        ..lastName = ''
        ..dateOfBirth = new DateTime(2030, 11, 9)
        ..ssn = '123',
      validPerson = new Person()
        ..id = 2
        ..firstName = 'Joe'
        ..lastName = 'Doe'
        ..email = 'joedoe@email.com'
        ..dateOfBirth = new DateTime(1989, 11, 9)
        ..ssn = '123-45-6789';

  print('invalidPerson: ${validate(invalidPerson)}');
  print('validPerson: ${validate(validPerson)}');

  1. create a file in tool folder called build.dart and put next code on it:
import 'package:build_runner/build_runner.dart';
import 'package:dson/phase.dart';

main() async {
  await build(new PhaseGroup()
      // In next line replace `example/**.dart` for the globs you want to use as input, for example `**/*.dart`
      // to take all the dart files of the project as input.
        dsonPhase(const ['example/**.dart'])),
      deleteFilesByDefault: true);
  1. run tool/build.dart. Then you will see that the file bin/drails_validator_sample.g.dart has been generated and it will contains the next code:

part of Validator.example;

// **************************************************************************
// Generator: InitMirrorsGenerator
// Target: library Validator.example
// **************************************************************************

_initMirrors() {
  initClassMirrors({Person: PersonClassMirror});

// **************************************************************************
// Generator: DsonGenerator
// Target: class Person
// **************************************************************************

abstract class _$PersonSerializable extends SerializableMap {
  int get id;
  String get firstName;
  String get lastName;
  String get email;
  DateTime get dateOfBirth;
  String get ssn;
  int get fieldA;
  int get fieldB;
  void set id(int v);
  void set firstName(String v);
  void set lastName(String v);
  void set email(String v);
  void set dateOfBirth(DateTime v);
  void set ssn(String v);
  void set fieldA(int v);
  void set fieldB(int v);

  operator [](Object key) {
    switch (key) {
      case 'id':
        return id;
      case 'firstName':
        return firstName;
      case 'lastName':
        return lastName;
      case 'email':
        return email;
      case 'dateOfBirth':
        return dateOfBirth;
      case 'ssn':
        return ssn;
      case 'fieldA':
        return fieldA;
      case 'fieldB':
        return fieldB;
    throwFieldNotFoundException(key, 'Person');

  operator []=(Object key, value) {
    switch (key) {
      case 'id':
        id = value;
      case 'firstName':
        firstName = value;
      case 'lastName':
        lastName = value;
      case 'email':
        email = value;
      case 'dateOfBirth':
        dateOfBirth = value;
      case 'ssn':
        ssn = value;
      case 'fieldA':
        fieldA = value;
      case 'fieldB':
        fieldB = value;
    throwFieldNotFoundException(key, 'Person');

  Iterable<String> get keys => PersonClassMirror.fields.keys;

_Person__Constructor(params) => new Person();

const $$Person_fields_id = const DeclarationMirror(type: int);
const $$Person_fields_firstName = const DeclarationMirror(
    type: String,
    annotations: const [const Length(min: 2, max: null, description: null)]);
const $$Person_fields_lastName = const DeclarationMirror(
    type: String,
    annotations: const [const Length(min: 2, max: null, description: null)]);
const $$Person_fields_email = const DeclarationMirror(
    type: String,
    annotations: const [
      const ValidIf(isEmail,
          description: r'The entered email is invalid', iff: null)
const $$Person_fields_dateOfBirth =
    const DeclarationMirror(type: DateTime, annotations: const [
  const ValidIf(lowerThanOrEqualNow,
      description: r'Values after now are not allowed', iff: null)
const $$Person_fields_ssn =
    const DeclarationMirror(type: String, annotations: const [
  const ValidIf(isSSN, description: r'The entered SSN is invalid', iff: null)
const $$Person_fields_fieldA = const DeclarationMirror(type: int);
const $$Person_fields_fieldB =
    const DeclarationMirror(type: int, annotations: const [
  const NotNull(
      description: r'fieldB should not be null if fieldA is greater than 123',
      iff: _fieldAGt123)

const PersonClassMirror =
    const ClassMirror(name: 'Person', constructors: const {
  '': const FunctionMirror(parameters: const {}, call: _Person__Constructor)
}, fields: const {
  'id': $$Person_fields_id,
  'firstName': $$Person_fields_firstName,
  'lastName': $$Person_fields_lastName,
  'email': $$Person_fields_email,
  'dateOfBirth': $$Person_fields_dateOfBirth,
  'ssn': $$Person_fields_ssn,
  'fieldA': $$Person_fields_fieldA,
  'fieldB': $$Person_fields_fieldB
}, getters: const [
], setters: const [

  1. Finally you can run the file bin/drails_validator_sample.dart. If everything is ok you will see next output in console:
invalidPerson: isValid: false, errors: {firstName: [Length should be greather than 2], lastName: [Length should be greather than 2], email: [The entered email is invalid], dateOfBirth: [Values after now are not allowed], ssn: [The entered SSN is invalid]}
validPerson: isValid: true, errors: {}


  • rename customDescription to description in annotations
  • rename description to defaultDescription in annotations
  • add iff method to annotations
  • add typedef IfFunction for iff method



  • add reflectable ''>=0.3.2 <0.4.0"
  • upgrade drails_commons ^0.0.6
  • add Matches annotation
  • add test library and remove unittest


  • Initial version, created by Stagehand

Use this package as a library

1. Depend on it

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

  drails_validator: ^0.2.0

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:drails_validator/drails_validator.dart';
Version Uploaded Documentation Archive
0.2.0 Jun 15, 2017 Go to the documentation of drails_validator 0.2.0 Download drails_validator 0.2.0 archive
0.1.0 May 16, 2017 Go to the documentation of drails_validator 0.1.0 Download drails_validator 0.1.0 archive
0.0.2 Nov 18, 2015 Go to the documentation of drails_validator 0.0.2 Download drails_validator 0.0.2 archive
0.0.1 Mar 23, 2015 Go to the documentation of drails_validator 0.0.1 Download drails_validator 0.0.1 archive
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
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.

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.


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