must_be 0.2.0 copy "must_be: ^0.2.0" to clipboard
must_be: ^0.2.0 copied to clipboard

Validate your data similarly to a framework for checking values in tests.

Must be #

pub License: MIT must_be

Yes, you are not wrong, this is another validation package.

However more much idiomatic, for example, validate value using cascade syntax.

It's similar to a framework that checks values in tests.


At the moment there are common validations and data validation of Brazil(e.g: RG, CPF, CNH, CEP and others).

Set constraints in your data #

Validation of a value.


You can set what is the validation field name.

requireThat('', name: 'email').mustBeEmail()

But, what happens when the date violates the restriction? An exception is thrown with the violate constraint with a message containing the data and field name if have.


But if I want to define more constraints? #

That Dart side is with us, so let's use the cascade operator syntax.


That's amazing.

Create your constraint #

Here again Dart side will help us with the extension methodsextension_dart.

Firstly, create a Constraint subclass, e.g:

class SameDay extends Constraint {
  const SameDay(, [String? message]) : super(message);

  final DateTime date;

Now, create an extension of the Require with the generic of the data type that to want to validate, call the method how you want, (the pattern is mustBe...), and inside it calls the mustBe` method.

extension RequireString<T extends String> on Require<T?> {
 /// Verifies if the [String] is blank.
  void mustBeBlank([String? message]) =>
      mustBe(value?.trimLeft().isEmpty ?? true, Blank(message));

In Require you can access the value and the field name(value and name).

How validate? #

When a restriction is violated an exception is thrown, then there is a support function that captures this and returns its message, if there is no violation then returns null.

    () => requireThat(10).mustBeGreaterThan(10),
    ifNotValid: (_) => 'error',
) // print -> 'error'

You can also do whatever you want with each restriction that is violated.

    () => requireThat(10)..mustBeGreaterThan(10)..mustBeLessThan(100),
     ifNotValid: (constraint) => switch (constraint.constraintViolation) {
        GreaterThanOrEqualTo(min: final min) =>
          'name must have at least $min characters',
        LessThan(max: final max) => 'name must be up to $max characters',
        _ => null,

You can use try/catch too.

For the customize you message see below Custom error message.

It's perfect to use in the TextFormFields validation.

    validator: (value) => validate(
        () => requireThat(value, name: 'nick')
    decoration: const InputDecoration(
        labelText: 'Nickname',
        border: OutlineInputBorder(),

Custom error message #

If you want to change the constraint default message when calling the validate function, use the ValidationsLocalization.

void main() {
    ValidationsLocalization.on<Email>((constraint) => 'custom message - ${}-${constraintException.value}');

This does not translate or change the constraint exception message, for this do:

requireThat('value').mustBeGreaterThan(5, 'custom message');

In the validate function there is ifNotValid function then pass the message do you want according to the constraint violated.

    () => requireThat(10).mustBeGreaterThan(0),
    ifNotValid: (constraint) => constraint.when({
        isType<LessThan>(): (_) => 'less',
        isType<GreaterThan>(): (_) => 'greater',

Tests #

There is a new matcher for the throwing ConstraintException:

    () => requireThat(2, name: 'number').mustBeGreaterThan(2),
        constraintMessage: 'Must be greater than 2',
        name: 'number',
        value: 2,

📝 Maintainers #

Kauê Martins

🤝 Support #

Did you like this package? Then give it a ⭐️. If you want to help then:

  • Fork this repository
  • Send a Pull Request with new features
  • Share this package
  • Create issues if you find a bug or want to suggest a new extension

Pull Request title follows Conventional Commits.

📝 License #

Copyright © 2023 Kauê Martins.
This project is MIT licensed.

pub points



Validate your data similarly to a framework for checking values in tests.

Repository (GitHub)
View/report issues


API reference




meta, test


Packages that depend on must_be