From 7d2b67023184508808ad6a3143a8e969e7896380 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Mon, 10 Dec 2018 12:51:10 -0500 Subject: [PATCH] 2.0.1 --- CHANGELOG.md | 3 +++ README.md | 12 ++++++------ example/main.dart | 29 +++++++++++++++++++++++++++++ lib/server.dart | 42 ++++++++++++++++++++---------------------- pubspec.yaml | 4 ++-- test/server_test.dart | 4 ++-- 6 files changed, 62 insertions(+), 32 deletions(-) create mode 100644 example/main.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index 22e0d51e..9cc02b51 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# 2.0.1 +* Patch for updated body parsing. + # 2.0.0 * Finish update for Angel 2. diff --git a/README.md b/README.md index 1f1f7933..51056633 100644 --- a/README.md +++ b/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') diff --git a/example/main.dart b/example/main.dart new file mode 100644 index 00000000..a94dfea9 --- /dev/null +++ b/example/main.dart @@ -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': [] + }); +} \ No newline at end of file diff --git a/lib/server.dart b/lib/server.dart index 2bd9151c..4e03bad5 100644 --- a/lib/server.dart +++ b/lib/server.dart @@ -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 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 fields) { return (RequestContext req, res) async { - var query = new Map.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 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 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); diff --git a/pubspec.yaml b/pubspec.yaml index 3bc9e2da..e3cbc0e9 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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 homepage: https://github.com/angel-dart/validate environment: diff --git a/test/server_test.dart b/test/server_test.dart index 1580b10b..0bf6451e 100644 --- a/test/server_test.dart +++ b/test/server_test.dart @@ -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);