From 070c22555f3c56aa46016babcc8e6bf2ba6b879e Mon Sep 17 00:00:00 2001 From: thosakwe Date: Tue, 28 Mar 2017 22:56:51 -0400 Subject: [PATCH] 1.0.1 --- README.md | 2 +- lib/server.dart | 14 +++++++------- lib/src/matchers.dart | 26 +++++++++++++++++++------- lib/src/validator.dart | 4 +++- pubspec.yaml | 2 +- test/server_test.dart | 3 ++- 6 files changed, 33 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 58feee1d..d5236a74 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # validate -[![version 1.0.0](https://img.shields.io/badge/pub-v1.0.0-brightgreen.svg)](https://pub.dartlang.org/packages/angel_validate) +[![version 1.0.1](https://img.shields.io/badge/pub-v1.0.1-brightgreen.svg)](https://pub.dartlang.org/packages/angel_validate) [![build status](https://travis-ci.org/angel-dart/validate.svg)](https://travis-ci.org/angel-dart/validate) [Live Example](https://angel-dart.github.io/validate) diff --git a/lib/server.dart b/lib/server.dart index fc5bf6ef..5fdbf0db 100644 --- a/lib/server.dart +++ b/lib/server.dart @@ -8,7 +8,7 @@ export 'angel_validate.dart'; /// Auto-parses numbers in `req.body`. RequestMiddleware autoParseBody(List fields) { return (RequestContext req, res) async { - req.body.addAll(autoParse(req.body, fields)); + (await req.lazyBody()).addAll(autoParse(req.body, fields)); return true; }; } @@ -16,7 +16,7 @@ RequestMiddleware autoParseBody(List fields) { /// Auto-parses numbers in `req.query`. RequestMiddleware autoParseQuery(List fields) { return (RequestContext req, res) async { - req.query.addAll(autoParse(req.query, fields)); + (await req.lazyQuery()).addAll(autoParse(req.query, fields)); return true; }; } @@ -24,7 +24,7 @@ RequestMiddleware autoParseQuery(List fields) { /// Filters unwanted data out of `req.body`. RequestMiddleware filterBody(Iterable only) { return (RequestContext req, res) async { - var filtered = filter(req.body, only); + var filtered = filter(await req.lazyBody(), only); req.body ..clear() ..addAll(filtered); @@ -35,7 +35,7 @@ RequestMiddleware filterBody(Iterable only) { /// Filters unwanted data out of `req.query`. RequestMiddleware filterQuery(Iterable only) { return (RequestContext req, res) async { - var filtered = filter(req.query, only); + var filtered = filter(await req.lazyQuery(), only); req.query ..clear() ..addAll(filtered); @@ -51,7 +51,7 @@ RequestMiddleware validate(Validator validator, var result = validator.check(req.body); if (result.errors.isNotEmpty) { - throw new AngelHttpException.BadRequest( + throw new AngelHttpException.badRequest( message: errorMessage, errors: result.errors); } @@ -71,7 +71,7 @@ RequestMiddleware validateQuery(Validator validator, var result = validator.check(req.query); if (result.errors.isNotEmpty) { - throw new AngelHttpException.BadRequest( + throw new AngelHttpException.badRequest( message: errorMessage, errors: result.errors); } @@ -91,7 +91,7 @@ HookedServiceEventListener validateEvent(Validator validator, var result = validator.check(e.data); if (result.errors.isNotEmpty) { - throw new AngelHttpException.BadRequest( + throw new AngelHttpException.badRequest( message: errorMessage, errors: result.errors); } diff --git a/lib/src/matchers.dart b/lib/src/matchers.dart index f4bd6a8d..a5a21f94 100644 --- a/lib/src/matchers.dart +++ b/lib/src/matchers.dart @@ -4,28 +4,40 @@ final RegExp _alphaDash = new RegExp(r'^[A-Za-z0-9_-]+$'); final RegExp _alphaNum = new RegExp(r'^[A-Za-z0-9]+$'); final RegExp _email = new RegExp( r"^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$"); +final RegExp _url = new RegExp( + r'https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)'); /// Asserts that a `String` is alphanumeric, but also lets it contain dashes or underscores. final Matcher isAlphaDash = predicate( (value) => value is String && _alphaDash.hasMatch(value), - 'alphanumeric (dashes and underscores are allowed) '); + 'alphanumeric (dashes and underscores are allowed)'); /// Asserts that a `String` is alphanumeric, but also lets it contain dashes or underscores. final Matcher isAlphaNum = predicate( - (value) => value is String && _alphaNum.hasMatch(value), 'alphanumeric '); + (value) => value is String && _alphaNum.hasMatch(value), 'alphanumeric'); /// Asserts that a value either equals `true` or `false`. -final Matcher isBool = predicate((value) => value is bool, 'a bool '); +final Matcher isBool = predicate((value) => value is bool, 'a bool'); /// Asserts that a `String` complies to the RFC 5322 e-mail standard. final Matcher isEmail = predicate( - (value) => value is String && _email.hasMatch(value), 'a valid e-mail '); + (value) => value is String && _email.hasMatch(value), 'a valid e-mail'); /// Asserts that a value is an `int`. -final Matcher isInt = predicate((value) => value is int, 'an integer '); +final Matcher isInt = predicate((value) => value is int, 'an integer'); /// Asserts that a value is a `num`. -final Matcher isNum = predicate((value) => value is num, 'a number '); +final Matcher isNum = predicate((value) => value is num, 'a number'); /// Asserts that a value is a `String`. -final Matcher isString = predicate((value) => value is String, 'a String '); +final Matcher isString = predicate((value) => value is String, 'a String'); + +/// Asserts that a `String` is an `http://` or `https://` URL. +/// +/// The regular expression used: +/// ``` +/// https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*) +/// ``` +final Matcher isurl = predicate( + (value) => value is String && _url.hasMatch(value), + 'a valid url, starting with http:// or https://'); diff --git a/lib/src/validator.dart b/lib/src/validator.dart index 12452425..5d0c1c22 100644 --- a/lib/src/validator.dart +++ b/lib/src/validator.dart @@ -22,7 +22,9 @@ Map autoParse(Map inputData, Iterable fields) { data[key] = inputData[key]; } else { try { - var n = num.parse(inputData[key].toString()); + var n = inputData[key] is num + ? inputData[key] + : num.parse(inputData[key].toString()); data[key] = n == n.toInt() ? n.toInt() : n; } catch (e) { // Invalid number, don't pass it diff --git a/pubspec.yaml b/pubspec.yaml index f6555622..e5d3b9a3 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: angel_validate description: Cross-platform validation library based on `matcher`. -version: 1.0.0 +version: 1.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 f056d3f6..15d24190 100644 --- a/test/server_test.dart +++ b/test/server_test.dart @@ -19,7 +19,8 @@ main() { res.write('Hello, ${req.body['message']}!'); }); - client = await connectTo(new DiagnosticsServer(app, new File('log.txt'))); + await app.configure(logRequests(new File('log.txt'))); + client = await connectTo(app); }); tearDown(() async {