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.

requireThat('value')..mustBeGreaterThan(6)..mustBeLessThanOrEqualTo(10)
copied to clipboard

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.

requireThat('teste@gmail.com').mustBeEmail()
copied to clipboard

You can set what is the validation field name.

requireThat('teste@gmail.com', name: 'email').mustBeEmail()
copied to clipboard

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.

requireThat('teste@gmail.com').mustBeEmail('error')
copied to clipboard

But if I want to define more constraints? #

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

requireThat('value')..mustBeGreaterThan(6)..mustBeLessThanOrEqualTo(10)
copied to clipboard

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(this.date, [String? message]) : super(message);

  final DateTime date;
}
copied to clipboard

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));
}
copied to clipboard

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.

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

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

validate(
    () => 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,
    },),
)
copied to clipboard

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.

TextFormField(
    validator: (value) => validate(
        () => requireThat(value, name: 'nick')
            ..mustBeGreaterThanOrEqualTo(3)
            ..mustBeLessThan(10),
    ),
    decoration: const InputDecoration(
        labelText: 'Nickname',
        border: OutlineInputBorder(),
    ),
),
copied to clipboard

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.name}-${constraintException.value}');
}
copied to clipboard

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

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

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

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

Tests #

There is a new matcher for the throwing ConstraintException:

expect(
    () => requireThat(2, name: 'number').mustBeGreaterThan(2),
    throwsConstraintException<GreaterThan>(
        constraintMessage: 'Must be greater than 2',
        name: 'number',
        value: 2,
    ),
);
copied to clipboard

📝 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.

3
likes
140
points
61
downloads

Publisher

verified publisherkmartins.dev

Weekly Downloads

2024.08.04 - 2025.02.16

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

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

meta, test

More

Packages that depend on must_be