Library for validate models similar to JPA Bean validation. It provides a way to validate a dart object using constraints which we can use for validation. To check if the object is valid we need to invoke the function validate
which returns an Object of type ValidationResult
which contains a map of errors for every attribute of the object, for example:
-
Create a new dart project
-
Add dependencies to
pubspec.yaml
...
dependencies:
...
drails_validator: ^0.1.0 #change it for the latest version
...
dev_dependencies:
...
link:example/pubspec.yaml[role=include]
...
-
Create/edit the file
main.dart
in the folderbin
and put next code on it:
link:example/bin/main.dart[role=include]
-
run
pub run build_runner build
. Then you will see that the filebin/drails_validator_sample.g.dart
has been generated and it will contains the next code:
link:example/bin/main.g.dart[role=include]
-
Finally you can run the file
bin/drails_validator_sample.dart
. If everything is ok you will see next output in console:
invalidPerson: isValid: false, errors: {firstName: [Length should be greather than 2], lastName: [Length should be greather than 2], email: [The entered email is invalid], dateOfBirth: [Values after now are not allowed], ssn: [The entered SSN is invalid]}
validPerson: isValid: true, errors: {}
-
install
l10n
package by running:
pub global activate l10n
-
generate translations by running:
mkl10n -l es . <path-to-drails_validator-package>
Warning
|
Remember to replace <path-to-drails_validator-package> by the correct path
|
-
translate
intl_es.arb
file using Google Translate Toolkit -
run again:
mkl10n -l es . <path-to-drails_validator-package>
-
import the generated
messages_all.dart
file in themain.dart
file:
link:example/bin/main.dart[role=include]
then set the default locale and initialize the messages by adding next code to main function:
main() {
...
link:example/bin/main.dart[role=include]
...
}
final code in main.dart
:
link:example/bin/main.dart[role=include]