angel2
This commit is contained in:
parent
95106b6271
commit
8bf246e564
7 changed files with 28 additions and 23 deletions
|
@ -1,3 +1,6 @@
|
|||
# 2.0.0-alpha.1
|
||||
* Update for Angel 2.
|
||||
|
||||
# 1.0.5-beta
|
||||
* Use `wrapMatcher` on explicit values instead of throwing.
|
||||
* Add async matchers.
|
||||
|
|
|
@ -12,7 +12,7 @@ export 'angel_validate.dart';
|
|||
/// Auto-parses numbers in `req.body`.
|
||||
RequestHandler autoParseBody(List<String> fields) {
|
||||
return (RequestContext req, res) async {
|
||||
(await req.lazyBody()).addAll(autoParse(req.body, fields));
|
||||
(await req.parseBody()).addAll(autoParse(await req.parseBody(), fields));
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ RequestHandler autoParseBody(List<String> fields) {
|
|||
/// Auto-parses numbers in `req.query`.
|
||||
RequestHandler autoParseQuery(List<String> fields) {
|
||||
return (RequestContext req, res) async {
|
||||
req.query.addAll(autoParse(req.query, fields));
|
||||
(await req.parseQuery()).addAll(autoParse(await req.parseQuery(), fields));
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
@ -28,8 +28,8 @@ RequestHandler autoParseQuery(List<String> fields) {
|
|||
/// Filters unwanted data out of `req.body`.
|
||||
RequestHandler filterBody(Iterable<String> only) {
|
||||
return (RequestContext req, res) async {
|
||||
var filtered = filter(await req.lazyBody(), only);
|
||||
req.body
|
||||
var filtered = filter(await req.parseBody(), only);
|
||||
(await req.parseBody())
|
||||
..clear()
|
||||
..addAll(filtered);
|
||||
return true;
|
||||
|
@ -39,8 +39,9 @@ RequestHandler filterBody(Iterable<String> only) {
|
|||
/// Filters unwanted data out of `req.query`.
|
||||
RequestHandler filterQuery(Iterable<String> only) {
|
||||
return (RequestContext req, res) async {
|
||||
var filtered = filter(req.query, only);
|
||||
req.query
|
||||
var query = await req.parseQuery();
|
||||
var filtered = filter(query, only);
|
||||
query
|
||||
..clear()
|
||||
..addAll(filtered);
|
||||
return true;
|
||||
|
@ -60,7 +61,7 @@ RequestHandler validate(Validator validator,
|
|||
message: errorMessage, errors: result.errors);
|
||||
}
|
||||
|
||||
req.body
|
||||
(await req.parseBody())
|
||||
..clear()
|
||||
..addAll(result.data);
|
||||
|
||||
|
@ -73,14 +74,15 @@ RequestHandler validate(Validator validator,
|
|||
RequestHandler validateQuery(Validator validator,
|
||||
{String errorMessage: 'Invalid data.'}) {
|
||||
return (RequestContext req, res) async {
|
||||
var result = await asyncApplyValidator(validator, await req.parseQuery(), req.app);
|
||||
var result =
|
||||
await asyncApplyValidator(validator, await req.parseQuery(), req.app);
|
||||
|
||||
if (result.errors.isNotEmpty) {
|
||||
throw new AngelHttpException.badRequest(
|
||||
message: errorMessage, errors: result.errors);
|
||||
}
|
||||
|
||||
req.query
|
||||
(await req.parseQuery())
|
||||
..clear()
|
||||
..addAll(result.data);
|
||||
|
||||
|
|
|
@ -60,7 +60,7 @@ AngelMatcher idExistsInService(String servicePath,
|
|||
return predicateWithAngel(
|
||||
(key, item, app) async {
|
||||
try {
|
||||
var result = await app.service(servicePath)?.read(item);
|
||||
var result = await app.findService(servicePath)?.read(item);
|
||||
return result != null;
|
||||
} on AngelHttpException catch (e) {
|
||||
if (e.statusCode == 404) {
|
||||
|
|
|
@ -12,5 +12,4 @@ class ContextValidator extends Matcher {
|
|||
|
||||
@override
|
||||
bool matches(item, Map matchState) => true;
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -362,9 +362,8 @@ class ValidationResult {
|
|||
/// This is empty if validation was successful.
|
||||
List<String> get errors => new List<String>.unmodifiable(_errors);
|
||||
|
||||
ValidationResult withData(Map<String, dynamic> data) => new ValidationResult()
|
||||
.._data.addAll(data)
|
||||
.._errors.addAll(_errors);
|
||||
ValidationResult withData(Map<String, dynamic> data) =>
|
||||
new ValidationResult().._data.addAll(data).._errors.addAll(_errors);
|
||||
|
||||
ValidationResult withErrors(Iterable<String> errors) =>
|
||||
new ValidationResult().._data.addAll(_data).._errors.addAll(errors);
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
name: angel_validate
|
||||
description: Cross-platform validation library based on `matcher`.
|
||||
version: 2.0.0-alpha
|
||||
version: 2.0.0-alpha.1
|
||||
author: Tobe O <thosakwe@gmail.com>
|
||||
homepage: https://github.com/angel-dart/validate
|
||||
environment:
|
||||
|
@ -10,7 +10,7 @@ dependencies:
|
|||
angel_http_exception: ^1.0.0
|
||||
matcher: ^0.12.0
|
||||
dev_dependencies:
|
||||
#angel_test: ^1.0.0-dev
|
||||
angel_test: ^2.0.0-alpha
|
||||
build_runner: ^0.10.0
|
||||
build_web_compilers: ^0.4.0
|
||||
logging: ^0.11.0
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import 'package:angel_framework/angel_framework.dart';
|
||||
import 'package:angel_framework/http.dart';
|
||||
import 'package:angel_test/angel_test.dart';
|
||||
import 'package:angel_validate/server.dart';
|
||||
import 'package:dart2_constant/convert.dart';
|
||||
|
@ -23,9 +24,10 @@ main() {
|
|||
app = new Angel();
|
||||
http = new AngelHttp(app, useZone: false);
|
||||
|
||||
app.chain(validate(echoSchema)).post('/echo',
|
||||
app.chain([validate(echoSchema)]).post('/echo',
|
||||
(RequestContext req, res) async {
|
||||
res.write('Hello, ${req.body['message']}!');
|
||||
var body = await req.parseBody();
|
||||
res.write('Hello, ${body['message']}!');
|
||||
});
|
||||
|
||||
app.logger = new Logger('angel')..onRecord.listen(printRecord);
|
||||
|
@ -50,10 +52,10 @@ main() {
|
|||
|
||||
test('enforce', () async {
|
||||
var rq = new MockHttpRequest('POST', new Uri(path: '/echo'))
|
||||
..headers.add('accept', '*/*')
|
||||
..headers.add('content-type', 'application/json')
|
||||
..write(json.encode({'foo': 'bar'}))
|
||||
..close();
|
||||
..headers.add('accept', '*/*')
|
||||
..headers.add('content-type', 'application/json')
|
||||
..write(json.encode({'foo': 'bar'}))
|
||||
..close();
|
||||
http.handleRequest(rq);
|
||||
|
||||
var responseBody = await rq.response.transform(utf8.decoder).join();
|
||||
|
|
Loading…
Reference in a new issue