Skip to content

Commit b34722b

Browse files
committed
fix_validation
1 parent 011f132 commit b34722b

File tree

3 files changed

+12
-26
lines changed

3 files changed

+12
-26
lines changed

lib/src/exceptions/validation_err.dart

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
import 'package:laravel_exception/src/exceptions/imp.dart';
22

33
class LValidationException extends LaravelException {
4-
final Map<String, List<String>> _errors;
4+
final Map<String, List> _errors;
55

66
LValidationException(
77
Map<String, dynamic> response,
8-
) : _errors = response['errors'] ?? response['error'],
9-
super(
10-
response: response,
11-
);
8+
) : _errors = Map<String, List>.from(response['errors']),
9+
super(response: response);
1210

1311
/// contains the failed input keys in the exception object
1412
List<String> get keys => _errors.keys.toList();
1513

1614
String get firstErrorKey => keys.first;
1715

18-
String get firstErrorMessage => _errors[firstErrorKey]!.first;
16+
String? get firstErrorMessage => errorsByKey(firstErrorKey)?.first;
1917

20-
List<String> get firstErrorMessages => _errors[firstErrorKey]!;
18+
List<String> get firstErrorMessages => errorsByKey(firstErrorKey)!;
2119

22-
List<String> errorsByKey(String key) => _errors[key] ?? [];
20+
List<String>? errorsByKey(String key) => _errors[key] as List<String>?;
2321

2422
@override
2523
List<Object?> get props => [
@@ -61,5 +59,5 @@ class LValidationException extends LaravelException {
6159
}
6260

6361
@override
64-
String get message => firstErrorMessage;
62+
String get message => response['message'] ?? firstErrorMessage!;
6563
}

pubspec.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
name: laravel_exception
22
description: parse laravel exceptions to dart classes for easier and better handling support 422 - validation ,500 server errors , 404 not found exceptions
3-
version: 0.0.2
3+
version: 0.0.3
44
homepage: https://github.com/maxzod/laravel_exception
55
environment:
66
sdk: ">=2.12.0 <3.0.0"
77

88
dependencies:
9+
blueprint: ^0.0.1-beta
910
equatable: ^2.0.3
1011

1112
dev_dependencies:

test/laravel_validation_test.dart

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'package:laravel_exception/src/exceptions/validation_err.dart';
22
import 'package:test/test.dart';
33

4-
void main() {
4+
void main() async {
55
const resData = <String, dynamic>{
66
'errors': {
77
'website': [
@@ -15,20 +15,7 @@ void main() {
1515
],
1616
},
1717
};
18-
// const resData = <String, dynamic>{
19-
// "error": {
20-
// "phone": [
21-
// "قيمة الهاتف مُستخدمة من قبل.",
22-
// 'مش مرتاح للرقم ده',
23-
// ],
24-
// "email": [
25-
// "قيمة البريد الالكتروني مُستخدمة من قبل.",
26-
// ],
27-
// "username": [
28-
// "قيمة username مُستخدمة من قبل.",
29-
// ],
30-
// },
31-
// };
18+
3219
test(
3320
'when parsing valid response it will do it right',
3421
() {
@@ -61,7 +48,7 @@ void main() {
6148

6249
expect(
6350
exception.errorsByKey('success_key'),
64-
equals([]),
51+
equals(null),
6552
);
6653

6754
expect(

0 commit comments

Comments
 (0)