@adobe/aio-commerce-lib-core 0.2.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/.turbo/turbo-build.log +36 -0
- package/CHANGELOG.md +12 -0
- package/LICENSE +201 -0
- package/README.md +154 -0
- package/dist/cjs/chunk-CsX-DzYB.cjs +42 -0
- package/dist/cjs/index.cjs +15 -0
- package/dist/cjs/index.d.cts +3 -0
- package/dist/cjs/lib/result.cjs +10 -0
- package/dist/cjs/lib/validation.cjs +4 -0
- package/dist/cjs/result-B_qN_g1r.d.cts +26 -0
- package/dist/cjs/result-fxmTDycS.cjs +160 -0
- package/dist/cjs/validation-12iMy71C.cjs +71 -0
- package/dist/cjs/validation-BonM9W9Z.d.cts +21 -0
- package/dist/es/chunk-Cl8Af3a2.js +11 -0
- package/dist/es/index.d.ts +3 -0
- package/dist/es/index.js +4 -0
- package/dist/es/lib/result.d.ts +2 -0
- package/dist/es/lib/result.js +3 -0
- package/dist/es/lib/validation.d.ts +3 -0
- package/dist/es/lib/validation.js +3 -0
- package/dist/es/result-B394X-_m.js +107 -0
- package/dist/es/result-BooxElIJ.d.ts +26 -0
- package/dist/es/validation-B2JQpDTl.d.ts +21 -0
- package/dist/es/validation-DmGRxtJ7.js +54 -0
- package/dist/lib/result.d.cts +2 -0
- package/dist/lib/validation.d.cts +3 -0
- package/package.json +77 -0
- package/source/index.ts +15 -0
- package/source/lib/result.ts +156 -0
- package/source/lib/validation.ts +97 -0
- package/test/result.test.ts +56 -0
- package/test/validation.test.ts +71 -0
- package/tsconfig.json +15 -0
- package/tsdown.config.ts +11 -0
- package/vitest.config.ts +16 -0
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2025 Adobe. All rights reserved.
|
|
3
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*
|
|
7
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
+
* governing permissions and limitations under the License.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { cyanBright, dim, whiteBright, yellowBright } from "ansis";
|
|
14
|
+
import { type BaseIssue, type GenericIssue, getDotPath } from "valibot";
|
|
15
|
+
|
|
16
|
+
import type { ErrorType } from "./result";
|
|
17
|
+
|
|
18
|
+
const LAST_RETURN_CHAR = "└── ";
|
|
19
|
+
const RETURN_CHAR = "├── ";
|
|
20
|
+
|
|
21
|
+
type IssueKind = "schema" | "validation" | "transformation";
|
|
22
|
+
type ValidationErrorTag = `${string}ValidationError`;
|
|
23
|
+
|
|
24
|
+
const ISSUE_KIND_TO_ERROR_TITLE: Record<IssueKind, string> = {
|
|
25
|
+
schema: "Schema validation error",
|
|
26
|
+
validation: "Input error",
|
|
27
|
+
transformation: "Transformation error",
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
function issueToDisplay<TInput>(issues: BaseIssue<TInput>[]) {
|
|
31
|
+
const lines = issues.map((issue, index) => {
|
|
32
|
+
const returnChar = cyanBright(
|
|
33
|
+
index === issues.length - 1 ? LAST_RETURN_CHAR : RETURN_CHAR,
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
const kindText = yellowBright(
|
|
37
|
+
ISSUE_KIND_TO_ERROR_TITLE[issue.kind as IssueKind] ||
|
|
38
|
+
"Unknown issue kind",
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
const dotPath = getDotPath(issue);
|
|
42
|
+
const path = dotPath ? cyanBright(dotPath) + whiteBright(dim(" →")) : "";
|
|
43
|
+
const issueLine = `${kindText} ${whiteBright("at")} ${path} ${whiteBright(issue.message)}`;
|
|
44
|
+
|
|
45
|
+
return `${returnChar} ${issueLine}`;
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
return lines.join("\n");
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Summarizes a validation error by displaying the error message and the issues.
|
|
53
|
+
* @param error The validation error to summarize.
|
|
54
|
+
* @returns A pretty-printed string containing the summary of the validation error.
|
|
55
|
+
*/
|
|
56
|
+
export function summarizeValidationError(error: ValidationError) {
|
|
57
|
+
return `${whiteBright(error.message)}\n${issueToDisplay(error.issues)}`;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Creates a validation error.
|
|
62
|
+
* @param message The validation error message.
|
|
63
|
+
* @param issues The issues that occurred during validation.
|
|
64
|
+
* @param info Additional information about the error.
|
|
65
|
+
* @returns A validation error.
|
|
66
|
+
*/
|
|
67
|
+
export function makeValidationError<
|
|
68
|
+
TIssues extends GenericIssue[] = GenericIssue[],
|
|
69
|
+
TInfo extends Record<string, unknown> = Record<string, unknown>,
|
|
70
|
+
>(message: string, issues: TIssues, info: TInfo = {} as TInfo) {
|
|
71
|
+
return {
|
|
72
|
+
_tag: "ValidationError",
|
|
73
|
+
message,
|
|
74
|
+
issues,
|
|
75
|
+
...info,
|
|
76
|
+
} satisfies ValidationErrorType<ValidationErrorTag, TIssues, TInfo>;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Defines a generic type used to instantiate custom validation error interfaces. */
|
|
80
|
+
export type ValidationErrorType<
|
|
81
|
+
TErrorTag extends ValidationErrorTag = "ValidationError",
|
|
82
|
+
TIssues extends GenericIssue[] = GenericIssue[],
|
|
83
|
+
TInfo extends Record<string, unknown> = Record<string, unknown>,
|
|
84
|
+
> = ErrorType<
|
|
85
|
+
TErrorTag,
|
|
86
|
+
TInfo & {
|
|
87
|
+
message: string;
|
|
88
|
+
issues: TIssues;
|
|
89
|
+
}
|
|
90
|
+
>;
|
|
91
|
+
|
|
92
|
+
/** Defines a validation error. */
|
|
93
|
+
export type ValidationError = ValidationErrorType<
|
|
94
|
+
ValidationErrorTag,
|
|
95
|
+
GenericIssue[],
|
|
96
|
+
Record<string, unknown>
|
|
97
|
+
>;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2025 Adobe. All rights reserved.
|
|
3
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*
|
|
7
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
+
* governing permissions and limitations under the License.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { describe, expect, test } from "vitest";
|
|
14
|
+
import { err, isErr, isOk, ok, unwrap, unwrapErr } from "~/lib/result";
|
|
15
|
+
|
|
16
|
+
describe("aio-commerce-lib-core/result", () => {
|
|
17
|
+
test("should wrap a value in a success result", () => {
|
|
18
|
+
const result = ok("success");
|
|
19
|
+
expect(result).toEqual({ type: "success", value: "success" });
|
|
20
|
+
|
|
21
|
+
const value = unwrap(result);
|
|
22
|
+
expect(value).toBe("success");
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test("should wrap an error in a failure result", () => {
|
|
26
|
+
const result = err({ _tag: "Error", message: "error" });
|
|
27
|
+
expect(result).toEqual({
|
|
28
|
+
type: "failure",
|
|
29
|
+
error: { _tag: "Error", message: "error" },
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const error = unwrapErr(result);
|
|
33
|
+
expect(error).toEqual({ _tag: "Error", message: "error" });
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test("should unwrap a success result", () => {
|
|
37
|
+
const result = ok("success");
|
|
38
|
+
const value = unwrap(result);
|
|
39
|
+
expect(value).toBe("success");
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test("should unwrap a failure result", () => {
|
|
43
|
+
const result = err({ _tag: "Error", message: "error" });
|
|
44
|
+
expect(() => unwrap(result)).toThrow();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test("should check if a result is a success", () => {
|
|
48
|
+
const result = ok("success");
|
|
49
|
+
expect(isOk(result)).toBe(true);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test("should check if a result is a failure", () => {
|
|
53
|
+
const result = err({ _tag: "Error", message: "error" });
|
|
54
|
+
expect(isErr(result)).toBe(true);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2025 Adobe. All rights reserved.
|
|
3
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*
|
|
7
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
+
* governing permissions and limitations under the License.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { boolean, number, object, safeParse, string } from "valibot";
|
|
14
|
+
import { describe, expect, test } from "vitest";
|
|
15
|
+
|
|
16
|
+
import {
|
|
17
|
+
makeValidationError,
|
|
18
|
+
summarizeValidationError,
|
|
19
|
+
} from "~/lib/validation";
|
|
20
|
+
|
|
21
|
+
const SimpleObjectSchema = object({
|
|
22
|
+
key1: string(),
|
|
23
|
+
key2: number(),
|
|
24
|
+
key3: object({
|
|
25
|
+
nestedKey: object({
|
|
26
|
+
nestedKey: boolean(),
|
|
27
|
+
}),
|
|
28
|
+
}),
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const simpleObject = {
|
|
32
|
+
key3: {
|
|
33
|
+
nestedKey: {
|
|
34
|
+
nestedKey: "nestedKey",
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
describe("aio-commerce-lib-core/validation", () => {
|
|
40
|
+
test("should summarize a list of issues with colors and structure", () => {
|
|
41
|
+
const result = safeParse(SimpleObjectSchema, simpleObject);
|
|
42
|
+
|
|
43
|
+
if (!result.success) {
|
|
44
|
+
// `result.issues` is only satisfied if the validation fails.
|
|
45
|
+
const validationError = makeValidationError(
|
|
46
|
+
"Validation failed",
|
|
47
|
+
result.issues,
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
const output = summarizeValidationError(validationError);
|
|
51
|
+
expect(output).toContain("key1");
|
|
52
|
+
expect(output).toContain("key2");
|
|
53
|
+
expect(output).toContain("key3.nestedKey.nestedKey");
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test("should make a validation error", () => {
|
|
58
|
+
const result = safeParse(SimpleObjectSchema, simpleObject);
|
|
59
|
+
|
|
60
|
+
if (!result.success) {
|
|
61
|
+
const validationError = makeValidationError(
|
|
62
|
+
"Validation failed",
|
|
63
|
+
result.issues,
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
expect(validationError._tag).toBe("ValidationError");
|
|
67
|
+
expect(validationError.message).toBe("Validation failed");
|
|
68
|
+
expect(validationError.issues.length).toBeGreaterThan(0);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "@aio-commerce-sdk/config-typescript/tsconfig.base.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"baseUrl": ".",
|
|
5
|
+
"rootDir": ".",
|
|
6
|
+
"outDir": "./dist",
|
|
7
|
+
|
|
8
|
+
"paths": {
|
|
9
|
+
"~/*": ["./source/*"],
|
|
10
|
+
"~~/*": ["./*"]
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
|
|
14
|
+
"include": ["tsdown.config.ts", "source/**/*", "test/**/*"]
|
|
15
|
+
}
|
package/tsdown.config.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { baseConfig } from "@aio-commerce-sdk/config-tsdown/tsdown.config.base";
|
|
2
|
+
import { defineConfig } from "tsdown";
|
|
3
|
+
|
|
4
|
+
export default defineConfig({
|
|
5
|
+
...baseConfig,
|
|
6
|
+
entry: [
|
|
7
|
+
"./source/index.ts",
|
|
8
|
+
"./source/lib/result.ts",
|
|
9
|
+
"./source/lib/validation.ts",
|
|
10
|
+
],
|
|
11
|
+
});
|
package/vitest.config.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { resolve } from "node:path";
|
|
2
|
+
import { defineConfig } from "vitest/config";
|
|
3
|
+
|
|
4
|
+
export default defineConfig({
|
|
5
|
+
resolve: {
|
|
6
|
+
alias: {
|
|
7
|
+
"~": resolve(__dirname, "./source"),
|
|
8
|
+
"~~": resolve(__dirname, "./"),
|
|
9
|
+
},
|
|
10
|
+
},
|
|
11
|
+
|
|
12
|
+
test: {
|
|
13
|
+
globals: true,
|
|
14
|
+
environment: "node",
|
|
15
|
+
},
|
|
16
|
+
});
|