From 0043114180537c49a325195f28b0084cc960e48c Mon Sep 17 00:00:00 2001 From: "thomashii@dukefirehawk.com" Date: Wed, 17 Aug 2022 20:15:23 +0800 Subject: [PATCH] Fixed validator analysis warnings --- packages/validate/lib/server.dart | 18 ++++++++---------- packages/validate/lib/src/validator.dart | 20 ++++++++++---------- packages/validate/web/main.dart | 4 ++-- 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/packages/validate/lib/server.dart b/packages/validate/lib/server.dart index 3d201b60..dea30ad6 100644 --- a/packages/validate/lib/server.dart +++ b/packages/validate/lib/server.dart @@ -101,18 +101,16 @@ HookedServiceEventListener validateEvent(Validator validator, {String errorMessage = 'Invalid data.'}) { return (HookedServiceEvent e) async { var app = e.request?.app ?? e.service.app; - if (app != null) { - var result = await asyncApplyValidator(validator, e.data as Map, app); + var result = await asyncApplyValidator(validator, e.data as Map, app); - if (result.errors.isNotEmpty) { - throw AngelHttpException.badRequest( - message: errorMessage, errors: result.errors); - } - - e.data - ..clear() - ..addAll(result.data); + if (result.errors.isNotEmpty) { + throw AngelHttpException.badRequest( + message: errorMessage, errors: result.errors); } + + e.data + ..clear() + ..addAll(result.data); }; } diff --git a/packages/validate/lib/src/validator.dart b/packages/validate/lib/src/validator.dart index 37afb723..9548c41f 100644 --- a/packages/validate/lib/src/validator.dart +++ b/packages/validate/lib/src/validator.dart @@ -79,7 +79,7 @@ class Validator extends Matcher { requiredFields.add(fieldName); } - var _iterable = + var tmpIterable = schema[keys] is Iterable ? schema[keys] : [schema[keys]]; var iterable = []; @@ -91,7 +91,7 @@ class Validator extends Matcher { } } - _iterable.forEach(_addTo); + tmpIterable.forEach(_addTo); for (var rule in iterable) { if (rule is Matcher) { @@ -271,7 +271,7 @@ class Validator extends Matcher { {Map defaultValues = const {}, Map customErrorMessages = const {}, bool overwrite = false}) { - var _schema = {}; + var tmpSchema = {}; var child = Validator.empty() ..defaultValues.addAll(this.defaultValues) ..defaultValues.addAll(defaultValues) @@ -307,10 +307,10 @@ class Validator extends Matcher { if (child.rules.containsKey(fieldName)) child.rules.remove(fieldName); } - _schema[fieldName] = schema[key]; + tmpSchema[fieldName] = schema[key]; } - return child.._importSchema(_schema); + return child.._importSchema(tmpSchema); } /// Adds a [rule]. @@ -383,14 +383,14 @@ class ValidationResult { /// Occurs when user-provided data is invalid. class ValidationException extends AngelHttpException { /// A list of errors that resulted in the given data being marked invalid. - @override - final List errors = []; + //@override + //final List errors = []; /// A descriptive message describing the error. - @override - final String message; + //@override + //final String message; - ValidationException(this.message, {Iterable errors = const []}) + ValidationException(String message, {Iterable errors = const []}) : super( message: message, statusCode: 400, diff --git a/packages/validate/web/main.dart b/packages/validate/web/main.dart index 4039d11b..94c474bc 100644 --- a/packages/validate/web/main.dart +++ b/packages/validate/web/main.dart @@ -35,9 +35,9 @@ void main() { var formData = {}; - ['firstName', 'lastName', 'age', 'familySize'].forEach((key) { + for (var key in ['firstName', 'lastName', 'age', 'familySize']) { formData[key] = (querySelector('[name="$key"]') as InputElement).value; - }); + } if ($blank!.value!.isNotEmpty) formData['blank'] = $blank!.value;