fluent_validation_flutter 0.0.2 copy "fluent_validation_flutter: ^0.0.2" to clipboard
fluent_validation_flutter: ^0.0.2 copied to clipboard

discontinued
outdated

FluentValidationFlutter is strongly inspired by FluentValidation for C# and is intended to bring Dart a similar experience that allows you to validate common rules in entities in a simple way.

example/lib/main.dart

import 'package:fluent_validation_flutter/fluent_validation_flutter.dart';

void main() {
  var entity = Customer(id: 1, name: 'Tifa Lockhart', age: 25);
  if (entity.isValid()) {
    print('saved!');
  } else {
    print('sorry!');
  }
}

abstract class Entity<T> extends BaseValidator {}

class BaseValidator {
  List<String> _erros = new List<String>();
  List<String> get erros => _erros;
  BaseValidator();

  void validateNow<T>(AbstractValidator<T> validator, T instance) {
    validator.validate(instance);
    _erros = validator.allErros;
  }

  bool isValid() {
    if (_erros.length > 0) {
      return false;
    } else {
      return true;
    }
  }
}

class Customer extends Entity<Customer> {
  int id;
  String name;
  int age;

  factory Customer({int id, String name, int age}) {
    var customer = Customer._(id: id, name: name, age: age);
    customer.validateNow(CustomerValidator(), customer);
    return customer;
  }

  Customer._({this.id, this.name, this.age});

  Customer.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['name'];
    age = json['age'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['name'] = this.name;
    data['age'] = this.age;
    return data;
  }
}

class CustomerValidator extends AbstractValidator<Customer> {
  CustomerValidator() {
    ruleFor("id", (customer) => customer.id).notNull();
    ruleFor("name", (customer) => customer.name).notEmpty();
    ruleFor("age", (customer) => customer.age)
      ..greaterThan(18)
      ..withMessage("Custom Message");
  }
}
1
likes
0
pub points
0%
popularity

Publisher

unverified uploader

FluentValidationFlutter is strongly inspired by FluentValidation for C# and is intended to bring Dart a similar experience that allows you to validate common rules in entities in a simple way.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

flutter

More

Packages that depend on fluent_validation_flutter