@leancodepl/validation 9.6.4 → 9.6.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -23
- package/index.cjs.js +5 -7
- package/index.esm.js +5 -7
- package/package.json +2 -2
- package/src/lib/handleValidationErrors.d.ts +1 -1
package/README.md
CHANGED
|
@@ -43,14 +43,12 @@ Handles CQRS command responses and transforms them into validation error handler
|
|
|
43
43
|
import { handleValidationErrors } from "@leancodepl/validation"
|
|
44
44
|
|
|
45
45
|
const errorCodes = { EmailExists: 1, InvalidEmail: 2 } as const
|
|
46
|
-
const errors = [
|
|
47
|
-
{ ErrorCode: 1, ErrorMessage: "Email exists", PropertyName: "Email", AttemptedValue: "user@example.com" },
|
|
48
|
-
]
|
|
46
|
+
const errors = [{ ErrorCode: 1, ErrorMessage: "Email exists", PropertyName: "Email" }]
|
|
49
47
|
|
|
50
48
|
handleValidationErrors(errors, errorCodes)
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
49
|
+
.handle("EmailExists", () => console.log("Email already registered"))
|
|
50
|
+
.handle("InvalidEmail", () => console.log("Invalid email format"))
|
|
51
|
+
.check()
|
|
54
52
|
```
|
|
55
53
|
|
|
56
54
|
### Command Response Handling
|
|
@@ -62,10 +60,10 @@ const errorCodes = { UserNotFound: 1 } as const
|
|
|
62
60
|
const response = await fetch("/api/users/123", { method: "PUT", body: JSON.stringify({ name: "John" }) })
|
|
63
61
|
|
|
64
62
|
handleResponse(response, errorCodes)
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
63
|
+
.handle("success", () => console.log("User updated"))
|
|
64
|
+
.handle("UserNotFound", () => console.log("User not found"))
|
|
65
|
+
.handle("failure", () => console.log("Request failed"))
|
|
66
|
+
.check()
|
|
69
67
|
```
|
|
70
68
|
|
|
71
69
|
### Multiple Error Handling
|
|
@@ -75,17 +73,17 @@ import { handleValidationErrors } from "@leancodepl/validation"
|
|
|
75
73
|
|
|
76
74
|
const errorCodes = { Required: 1, Invalid: 2 } as const
|
|
77
75
|
const errors = [
|
|
78
|
-
|
|
79
|
-
|
|
76
|
+
{ ErrorCode: 1, PropertyName: "email", ErrorMessage: "Email required" },
|
|
77
|
+
{ ErrorCode: 2, PropertyName: "name", ErrorMessage: "Invalid name" },
|
|
80
78
|
]
|
|
81
79
|
|
|
82
80
|
handleValidationErrors(errors, errorCodes)
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
})
|
|
81
|
+
.handleAll(["Required", "Invalid"], errorGroups => {
|
|
82
|
+
errorGroups.forEach(({ errors }) => {
|
|
83
|
+
errors.forEach(error => console.log(`${error.PropertyName}: ${error.ErrorMessage}`))
|
|
87
84
|
})
|
|
88
|
-
|
|
85
|
+
})
|
|
86
|
+
.check()
|
|
89
87
|
```
|
|
90
88
|
|
|
91
89
|
### Success/Failure Result Processing
|
|
@@ -97,10 +95,10 @@ const errorCodes = { InvalidData: 1 } as const
|
|
|
97
95
|
const response = await fetch("/api/data")
|
|
98
96
|
|
|
99
97
|
const isSuccess = handleResponse(response, errorCodes)
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
98
|
+
.handle("success", () => true)
|
|
99
|
+
.handle(["InvalidData", "failure"], () => false)
|
|
100
|
+
.check({
|
|
101
|
+
reducer: (prev, current) => prev && current,
|
|
102
|
+
initialValue: true,
|
|
103
|
+
})
|
|
106
104
|
```
|
package/index.cjs.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Creates a validation error handler that processes errors with type-safe error code mapping.
|
|
5
|
-
*
|
|
5
|
+
*
|
|
6
6
|
* @template TAllErrors - Error codes map type extending Record<string, number>
|
|
7
7
|
* @template TInResult - Type of results accumulated from previous handlers
|
|
8
8
|
* @param validationErrors - Array of validation errors to process
|
|
@@ -12,8 +12,8 @@
|
|
|
12
12
|
* @example
|
|
13
13
|
* ```typescript
|
|
14
14
|
* const errorCodes = { EmailExists: 1, InvalidEmail: 2 } as const;
|
|
15
|
-
* const errors = [{ ErrorCode: 1, ErrorMessage: 'Email exists', PropertyName: 'Email'
|
|
16
|
-
*
|
|
15
|
+
* const errors = [{ ErrorCode: 1, ErrorMessage: 'Email exists', PropertyName: 'Email' }];
|
|
16
|
+
*
|
|
17
17
|
* handleValidationErrors(errors, errorCodes)
|
|
18
18
|
* .handle('EmailExists', () => console.warn('Email already registered'))
|
|
19
19
|
* .handle('InvalidEmail', () => console.warn('Invalid email format'))
|
|
@@ -84,7 +84,7 @@
|
|
|
84
84
|
|
|
85
85
|
/**
|
|
86
86
|
* Handles CQRS command responses and transforms them into validation error handlers.
|
|
87
|
-
*
|
|
87
|
+
*
|
|
88
88
|
* @template TErrors - Error codes map type extending Record<string, number>
|
|
89
89
|
* @param response - API response containing command result
|
|
90
90
|
* @param errorCodesMap - Mapping of error names to numeric codes
|
|
@@ -93,7 +93,7 @@
|
|
|
93
93
|
* ```typescript
|
|
94
94
|
* const errorCodes = { UserNotFound: 1 } as const;
|
|
95
95
|
* const response = await commandClient.execute(createUserCommand);
|
|
96
|
-
*
|
|
96
|
+
*
|
|
97
97
|
* handleResponse(response, errorCodes)
|
|
98
98
|
* .handle('success', () => console.log('User created'))
|
|
99
99
|
* .handle('failure', () => console.log('Network error'))
|
|
@@ -108,14 +108,12 @@
|
|
|
108
108
|
};
|
|
109
109
|
const validationErrors = response.isSuccess ? response.result.WasSuccessful ? [
|
|
110
110
|
{
|
|
111
|
-
AttemptedValue: "",
|
|
112
111
|
ErrorMessage: "",
|
|
113
112
|
PropertyName: "",
|
|
114
113
|
ErrorCode: -1
|
|
115
114
|
}
|
|
116
115
|
] : response.result.ValidationErrors : [
|
|
117
116
|
{
|
|
118
|
-
AttemptedValue: "",
|
|
119
117
|
ErrorMessage: "",
|
|
120
118
|
PropertyName: "",
|
|
121
119
|
ErrorCode: -2
|
package/index.esm.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Creates a validation error handler that processes errors with type-safe error code mapping.
|
|
3
|
-
*
|
|
3
|
+
*
|
|
4
4
|
* @template TAllErrors - Error codes map type extending Record<string, number>
|
|
5
5
|
* @template TInResult - Type of results accumulated from previous handlers
|
|
6
6
|
* @param validationErrors - Array of validation errors to process
|
|
@@ -10,8 +10,8 @@
|
|
|
10
10
|
* @example
|
|
11
11
|
* ```typescript
|
|
12
12
|
* const errorCodes = { EmailExists: 1, InvalidEmail: 2 } as const;
|
|
13
|
-
* const errors = [{ ErrorCode: 1, ErrorMessage: 'Email exists', PropertyName: 'Email'
|
|
14
|
-
*
|
|
13
|
+
* const errors = [{ ErrorCode: 1, ErrorMessage: 'Email exists', PropertyName: 'Email' }];
|
|
14
|
+
*
|
|
15
15
|
* handleValidationErrors(errors, errorCodes)
|
|
16
16
|
* .handle('EmailExists', () => console.warn('Email already registered'))
|
|
17
17
|
* .handle('InvalidEmail', () => console.warn('Invalid email format'))
|
|
@@ -82,7 +82,7 @@
|
|
|
82
82
|
|
|
83
83
|
/**
|
|
84
84
|
* Handles CQRS command responses and transforms them into validation error handlers.
|
|
85
|
-
*
|
|
85
|
+
*
|
|
86
86
|
* @template TErrors - Error codes map type extending Record<string, number>
|
|
87
87
|
* @param response - API response containing command result
|
|
88
88
|
* @param errorCodesMap - Mapping of error names to numeric codes
|
|
@@ -91,7 +91,7 @@
|
|
|
91
91
|
* ```typescript
|
|
92
92
|
* const errorCodes = { UserNotFound: 1 } as const;
|
|
93
93
|
* const response = await commandClient.execute(createUserCommand);
|
|
94
|
-
*
|
|
94
|
+
*
|
|
95
95
|
* handleResponse(response, errorCodes)
|
|
96
96
|
* .handle('success', () => console.log('User created'))
|
|
97
97
|
* .handle('failure', () => console.log('Network error'))
|
|
@@ -106,14 +106,12 @@
|
|
|
106
106
|
};
|
|
107
107
|
const validationErrors = response.isSuccess ? response.result.WasSuccessful ? [
|
|
108
108
|
{
|
|
109
|
-
AttemptedValue: "",
|
|
110
109
|
ErrorMessage: "",
|
|
111
110
|
PropertyName: "",
|
|
112
111
|
ErrorCode: -1
|
|
113
112
|
}
|
|
114
113
|
] : response.result.ValidationErrors : [
|
|
115
114
|
{
|
|
116
|
-
AttemptedValue: "",
|
|
117
115
|
ErrorMessage: "",
|
|
118
116
|
PropertyName: "",
|
|
119
117
|
ErrorCode: -2
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leancodepl/validation",
|
|
3
|
-
"version": "9.6.
|
|
3
|
+
"version": "9.6.6",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"dependencies": {
|
|
6
|
-
"@leancodepl/cqrs-client-base": "9.6.
|
|
6
|
+
"@leancodepl/cqrs-client-base": "9.6.6"
|
|
7
7
|
},
|
|
8
8
|
"devDependencies": {
|
|
9
9
|
"sinon": "15.2.0"
|
|
@@ -32,7 +32,7 @@ export interface ValidationErrorsHandler<TRemainingErrors extends Record<string,
|
|
|
32
32
|
* @example
|
|
33
33
|
* ```typescript
|
|
34
34
|
* const errorCodes = { EmailExists: 1, InvalidEmail: 2 } as const;
|
|
35
|
-
* const errors = [{ ErrorCode: 1, ErrorMessage: 'Email exists', PropertyName: 'Email'
|
|
35
|
+
* const errors = [{ ErrorCode: 1, ErrorMessage: 'Email exists', PropertyName: 'Email' }];
|
|
36
36
|
*
|
|
37
37
|
* handleValidationErrors(errors, errorCodes)
|
|
38
38
|
* .handle('EmailExists', () => console.warn('Email already registered'))
|