@leancodepl/validation 7.1.1

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/index.cjs.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from "./src/index";
package/index.cjs.js ADDED
@@ -0,0 +1,93 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ function handleValidationErrors(validationErrors, errorCodesMap, validationResults = []) {
6
+ const handle = (validationErrorsToHandle, handler)=>{
7
+ let result = undefined;
8
+ for (const validationErrorToHandle of Array.isArray(validationErrorsToHandle) ? validationErrorsToHandle : [
9
+ validationErrorsToHandle
10
+ ]){
11
+ const ve = validationErrors.find((ve)=>ve.ErrorCode === errorCodesMap[validationErrorToHandle]);
12
+ if (ve) {
13
+ result = handler(validationErrorToHandle, ve);
14
+ break;
15
+ }
16
+ }
17
+ let nextResult = validationResults;
18
+ if (result !== undefined) {
19
+ nextResult = [
20
+ ...nextResult,
21
+ result
22
+ ];
23
+ }
24
+ return handleValidationErrors(validationErrors, errorCodesMap, nextResult);
25
+ };
26
+ const handleAll = (_validationErrorsToHandle, handler)=>{
27
+ let result = undefined;
28
+ const validationErrorsToHandle = Array.isArray(_validationErrorsToHandle) ? _validationErrorsToHandle : [
29
+ _validationErrorsToHandle
30
+ ];
31
+ const foundErrors = validationErrorsToHandle.reduce((prev, cur)=>{
32
+ const ves = validationErrors.filter((ve)=>ve.ErrorCode === errorCodesMap[cur]);
33
+ if (ves.length === 0) {
34
+ return prev;
35
+ }
36
+ return [
37
+ ...prev,
38
+ {
39
+ errorName: cur,
40
+ errors: ves
41
+ }
42
+ ];
43
+ }, []);
44
+ if (foundErrors.length > 0) {
45
+ result = handler(foundErrors);
46
+ }
47
+ let nextResult = validationResults;
48
+ if (result !== undefined) {
49
+ nextResult = [
50
+ ...nextResult,
51
+ result
52
+ ];
53
+ }
54
+ return handleValidationErrors(validationErrors, errorCodesMap, nextResult);
55
+ };
56
+ return {
57
+ handle,
58
+ handleAll,
59
+ check: (reducer)=>{
60
+ if (reducer) {
61
+ return validationResults.reduce(reducer.reducer, reducer.initialValue);
62
+ }
63
+ return;
64
+ }
65
+ };
66
+ }
67
+
68
+ function handleResponse(response, errorCodesMap) {
69
+ const newErrorCodesMap = {
70
+ ...errorCodesMap,
71
+ success: -1,
72
+ failure: -2
73
+ };
74
+ const validationErrors = response.isSuccess ? response.result.WasSuccessful ? [
75
+ {
76
+ AttemptedValue: "",
77
+ ErrorMessage: "",
78
+ PropertyName: "",
79
+ ErrorCode: -1
80
+ }
81
+ ] : response.result.ValidationErrors : [
82
+ {
83
+ AttemptedValue: "",
84
+ ErrorMessage: "",
85
+ PropertyName: "",
86
+ ErrorCode: -2
87
+ }
88
+ ];
89
+ return handleValidationErrors(validationErrors, newErrorCodesMap);
90
+ }
91
+
92
+ exports.handleResponse = handleResponse;
93
+ exports.handleValidationErrors = handleValidationErrors;
package/index.esm.js ADDED
@@ -0,0 +1,88 @@
1
+ function handleValidationErrors(validationErrors, errorCodesMap, validationResults = []) {
2
+ const handle = (validationErrorsToHandle, handler)=>{
3
+ let result = undefined;
4
+ for (const validationErrorToHandle of Array.isArray(validationErrorsToHandle) ? validationErrorsToHandle : [
5
+ validationErrorsToHandle
6
+ ]){
7
+ const ve = validationErrors.find((ve)=>ve.ErrorCode === errorCodesMap[validationErrorToHandle]);
8
+ if (ve) {
9
+ result = handler(validationErrorToHandle, ve);
10
+ break;
11
+ }
12
+ }
13
+ let nextResult = validationResults;
14
+ if (result !== undefined) {
15
+ nextResult = [
16
+ ...nextResult,
17
+ result
18
+ ];
19
+ }
20
+ return handleValidationErrors(validationErrors, errorCodesMap, nextResult);
21
+ };
22
+ const handleAll = (_validationErrorsToHandle, handler)=>{
23
+ let result = undefined;
24
+ const validationErrorsToHandle = Array.isArray(_validationErrorsToHandle) ? _validationErrorsToHandle : [
25
+ _validationErrorsToHandle
26
+ ];
27
+ const foundErrors = validationErrorsToHandle.reduce((prev, cur)=>{
28
+ const ves = validationErrors.filter((ve)=>ve.ErrorCode === errorCodesMap[cur]);
29
+ if (ves.length === 0) {
30
+ return prev;
31
+ }
32
+ return [
33
+ ...prev,
34
+ {
35
+ errorName: cur,
36
+ errors: ves
37
+ }
38
+ ];
39
+ }, []);
40
+ if (foundErrors.length > 0) {
41
+ result = handler(foundErrors);
42
+ }
43
+ let nextResult = validationResults;
44
+ if (result !== undefined) {
45
+ nextResult = [
46
+ ...nextResult,
47
+ result
48
+ ];
49
+ }
50
+ return handleValidationErrors(validationErrors, errorCodesMap, nextResult);
51
+ };
52
+ return {
53
+ handle,
54
+ handleAll,
55
+ check: (reducer)=>{
56
+ if (reducer) {
57
+ return validationResults.reduce(reducer.reducer, reducer.initialValue);
58
+ }
59
+ return;
60
+ }
61
+ };
62
+ }
63
+
64
+ function handleResponse(response, errorCodesMap) {
65
+ const newErrorCodesMap = {
66
+ ...errorCodesMap,
67
+ success: -1,
68
+ failure: -2
69
+ };
70
+ const validationErrors = response.isSuccess ? response.result.WasSuccessful ? [
71
+ {
72
+ AttemptedValue: "",
73
+ ErrorMessage: "",
74
+ PropertyName: "",
75
+ ErrorCode: -1
76
+ }
77
+ ] : response.result.ValidationErrors : [
78
+ {
79
+ AttemptedValue: "",
80
+ ErrorMessage: "",
81
+ PropertyName: "",
82
+ ErrorCode: -2
83
+ }
84
+ ];
85
+ return handleValidationErrors(validationErrors, newErrorCodesMap);
86
+ }
87
+
88
+ export { handleResponse, handleValidationErrors };
package/package.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "name": "@leancodepl/validation",
3
+ "version": "7.1.1",
4
+ "type": "commonjs",
5
+ "main": "./index.cjs.js",
6
+ "module": "./index.esm.js",
7
+ "dependencies": {
8
+ "@leancodepl/cqrs-client-base": "7.1.0"
9
+ }
10
+ }
package/src/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./lib/handleResponse";
2
+ export * from "./lib/handleValidationErrors";
@@ -0,0 +1,9 @@
1
+ import { ApiResponse, CommandResult } from "@leancodepl/cqrs-client-base";
2
+ export type SuccessOrFailureMarker = {
3
+ success: -1;
4
+ failure: -2;
5
+ };
6
+ export declare function handleResponse<TErrors extends Record<string, number>>(response: ApiResponse<CommandResult<TErrors>>, errorCodesMap: TErrors): import("./handleValidationErrors").ValidationErrorsHandler<TErrors & {
7
+ readonly success: -1;
8
+ readonly failure: -2;
9
+ }, never>;
@@ -0,0 +1,23 @@
1
+ import { ValidationError } from "@leancodepl/cqrs-client-base";
2
+ export type ReducerDescription<THandlerResult, TReturnValue = THandlerResult> = {
3
+ reducer: (prev: TReturnValue, cur: THandlerResult) => TReturnValue;
4
+ initialValue: TReturnValue;
5
+ };
6
+ export type SpecificValidationError<TErrors extends Record<string, number>, TError extends keyof TErrors> = ValidationError<Record<TError, TErrors[TError]>>;
7
+ export type ValidationErrorHandlerFunc<TErrorsToHandle extends Record<string, number>, THandledErrors extends keyof TErrorsToHandle, TResult> = (errorName: THandledErrors, error: SpecificValidationError<TErrorsToHandle, THandledErrors>) => TResult;
8
+ export type ValidationErrorsHandleFunc<TErrorsToHandle extends Record<string, number>, TInResult> = {
9
+ <THandledErrors extends keyof TErrorsToHandle, TResult>(validationErrors: THandledErrors | THandledErrors[], handler: ValidationErrorHandlerFunc<TErrorsToHandle, THandledErrors, TResult>): ValidationErrorsHandler<Omit<TErrorsToHandle, THandledErrors>, TResult | TInResult>;
10
+ };
11
+ export type ValidationErrorHandlerAllFunc<TErrorsToHandle extends Record<string, number>, THandledErrors extends keyof TErrorsToHandle, TResult> = (errors: {
12
+ errorName: THandledErrors;
13
+ errors: SpecificValidationError<TErrorsToHandle, THandledErrors>[];
14
+ }[]) => TResult;
15
+ export type ValidationErrorsHandleAllFunc<TErrorsToHandle extends Record<string, number>, TInResult> = {
16
+ <THandledErrors extends keyof TErrorsToHandle, TResult>(validationErrors: THandledErrors | THandledErrors[], handler: ValidationErrorHandlerAllFunc<TErrorsToHandle, THandledErrors, TResult>): ValidationErrorsHandler<Omit<TErrorsToHandle, THandledErrors>, TResult | TInResult>;
17
+ };
18
+ export interface ValidationErrorsHandler<TRemainingErrors extends Record<string, number>, TResult> {
19
+ handle: ValidationErrorsHandleFunc<TRemainingErrors, TResult>;
20
+ handleAll: ValidationErrorsHandleAllFunc<TRemainingErrors, TResult>;
21
+ check: {} extends TRemainingErrors ? <TReturnValue = void>(reducer?: ReducerDescription<TResult, TReturnValue>) => TReturnValue : unknown;
22
+ }
23
+ export declare function handleValidationErrors<TAllErrors extends Record<string, number>, TInResult = never>(validationErrors: ValidationError<TAllErrors>[], errorCodesMap: TAllErrors, validationResults?: TInResult[]): ValidationErrorsHandler<TAllErrors, TInResult>;