This commit is contained in:
thosakwe 2017-03-28 22:56:51 -04:00
parent 94921207e0
commit 070c22555f
6 changed files with 33 additions and 18 deletions

View file

@ -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)

View file

@ -8,7 +8,7 @@ export 'angel_validate.dart';
/// Auto-parses numbers in `req.body`.
RequestMiddleware autoParseBody(List<String> 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<String> fields) {
/// Auto-parses numbers in `req.query`.
RequestMiddleware autoParseQuery(List<String> 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<String> fields) {
/// Filters unwanted data out of `req.body`.
RequestMiddleware filterBody(Iterable<String> 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<String> only) {
/// Filters unwanted data out of `req.query`.
RequestMiddleware filterQuery(Iterable<String> 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);
}

View file

@ -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://');

View file

@ -22,7 +22,9 @@ Map<String, dynamic> autoParse(Map inputData, Iterable<String> 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

View file

@ -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 <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/validate
environment:

View file

@ -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 {