@leancodepl/validation 8.4.0 → 8.5.0

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/package.json CHANGED
@@ -1,23 +1,59 @@
1
1
  {
2
2
  "name": "@leancodepl/validation",
3
- "version": "8.4.0",
3
+ "version": "8.5.0",
4
4
  "license": "Apache-2.0",
5
5
  "dependencies": {
6
- "@leancodepl/cqrs-client-base": "8.4.0"
6
+ "@leancodepl/cqrs-client-base": "8.5.0"
7
7
  },
8
8
  "devDependencies": {
9
9
  "sinon": "15.2.0"
10
10
  },
11
+ "publishConfig": {
12
+ "access": "public",
13
+ "registry": "https://registry.npmjs.org/"
14
+ },
15
+ "engines": {
16
+ "node": ">=18.0.0"
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/leancodepl/js_corelibrary.git",
21
+ "directory": "packages/validation"
22
+ },
23
+ "homepage": "https://github.com/leancodepl/js_corelibrary",
24
+ "bugs": {
25
+ "url": "https://github.com/leancodepl/js_corelibrary/issues"
26
+ },
27
+ "description": "CQRS validation helpers for command and query validation",
28
+ "keywords": [
29
+ "cqrs",
30
+ "validation",
31
+ "commands",
32
+ "queries",
33
+ "typescript",
34
+ "javascript",
35
+ "leancode"
36
+ ],
37
+ "author": {
38
+ "name": "LeanCode",
39
+ "url": "https://leancode.co"
40
+ },
41
+ "files": [
42
+ "dist",
43
+ "README.md",
44
+ "CHANGELOG.md"
45
+ ],
46
+ "sideEffects": false,
11
47
  "exports": {
12
48
  "./package.json": "./package.json",
13
49
  ".": {
14
50
  "module": "./index.esm.js",
15
- "types": "./index.esm.d.ts",
51
+ "types": "./index.d.ts",
16
52
  "import": "./index.cjs.mjs",
17
53
  "default": "./index.cjs.js"
18
54
  }
19
55
  },
20
56
  "module": "./index.esm.js",
21
57
  "main": "./index.cjs.js",
22
- "types": "./index.esm.d.ts"
58
+ "types": "./index.d.ts"
23
59
  }
package/index.cjs.d.ts DELETED
@@ -1 +0,0 @@
1
- export * from "./src/index";
@@ -1 +0,0 @@
1
- exports._default = require('./index.cjs.js').default;
package/index.cjs.js DELETED
@@ -1,91 +0,0 @@
1
- 'use strict';
2
-
3
- function handleValidationErrors(validationErrors, errorCodesMap, validationResults = []) {
4
- const handle = (validationErrorsToHandle, handler)=>{
5
- let result = undefined;
6
- for (const validationErrorToHandle of Array.isArray(validationErrorsToHandle) ? validationErrorsToHandle : [
7
- validationErrorsToHandle
8
- ]){
9
- const ve = validationErrors.find((ve)=>ve.ErrorCode === errorCodesMap[validationErrorToHandle]);
10
- if (ve) {
11
- result = handler(validationErrorToHandle, ve);
12
- break;
13
- }
14
- }
15
- let nextResult = validationResults;
16
- if (result !== undefined) {
17
- nextResult = [
18
- ...nextResult,
19
- result
20
- ];
21
- }
22
- return handleValidationErrors(validationErrors, errorCodesMap, nextResult);
23
- };
24
- const handleAll = (_validationErrorsToHandle, handler)=>{
25
- let result = undefined;
26
- const validationErrorsToHandle = Array.isArray(_validationErrorsToHandle) ? _validationErrorsToHandle : [
27
- _validationErrorsToHandle
28
- ];
29
- const foundErrors = validationErrorsToHandle.reduce((prev, cur)=>{
30
- const ves = validationErrors.filter((ve)=>ve.ErrorCode === errorCodesMap[cur]);
31
- if (ves.length === 0) {
32
- return prev;
33
- }
34
- return [
35
- ...prev,
36
- {
37
- errorName: cur,
38
- errors: ves
39
- }
40
- ];
41
- }, []);
42
- if (foundErrors.length > 0) {
43
- result = handler(foundErrors);
44
- }
45
- let nextResult = validationResults;
46
- if (result !== undefined) {
47
- nextResult = [
48
- ...nextResult,
49
- result
50
- ];
51
- }
52
- return handleValidationErrors(validationErrors, errorCodesMap, nextResult);
53
- };
54
- return {
55
- handle,
56
- handleAll,
57
- check: (reducer)=>{
58
- if (reducer) {
59
- return validationResults.reduce(reducer.reducer, reducer.initialValue);
60
- }
61
- return;
62
- }
63
- };
64
- }
65
-
66
- function handleResponse(response, errorCodesMap) {
67
- const newErrorCodesMap = {
68
- ...errorCodesMap,
69
- success: -1,
70
- failure: -2
71
- };
72
- const validationErrors = response.isSuccess ? response.result.WasSuccessful ? [
73
- {
74
- AttemptedValue: "",
75
- ErrorMessage: "",
76
- PropertyName: "",
77
- ErrorCode: -1
78
- }
79
- ] : response.result.ValidationErrors : [
80
- {
81
- AttemptedValue: "",
82
- ErrorMessage: "",
83
- PropertyName: "",
84
- ErrorCode: -2
85
- }
86
- ];
87
- return handleValidationErrors(validationErrors, newErrorCodesMap);
88
- }
89
-
90
- exports.handleResponse = handleResponse;
91
- exports.handleValidationErrors = handleValidationErrors;
package/index.cjs.mjs DELETED
@@ -1,2 +0,0 @@
1
- export * from './index.cjs.js';
2
- export { _default as default } from './index.cjs.default.js';
package/index.esm.d.ts DELETED
@@ -1 +0,0 @@
1
- export * from "./src/index";
package/index.esm.js DELETED
@@ -1,88 +0,0 @@
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/src/index.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from "./lib/handleResponse";
2
- export * from "./lib/handleValidationErrors";
@@ -1,9 +0,0 @@
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>;
@@ -1,23 +0,0 @@
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>, TInResult | TResult>;
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>, TInResult | TResult>;
17
- };
18
- export interface ValidationErrorsHandler<TRemainingErrors extends Record<string, number>, TResult> {
19
- handle: ValidationErrorsHandleFunc<TRemainingErrors, TResult>;
20
- handleAll: ValidationErrorsHandleAllFunc<TRemainingErrors, TResult>;
21
- check: object 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>;