validate

Pubbuild status

Strongly-typed form handlers and validators for Angel.

Validation library based on the matcher library, with Angel support.Why re-invent the wheel, when you can use the same validators you alreadyuse for tests?

For convenience's sake, this library also exports matcher.

Field

The basic unit is the Field class, which is a type-safe way to readvalues from a RequestContext. Here is a simple example of using aTextField instance to read a value from the URL query parameters:

  1. app.get('/hello', (req, res) async {
  2. var nameField = TextField('name');
  3. var name = await nameField.getValue(req, query: true); // String
  4. return 'Hello, $name!';
  5. });

There are several included field types:

  • TextField
  • BoolField
  • NumField
  • DoubleField
  • IntField
  • DateTimeField
  • FileField
  • ImageField

Forms

The Form class lets you combine Field instances, and decoderequest bodies into Map<String, dynamic>. Unrecognized fields arestripped out of the body, so a Form is effectively a whitelist.

  1. var todoForm = Form(fields: [
  2. TextField('text'),
  3. BoolField('is_complete'),
  4. ]);
  5.  
  6. // Validate a request body, and deserialize it immediately.
  7. var todo = await todoForm.deserialize(req, TodoSerializer.fromMap);
  8.  
  9. // Same as above, but with a Codec<Todo, Map> (i.e. via `angel_serialize`).
  10. var todo = await todoForm.decode(req, todoSerializer);
  11.  
  12. // Lower-level functionality, typically not called directly.
  13. // Use it if you want to handle validation errors directly, without
  14. // throwing exceptions.
  15.  
  16. @serializable
  17. class _Todo {
  18. String text;
  19. bool isComplete;
  20. }

Form Rendering

TODO: Docs about this

Bundled Matchers

This library includes some Matchers for common validations,including:

  • isAlphaDash: Asserts that a String is alphanumeric, but also lets it contain dashes or underscores.
  • isAlphaNum: Asserts that a String is alphanumeric.
  • isBool: Asserts that a value either equals true or false.
  • isEmail: Asserts that a String complies to the RFC 5322 e-mail standard.
  • isInt: Asserts that a value is an int.
  • isNum: Asserts that a value is a num.
  • isString: Asserts that a value is a String.
  • isNonEmptyString: Asserts that a value is a non-empty String.
  • isUrl: Asserts that a String is an HTTPS or HTTP URL.

The remaining functionality iseffectively implemented by the matcher package.