Skip to content
Open
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
24 changes: 20 additions & 4 deletions lib/src/exceptions/validation_err.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,31 @@ class LValidationException extends LaravelException {
/// contains the failed input keys in the exception object
List<String> get keys => _errors.keys.toList();

String get firstErrorKey => keys.first;
List<String> errors() {
List<List<String>> errorsList = _errors.values.cast<List<String>>().toList();
errorsList.removeWhere((element) => element.isEmpty);
List<String> errors = [];
for (List<String> 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<String> get firstErrorMessages => errorsByKey(firstErrorKey)!;

List<String>? errorsByKey(String key) => _errors[key]?.cast<String>();

String? errorsCombinedByKey(String key) => _errors[key]?.cast<String>().join("\n");

String get errorsCombined => errors().join("\n");

@override
List<Object?> get props => [
message,
Expand Down Expand Up @@ -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
Expand Down
25 changes: 23 additions & 2 deletions test/laravel_validation_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const resData = <String, dynamic>{
],
'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.',
Expand All @@ -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,
Expand All @@ -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),
Expand Down