Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#45): insert parsed value back to the context #46

Merged
merged 1 commit into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/serinus/bin/serinus.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,13 @@ class HomeController extends Controller {
on(
GetRoute(path: '/'),
(context) async {
return Response.json([TestObj('Hello'), TestObj('World')]);
return Response.json([TestObj('Hello'), TestObj('World'), {'test': context.query['test']}]);
},
schema: ParseSchema(
query: object({
'test': string().encode(),
}).optionals(['test'])
)
);
on(PostRoute(path: '/*'), (context) async {
return Response.text(
Expand Down
5 changes: 3 additions & 2 deletions packages/serinus/lib/src/core/parse_schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import '../exceptions/exceptions.dart';

/// The [ParseSchema] class is used to define the schema of the parsing process.
final class ParseSchema {
late AcanthisType _schema;
late AcanthisMap _schema;

/// The [error] property contains the error that will be thrown if the parsing fails.
final SerinusException Function(Map<String, dynamic>)? error;
Expand All @@ -29,7 +29,7 @@ final class ParseSchema {
}

/// The [tryParse] method is used to validate the data.
void tryParse({required Map<String, dynamic> value}) {
Map<String, dynamic> tryParse({required Map<String, dynamic> value}) {
AcanthisParseResult? result;
try {
result = _schema.tryParse(value);
Expand All @@ -40,5 +40,6 @@ final class ParseSchema {
throw error?.call(result.errors) ??
BadRequestException(message: jsonEncode(result.errors));
}
return result.value;
}
}
8 changes: 7 additions & 1 deletion packages/serinus/lib/src/handlers/request_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,19 @@ class RequestHandler extends Handler {
buildRequestContext(scopedProviders, wrappedRequest);
await route.transform(context);
if (schema != null) {
schema.tryParse(value: {
final result = schema.tryParse(value: {
'body': wrappedRequest.body?.value,
'query': wrappedRequest.query,
'params': wrappedRequest.params,
'headers': wrappedRequest.headers,
'session': wrappedRequest.session.all,
});
wrappedRequest.headers.addAll(result['headers']);
wrappedRequest.params.addAll(result['params']);
wrappedRequest.query.addAll(result['query']);
for(final key in result['session'].keys) {
wrappedRequest.session.put(key, result['session'][key]);
}
}
final middlewares = injectables.filterMiddlewaresByRoute(
routeData.path, wrappedRequest.params);
Expand Down
Loading