Git Product home page Git Product logo

must_be's Introduction

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)

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('[email protected]').mustBeEmail()

You can set what is the validation field name.

requireThat('[email protected]', 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.

requireThat('[email protected]').mustBeEmail('error')

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)

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;
}

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.

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

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,
    },),
)

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(),
    ),
),

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}');
}

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.

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

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,
    ),
);

๐Ÿ“ 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.

must_be's People

Contributors

kmartins avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.