2.0.1
This commit is contained in:
parent
161269bf82
commit
7d2b670231
6 changed files with 62 additions and 32 deletions
|
@ -1,3 +1,6 @@
|
|||
# 2.0.1
|
||||
* Patch for updated body parsing.
|
||||
|
||||
# 2.0.0
|
||||
* Finish update for Angel 2.
|
||||
|
||||
|
|
12
README.md
12
README.md
|
@ -247,8 +247,8 @@ a `Validator` instance to the constructor, because it extends the
|
|||
```dart
|
||||
main() {
|
||||
var bio = new Validator({
|
||||
'age*': [isInteger, greaterThanOrEqualTo(0)],
|
||||
'birthYear*': isInteger,
|
||||
'age*': [isInt, greaterThanOrEqualTo(0)],
|
||||
'birthYear*': isInt,
|
||||
'countryOfOrigin': isString
|
||||
});
|
||||
|
||||
|
@ -277,12 +277,12 @@ main() {
|
|||
# Use with Angel
|
||||
|
||||
`server.dart` exposes seven helper middleware:
|
||||
* `validate(validator)`: Validates and filters `req.body`, and throws an `AngelHttpException.BadRequest` if data is invalid.
|
||||
* `validate(validator)`: Validates and filters `req.bodyAsMap`, and throws an `AngelHttpException.BadRequest` if data is invalid.
|
||||
* `validateEvent(validator)`: Sets `e.data` to the result of validation on a service event.
|
||||
* `validateQuery(validator)`: Same as `validate`, but operates on `req.query`.
|
||||
* `autoParseBody(fields)`: Auto-parses numbers in `req.body`.
|
||||
* `autoParseBody(fields)`: Auto-parses numbers in `req.bodyAsMap`.
|
||||
* `autoParseQuery(fields)`: Same as `autoParseBody`, but operates on `req.query`.
|
||||
* `filterBody(only)`: Filters unwanted data out of `req.body`.
|
||||
* `filterBody(only)`: Filters unwanted data out of `req.bodyAsMap`.
|
||||
* `filterQuery(only)`: Same as `filterBody`, but operates on `req.query`.
|
||||
|
||||
```dart
|
||||
|
@ -304,7 +304,7 @@ main() async {
|
|||
var app = new Angel();
|
||||
|
||||
app.chain(validate(echo)).post('/echo', (req, res) async {
|
||||
res.write('You said: "${req.body["message"]}"');
|
||||
res.write('You said: "${req.bodyAsMap["message"]}"');
|
||||
});
|
||||
|
||||
app.service('api/todos')
|
||||
|
|
29
example/main.dart
Normal file
29
example/main.dart
Normal file
|
@ -0,0 +1,29 @@
|
|||
import 'package:angel_validate/angel_validate.dart';
|
||||
|
||||
main() {
|
||||
var bio = new Validator({
|
||||
'age*': [isInt, greaterThanOrEqualTo(0)],
|
||||
'birthYear*': isInt,
|
||||
'countryOfOrigin': isString
|
||||
});
|
||||
|
||||
var book = new Validator({
|
||||
'title*': isString,
|
||||
'year*': [
|
||||
isNum,
|
||||
(year) {
|
||||
return year <= new DateTime.now().year;
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
var author = new Validator({
|
||||
'bio*': bio,
|
||||
'books*': [
|
||||
isList,
|
||||
everyElement(book)
|
||||
]
|
||||
}, defaultValues: {
|
||||
'books': []
|
||||
});
|
||||
}
|
|
@ -9,62 +9,60 @@ import 'angel_validate.dart';
|
|||
export 'src/async.dart';
|
||||
export 'angel_validate.dart';
|
||||
|
||||
/// Auto-parses numbers in `req.body`.
|
||||
/// Auto-parses numbers in `req.bodyAsMap`.
|
||||
RequestHandler autoParseBody(List<String> fields) {
|
||||
return (RequestContext req, res) async {
|
||||
var body = await req.parseBody();
|
||||
body.addAll(autoParse(body, fields));
|
||||
await req.parseBody();
|
||||
req.bodyAsMap.addAll(autoParse(req.bodyAsMap, fields));
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
||||
/// Auto-parses numbers in `req.query`.
|
||||
/// Auto-parses numbers in `req.queryParameters`.
|
||||
RequestHandler autoParseQuery(List<String> fields) {
|
||||
return (RequestContext req, res) async {
|
||||
var query = new Map<String, dynamic>.from(await req.parseQuery());
|
||||
(await req.parseQuery()).addAll(autoParse(query, fields));
|
||||
req.queryParameters.addAll(autoParse(req.queryParameters, fields));
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
||||
/// Filters unwanted data out of `req.body`.
|
||||
/// Filters unwanted data out of `req.bodyAsMap`.
|
||||
RequestHandler filterBody(Iterable<String> only) {
|
||||
return (RequestContext req, res) async {
|
||||
var body = await req.parseBody();
|
||||
var filtered = filter(body, only);
|
||||
body
|
||||
await req.parseBody();
|
||||
var filtered = filter(req.bodyAsMap, only);
|
||||
req.bodyAsMap
|
||||
..clear()
|
||||
..addAll(filtered);
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
||||
/// Filters unwanted data out of `req.query`.
|
||||
/// Filters unwanted data out of `req.queryParameters`.
|
||||
RequestHandler filterQuery(Iterable<String> only) {
|
||||
return (RequestContext req, res) async {
|
||||
var query = await req.parseQuery();
|
||||
var filtered = filter(query, only);
|
||||
(await req.parseQuery())
|
||||
var filtered = filter(req.queryParameters, only);
|
||||
req.queryParameters
|
||||
..clear()
|
||||
..addAll(filtered);
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
||||
/// Validates the data in `req.body`, and sets the body to
|
||||
/// Validates the data in `req.bodyAsMap`, and sets the body to
|
||||
/// filtered data before continuing the response.
|
||||
RequestHandler validate(Validator validator,
|
||||
{String errorMessage: 'Invalid data.'}) {
|
||||
return (RequestContext req, res) async {
|
||||
var body = await req.parseBody();
|
||||
var result = await asyncApplyValidator(validator, body, req.app);
|
||||
await req.parseBody();
|
||||
var result = await asyncApplyValidator(validator, req.bodyAsMap, req.app);
|
||||
|
||||
if (result.errors.isNotEmpty) {
|
||||
throw new AngelHttpException.badRequest(
|
||||
message: errorMessage, errors: result.errors);
|
||||
}
|
||||
|
||||
body
|
||||
req.bodyAsMap
|
||||
..clear()
|
||||
..addAll(result.data);
|
||||
|
||||
|
@ -72,20 +70,20 @@ RequestHandler validate(Validator validator,
|
|||
};
|
||||
}
|
||||
|
||||
/// Validates the data in `req.query`, and sets the query to
|
||||
/// Validates the data in `req.queryParameters`, and sets the query to
|
||||
/// filtered data before continuing the response.
|
||||
RequestHandler validateQuery(Validator validator,
|
||||
{String errorMessage: 'Invalid data.'}) {
|
||||
return (RequestContext req, res) async {
|
||||
var query = await req.parseQuery();
|
||||
var result = await asyncApplyValidator(validator, query, req.app);
|
||||
var result =
|
||||
await asyncApplyValidator(validator, req.queryParameters, req.app);
|
||||
|
||||
if (result.errors.isNotEmpty) {
|
||||
throw new AngelHttpException.badRequest(
|
||||
message: errorMessage, errors: result.errors);
|
||||
}
|
||||
|
||||
(await req.parseQuery())
|
||||
req.queryParameters
|
||||
..clear()
|
||||
..addAll(result.data);
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
name: angel_validate
|
||||
description: Cross-platform validation library based on `matcher`.
|
||||
version: 2.0.0
|
||||
description: Cross-platform request body validation library based on `matcher`.
|
||||
version: 2.0.1
|
||||
author: Tobe O <thosakwe@gmail.com>
|
||||
homepage: https://github.com/angel-dart/validate
|
||||
environment:
|
||||
|
|
|
@ -26,8 +26,8 @@ main() {
|
|||
|
||||
app.chain([validate(echoSchema)]).post('/echo',
|
||||
(RequestContext req, res) async {
|
||||
var body = await req.parseBody();
|
||||
res.write('Hello, ${body['message']}!');
|
||||
await req.parseBody();
|
||||
res.write('Hello, ${req.bodyAsMap['message']}!');
|
||||
});
|
||||
|
||||
app.logger = new Logger('angel')..onRecord.listen(printRecord);
|
||||
|
|
Loading…
Reference in a new issue