1.0.1
This commit is contained in:
parent
94921207e0
commit
070c22555f
6 changed files with 33 additions and 18 deletions
|
@ -1,5 +1,5 @@
|
||||||
# validate
|
# 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)
|
[![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)
|
[Live Example](https://angel-dart.github.io/validate)
|
||||||
|
|
|
@ -8,7 +8,7 @@ export 'angel_validate.dart';
|
||||||
/// Auto-parses numbers in `req.body`.
|
/// Auto-parses numbers in `req.body`.
|
||||||
RequestMiddleware autoParseBody(List<String> fields) {
|
RequestMiddleware autoParseBody(List<String> fields) {
|
||||||
return (RequestContext req, res) async {
|
return (RequestContext req, res) async {
|
||||||
req.body.addAll(autoParse(req.body, fields));
|
(await req.lazyBody()).addAll(autoParse(req.body, fields));
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@ RequestMiddleware autoParseBody(List<String> fields) {
|
||||||
/// Auto-parses numbers in `req.query`.
|
/// Auto-parses numbers in `req.query`.
|
||||||
RequestMiddleware autoParseQuery(List<String> fields) {
|
RequestMiddleware autoParseQuery(List<String> fields) {
|
||||||
return (RequestContext req, res) async {
|
return (RequestContext req, res) async {
|
||||||
req.query.addAll(autoParse(req.query, fields));
|
(await req.lazyQuery()).addAll(autoParse(req.query, fields));
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ RequestMiddleware autoParseQuery(List<String> fields) {
|
||||||
/// Filters unwanted data out of `req.body`.
|
/// Filters unwanted data out of `req.body`.
|
||||||
RequestMiddleware filterBody(Iterable<String> only) {
|
RequestMiddleware filterBody(Iterable<String> only) {
|
||||||
return (RequestContext req, res) async {
|
return (RequestContext req, res) async {
|
||||||
var filtered = filter(req.body, only);
|
var filtered = filter(await req.lazyBody(), only);
|
||||||
req.body
|
req.body
|
||||||
..clear()
|
..clear()
|
||||||
..addAll(filtered);
|
..addAll(filtered);
|
||||||
|
@ -35,7 +35,7 @@ RequestMiddleware filterBody(Iterable<String> only) {
|
||||||
/// Filters unwanted data out of `req.query`.
|
/// Filters unwanted data out of `req.query`.
|
||||||
RequestMiddleware filterQuery(Iterable<String> only) {
|
RequestMiddleware filterQuery(Iterable<String> only) {
|
||||||
return (RequestContext req, res) async {
|
return (RequestContext req, res) async {
|
||||||
var filtered = filter(req.query, only);
|
var filtered = filter(await req.lazyQuery(), only);
|
||||||
req.query
|
req.query
|
||||||
..clear()
|
..clear()
|
||||||
..addAll(filtered);
|
..addAll(filtered);
|
||||||
|
@ -51,7 +51,7 @@ RequestMiddleware validate(Validator validator,
|
||||||
var result = validator.check(req.body);
|
var result = validator.check(req.body);
|
||||||
|
|
||||||
if (result.errors.isNotEmpty) {
|
if (result.errors.isNotEmpty) {
|
||||||
throw new AngelHttpException.BadRequest(
|
throw new AngelHttpException.badRequest(
|
||||||
message: errorMessage, errors: result.errors);
|
message: errorMessage, errors: result.errors);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,7 +71,7 @@ RequestMiddleware validateQuery(Validator validator,
|
||||||
var result = validator.check(req.query);
|
var result = validator.check(req.query);
|
||||||
|
|
||||||
if (result.errors.isNotEmpty) {
|
if (result.errors.isNotEmpty) {
|
||||||
throw new AngelHttpException.BadRequest(
|
throw new AngelHttpException.badRequest(
|
||||||
message: errorMessage, errors: result.errors);
|
message: errorMessage, errors: result.errors);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,7 +91,7 @@ HookedServiceEventListener validateEvent(Validator validator,
|
||||||
var result = validator.check(e.data);
|
var result = validator.check(e.data);
|
||||||
|
|
||||||
if (result.errors.isNotEmpty) {
|
if (result.errors.isNotEmpty) {
|
||||||
throw new AngelHttpException.BadRequest(
|
throw new AngelHttpException.badRequest(
|
||||||
message: errorMessage, errors: result.errors);
|
message: errorMessage, errors: result.errors);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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 _alphaNum = new RegExp(r'^[A-Za-z0-9]+$');
|
||||||
final RegExp _email = new RegExp(
|
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])?$");
|
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.
|
/// Asserts that a `String` is alphanumeric, but also lets it contain dashes or underscores.
|
||||||
final Matcher isAlphaDash = predicate(
|
final Matcher isAlphaDash = predicate(
|
||||||
(value) => value is String && _alphaDash.hasMatch(value),
|
(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.
|
/// Asserts that a `String` is alphanumeric, but also lets it contain dashes or underscores.
|
||||||
final Matcher isAlphaNum = predicate(
|
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`.
|
/// 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.
|
/// Asserts that a `String` complies to the RFC 5322 e-mail standard.
|
||||||
final Matcher isEmail = predicate(
|
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`.
|
/// 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`.
|
/// 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`.
|
/// 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://');
|
||||||
|
|
|
@ -22,7 +22,9 @@ Map<String, dynamic> autoParse(Map inputData, Iterable<String> fields) {
|
||||||
data[key] = inputData[key];
|
data[key] = inputData[key];
|
||||||
} else {
|
} else {
|
||||||
try {
|
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;
|
data[key] = n == n.toInt() ? n.toInt() : n;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// Invalid number, don't pass it
|
// Invalid number, don't pass it
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
name: angel_validate
|
name: angel_validate
|
||||||
description: Cross-platform validation library based on `matcher`.
|
description: Cross-platform validation library based on `matcher`.
|
||||||
version: 1.0.0
|
version: 1.0.1
|
||||||
author: Tobe O <thosakwe@gmail.com>
|
author: Tobe O <thosakwe@gmail.com>
|
||||||
homepage: https://github.com/angel-dart/validate
|
homepage: https://github.com/angel-dart/validate
|
||||||
environment:
|
environment:
|
||||||
|
|
|
@ -19,7 +19,8 @@ main() {
|
||||||
res.write('Hello, ${req.body['message']}!');
|
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 {
|
tearDown(() async {
|
||||||
|
|
Loading…
Reference in a new issue