valify 0.0.2+2 valify: ^0.0.2+2 copied to clipboard
Validate your inputs like never before with highly intuitive constraint classes
valify #
✨ Easy to use pre-defined constraints covering most of your everyday validation needs.
🖍 Better still, you can create your own constraints on the go.
🏎 Set your own order in which constraints are evaluated.
🚜 Receive a list of constraints that got violated or receive the first one that was violated in the pipeline.
Overview
valify
works in an extremely simple manner.
- You have a list of constraints as part of your validation/constraints pipeline.
- The way you list defines the order in which those constraints are evaluated.
- Once you give it a run, at the end your pipeline you receive a list of constraints that were violated by your input.
- If no constraints were violated, you receive an empty list.
- Optionally, you can get access to only the first violation or you can simply check if any violation did occur at all.
Installation
To use valify
, you'd have to add the following dependency under your pubspec.yaml/depencencies
section.
# pubspec.yaml
dependencies:
valify: ^0.0.2+2
Let's get started.
Creating a valifying
pipeline
import 'package:valify/valifier.dart';
final valifier = Valifier(
constraints: [
MaxLengthLimitingConstraint(
maxLength: 64,
),
MinimumLengthRequiredConstraint(
minLength: 8,
),
UpperCaseCharactersRequiredConstraint(),
LowerCaseCharactersRequiredConstraint(),
DigitsRequiredConstraint(),
SpecialCharactersRequiredConstraint(
specialCharacters: ['@', '$', '-', '%'],
),
]
)
After having created the above valifer
object, it's very simple to validate any strings thereafter.
Validating userInput
using valifying
pipeline
// Let's assume user has entered something that is captured in the variable [userInput]
final violatedConstraints = valifier.allConstraintsViolatedOn(userInput);
if (violatedConstraints.isEmpty) {
// User didn't violate any constraints.
print('Awesome user. You did violate any constraints.');
} else {
// Some constraints were violated.
print('Uh oh! Some constraints were violated.');
violatedConstraints.forEach(
(violatedConstraint) => print(violatedConstraint.violationMessage),
);
}
- You may also just fetch the first violation that occurred on the given input by using
firstConstraintViolatedOn()
method of theValifier
. - Or you may even not go that far as you can easily toget to know if any constraints were violated at all by using
areAllConstraintsSatisfiedOn()
method of theValifier
.
___ NOTE ___ You can optionally pass-in a
violationMessage
parameter to any of your constraints. ThisviolationMessage
can be useful later on as shown above [print(violatedConstraint.violationMessage)
]
Want to contribute?
I'll be more than happy to receive your PR
at github.com/m-zaink/valify.