validations 0.8.5+1

  • Readme
  • Changelog
  • Installing
  • 59

Validations for Dart. #

Pub Package Build Status codecov

Validator setup #

You should add validations_generator as a dependency:

dev_dependencies:
  build_runner:
  validations_generator:

First declare your model and assign a generator class to validate the model.

car.dart:

import 'package:decimal/decimal.dart';
import 'package:validations/validations.dart';

part 'car.g.dart';

class Driver {
  Driver({this.name});
  @NotNull()
  String name;
}

class Car {
  Car({
    this.manufacturer,
    this.licensePlate,
    this.seatCount,
    this.topSpeed,
    this.price,
    this.isRegistered,
  });

  @NotNull()
  String manufacturer;

  @Valid(message: 'There should be a valid driver!')
  Driver driver;

  @Size(
    min: 2,
    max: 14,
    message:
        r'The license plate ${validatedValue} must be between ${min} and ${max} characters long',
  )
  @NotNull()
  String licensePlate;

  @Min(
    value: 1,
    message: r'Car must at least have ${value} seats available',
  )
  @Max(
    value: 2,
    message: r'Car cannot have more than ${value} seats',
  )
  int seatCount;

  @Max(
    value: 350,
    message: r'The top speed ${validatedValue} is higher than ${value}',
  )
  int topSpeed;

  @DecimalMax(
    value: '100.00',
    message: r'Price must not be lower than ${value}',
  )
  @DecimalMin(
    value: '49.99',
    message: r'Price must not be higher than ${value}',
  )
  Decimal price;

  @IsTrue(message: 'Car must be registered!')
  bool isRegistered;
}

@GenValidator()
class TestCarValidator extends Validator<Car> with _$TestCarValidator {}

@GenValidator()
class TestDriverValidator extends Validator<Driver> with _$TestDriverValidator {}

Generate the validators #

After the models have been annotated the validators should be generated:

# Dart
pub run build_runner build

# Flutter
flutter pub run build_runner build

Usage #

import 'car.dart';

final car = Car();

car.driver = Driver(name: 'TestDriver');
car.price = Decimal.parse('99.99');
car.isRegistered = true;
car.licensePlate = 'DY28-38';
car.manufacturer = 'VEB Sachsenring';
car.seatCount = 2;
car.topSpeed = 100;

final validator = TestCarValidator();

// Full validation of the model
validator.validate(car);

// Validates only a specific property returning all violations
validator.validateProperty(car, 'price');

// Check violations given an arbitrary value using the validators defined for `manufacturer`
validator.validateValue('manufacturer', null);

// Returns first error message as a string or null if there are no errors.
validator.errorCheck('isRegistered', false);

// Convenience methods are also generated which can be assigned directly to form validators in
// flutter e.g. validator: validator.validateLicensePlate,
// Internally it performs an errorCheck and thus also either returns an error message or [null];
validator.validateLicensePlate('DX');
validator.validateRegistered(true);
...etc

Test Coverage #

To run test coverage locally.

pub run test_coverage
# To install genhtml: apt|brew install lcov
genhtml -o coverage coverage/lcov.info
open coverage/index.html

Features and bugs #

Please file feature requests and bugs at the issue tracker.

0.8.5+1 #

  • Be more permissive about dependency versions.

0.8.4 #

  • Remove unused and unintentionally exported Alias mixin.

0.8.3 #

  • fix unintended modification of class validator map.

0.8.2 #

  • bump version to match the generator version.

0.8.0 #

  • add @Target to field annotations
  • add @Constraint to field annotations
  • fix time unit validator
  • improve class level validations and error messages

0.7.0 #

  • add support for class level validators
  • add a FieldMatch class level validator
  • introduce @property to support constructing class level validators.

0.6.0 #

  • make internationalization of messages optional.
  • generate to shared part files
  • extension changed from .gval.dart to .g.dart

0.5.0 #

  • change generated class prefix from $_ to _$ (private).

0.4.0 #

  • add fields option to GenValidator as alternative to annotating the model.

0.3.2 #

  • fix annotation inconsistencies
  • more annotation tests and code coverage

0.3.1 #

  • solve dependency errors.

0.3.0 #

  • fix date validators
  • tests for every validator

0.2.0 #

  • Wrap property validators with a FieldValidator to enable type checking.

0.1.0 #

  • Alpha version

0.0.1 #

  • Initial version

Use this package as a library

1. Depend on it

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


dependencies:
  validations: ^0.8.5+1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

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

3. Import it

Now in your Dart code, you can use:


import 'package:validations/validations.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
30
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
70
Overall:
Weighted score of the above. [more]
59
Learn more about scoring.

We analyzed this package on Oct 21, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:validations/validations.dart.

Maintenance suggestions

The package description is too short. (-20 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and validations.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.2 <3.0.0
decimal ^0.3.5 0.3.5
intl >=0.15.8 <1.0.0 0.16.0
meta >=1.0.0 <2.0.0 1.1.7
validators ^2.0.0 2.0.0+1
Transitive dependencies
path 1.6.4
rational 0.3.6
Dev dependencies
build_runner any
coverage ^0.12.2
pedantic ^1.0.0
test ^1.0.0
test_coverage ^0.3.0
validations_generator ^0.8.4