2016-12-25 19:40:36 +00:00
|
|
|
# validate
|
2019-09-27 13:30:39 +00:00
|
|
|
[![Pub](https://img.shields.io/pub/v/angel_validate.svg)](https://pub.dartlang.org/packages/angel_validate)
|
2016-12-25 21:26:55 +00:00
|
|
|
[![build status](https://travis-ci.org/angel-dart/validate.svg)](https://travis-ci.org/angel-dart/validate)
|
|
|
|
|
2019-10-17 01:58:24 +00:00
|
|
|
Strongly-typed form handlers and validators for Angel.
|
2016-12-26 01:09:24 +00:00
|
|
|
|
2016-12-25 21:26:55 +00:00
|
|
|
Validation library based on the `matcher` library, with Angel support.
|
|
|
|
Why re-invent the wheel, when you can use the same validators you already
|
|
|
|
use for tests?
|
|
|
|
|
2016-12-26 01:09:24 +00:00
|
|
|
For convenience's sake, this library also exports `matcher`.
|
|
|
|
|
2019-10-17 01:58:24 +00:00
|
|
|
# Field
|
|
|
|
The basic unit is the `Field` class, which is a type-safe way to read
|
|
|
|
values from a `RequestContext`. Here is a simple example of using a
|
|
|
|
`TextField` instance to read a value from the URL query parameters:
|
2016-12-25 21:26:55 +00:00
|
|
|
|
|
|
|
```dart
|
2019-10-17 01:58:24 +00:00
|
|
|
app.get('/hello', (req, res) async {
|
|
|
|
var nameField = TextField('name');
|
|
|
|
var name = await nameField.getValue(req, query: true); // String
|
|
|
|
return 'Hello, $name!';
|
2016-12-25 21:26:55 +00:00
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2019-10-17 01:58:24 +00:00
|
|
|
There are several included field types:
|
|
|
|
* `TextField`
|
|
|
|
* `BoolField`
|
|
|
|
* `NumField`
|
|
|
|
* `DoubleField`
|
|
|
|
* `IntField`
|
|
|
|
* `DateTimeField`
|
|
|
|
* `FileField`
|
|
|
|
* `ImageField`
|
|
|
|
|
|
|
|
# Forms
|
|
|
|
The `Form` class lets you combine `Field` instances, and decode
|
|
|
|
request bodies into `Map<String, dynamic>`. Unrecognized fields are
|
|
|
|
stripped out of the body, so a `Form` is effectively a whitelist.
|
2016-12-25 21:26:55 +00:00
|
|
|
|
|
|
|
```dart
|
2019-10-17 01:58:24 +00:00
|
|
|
var todoForm = Form(fields: [
|
|
|
|
TextField('text'),
|
|
|
|
BoolField('is_complete'),
|
|
|
|
]);
|
2016-12-26 13:04:42 +00:00
|
|
|
|
2019-10-17 01:58:24 +00:00
|
|
|
// Validate a request body, and deserialize it immediately.
|
|
|
|
var todo = await todoForm.deserialize(req, TodoSerializer.fromMap);
|
2016-12-26 13:04:42 +00:00
|
|
|
|
2019-10-17 01:58:24 +00:00
|
|
|
// Same as above, but with a Codec<Todo, Map> (i.e. via `angel_serialize`).
|
|
|
|
var todo = await todoForm.decode(req, todoSerializer);
|
2016-12-26 13:04:42 +00:00
|
|
|
|
2019-10-17 01:58:24 +00:00
|
|
|
// Lower-level functionality, typically not called directly.
|
|
|
|
// Use it if you want to handle validation errors directly, without
|
|
|
|
// throwing exceptions.
|
2016-12-26 13:04:42 +00:00
|
|
|
|
2019-10-17 01:58:24 +00:00
|
|
|
@serializable
|
|
|
|
class _Todo {
|
|
|
|
String text;
|
|
|
|
bool isComplete;
|
2016-12-26 13:04:42 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2019-10-17 01:58:24 +00:00
|
|
|
## Form Rendering
|
|
|
|
TODO: Docs about this
|
2016-12-25 21:26:55 +00:00
|
|
|
|
|
|
|
# Bundled Matchers
|
|
|
|
This library includes some `Matcher`s for common validations,
|
|
|
|
including:
|
|
|
|
|
2016-12-26 13:04:42 +00:00
|
|
|
* `isAlphaDash`: Asserts that a `String` is alphanumeric, but also lets it contain dashes or underscores.
|
|
|
|
* `isAlphaNum`: Asserts that a `String` is alphanumeric.
|
2016-12-25 21:26:55 +00:00
|
|
|
* `isBool`: Asserts that a value either equals `true` or `false`.
|
2016-12-26 13:04:42 +00:00
|
|
|
* `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`.
|
2016-12-25 21:26:55 +00:00
|
|
|
* `isString`: Asserts that a value is a `String`.
|
2017-04-26 12:49:26 +00:00
|
|
|
* `isNonEmptyString`: Asserts that a value is a non-empty `String`.
|
2017-04-01 22:34:06 +00:00
|
|
|
* `isUrl`: Asserts that a `String` is an HTTPS or HTTP URL.
|
2016-12-25 21:26:55 +00:00
|
|
|
|
|
|
|
The remaining functionality is
|
2019-10-17 01:58:24 +00:00
|
|
|
[effectively implemented by the `matcher` package](https://www.dartdocs.org/documentation/matcher/latest/matcher/matcher-library.html).
|