diff --git a/lib/src/exceptions/validation_err.dart b/lib/src/exceptions/validation_err.dart index 6de5ce8..6d64641 100644 --- a/lib/src/exceptions/validation_err.dart +++ b/lib/src/exceptions/validation_err.dart @@ -13,14 +13,31 @@ class LValidationException extends LaravelException { /// contains the failed input keys in the exception object List get keys => _errors.keys.toList(); - String get firstErrorKey => keys.first; + List errors() { + List> errorsList = _errors.values.cast>().toList(); + errorsList.removeWhere((element) => element.isEmpty); + List errors = []; + for (List element in errorsList) { + if (element.length == 1) { + errors.add(element.first); + continue; + } + + errors.add(element.join("\n")); + } + return errors; + } - String? get firstErrorMessage => errorsByKey(firstErrorKey)?.first; + String get firstErrorKey => keys.first; List get firstErrorMessages => errorsByKey(firstErrorKey)!; List? errorsByKey(String key) => _errors[key]?.cast(); + String? errorsCombinedByKey(String key) => _errors[key]?.cast().join("\n"); + + String get errorsCombined => errors().join("\n"); + @override List get props => [ message, @@ -49,8 +66,7 @@ class LValidationException extends LaravelException { final isLastMsg = i == _errors.keys.length; /// append the message - errorBuffer - .write('$currentFelid $currentMessage${isLastMsg ? '\n' : ''}'); + errorBuffer.write('$currentFelid $currentMessage${isLastMsg ? '\n' : ''}'); } /// append the message diff --git a/test/laravel_validation_test.dart b/test/laravel_validation_test.dart index ed5fc62..3a53922 100644 --- a/test/laravel_validation_test.dart +++ b/test/laravel_validation_test.dart @@ -9,6 +9,7 @@ const resData = { ], 'calling_code': [ 'The calling code may not be greater than 4 characters.', + 'The dial code is invalid.', ], 'mobile': [ 'The mobile may not be greater than 9 characters.', @@ -30,8 +31,13 @@ void main() async { equals('website'), ); expect( - exception.firstErrorMessage, - equals('The website format is invalid.'), + exception.errorsCombined, + equals( + """The website format is invalid.\n""" + """The calling code may not be greater than 4 characters.\n""" + """The dial code is invalid.\n""" + """The mobile may not be greater than 9 characters.""", + ), ); expect( exception.message, @@ -46,6 +52,21 @@ void main() async { equals(['The website format is invalid.']), ); + expect( + exception.errorsCombinedByKey("calling_code"), + equals( + """The calling code may not be greater than 4 characters.\n""" + """The dial code is invalid.""", + ), + ); + + expect( + exception.errors().length, + equals( + exception.keys.length, + ), + ); + expect( exception.errorsByKey('success_key'), equals(null),