Skip to content

Latest commit

 

History

History
103 lines (71 loc) · 2.62 KB

_README.adoc

File metadata and controls

103 lines (71 loc) · 2.62 KB

drails_validator

Build Status

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 folder bin 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 file bin/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: {}

Add Localization

  • 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
mkl10n -l es . <path-to-drails_validator-package>
  • import the generated messages_all.dart file in the main.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]

Edit the README file

Please don’t edit the README.md file since this one is generated by asciidoctor and pandoc. Instead edit _README.adoc and then run:

asciidoctor _README.adoc -b docbokk

and then:

pandoc -f docbook -t gfm -o README.md _README.adoc