@comity/validation-zod 0.9.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Filippo Bovo and contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # @comity/validation-zod
2
+
3
+ Zod adapter for @comity/validation contracts.
4
+
5
+ ---
6
+
7
+ ## Purpose
8
+
9
+ Implements the `Validator` contract defined by `@comity/validation` using Zod schemas. Enables applications to validate domain data with Zod while depending only on the Comity validation contract.
10
+
11
+ ---
12
+
13
+ ## Scope
14
+
15
+ This package:
16
+
17
+ - ✅ implements the `Validator` contract
18
+ - ✅ provides `ZodValidator`, a Zod-schema-backed validator
19
+ - ✅ maps Zod errors to Comity validation errors
20
+ - ✅ depends only on `@comity/primitives` and `@comity/validation`
21
+
22
+ This package does NOT:
23
+
24
+ - ❌ define validation contracts
25
+ - ❌ provide framework-independent validation logic
26
+ - ❌ depend on any Comity core module beyond `@comity/validation`
27
+
28
+ ---
29
+
30
+ ## Public API
31
+
32
+ - `ZodValidator` — validator implementing `Validator<T>` from a `ZodType<T>` schema
33
+
34
+
35
+ No exhaustive reference; see docs for constraints.
36
+ ---
37
+
38
+ ## Documentation
39
+
40
+ None — no package-specific documentation exists.
41
+
42
+ ---
43
+
44
+ ## Related Packages
45
+
46
+ - @comity/validation — defines the `Validator` contract this adapter implements
47
+ - @comity/primitives — foundational `Result` types used by the contract
48
+
49
+ ---
50
+
51
+ ## Status
52
+
53
+ Experimental
54
+
55
+ _Review Completed: 2026-08-15_
56
+ _Reviewer: Hobiri MAGI (DeepSeek v4 Pro)_
57
+ _Compliance Score: 98% (Green)_
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ZodValidator = void 0;
4
+ const result_1 = require("@comity/primitives/result");
5
+ const zod_error_mapper_js_1 = require("./internal/zod-error-mapper.js");
6
+ /**
7
+ * Zod-based validator implementation.
8
+ */
9
+ class ZodValidator {
10
+ #schema;
11
+ /**
12
+ * @param schema - Zod schema to validate against
13
+ */
14
+ constructor(schema) {
15
+ this.#schema = schema;
16
+ }
17
+ /**
18
+ * @inheritdoc
19
+ */
20
+ async validate(value) {
21
+ const result = await this.#schema.safeParseAsync(value);
22
+ if (result.success) {
23
+ return (0, result_1.success)(value);
24
+ }
25
+ return (0, result_1.failure)((0, zod_error_mapper_js_1.mapZodError)(result.error));
26
+ }
27
+ }
28
+ exports.ZodValidator = ZodValidator;
29
+ //# sourceMappingURL=adapter.js.map
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ZodValidator = void 0;
4
+ var adapter_js_1 = require("./adapter.js");
5
+ Object.defineProperty(exports, "ZodValidator", { enumerable: true, get: function () { return adapter_js_1.ZodValidator; } });
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.mapZodError = mapZodError;
4
+ const errors_1 = require("@comity/validation/errors");
5
+ /**
6
+ * Maps a ZodError to a ValidationError.
7
+ *
8
+ * @param error - The ZodError to map
9
+ *
10
+ * @returns A ValidationError representing the ZodError
11
+ *
12
+ * @remarks This function transforms the structure of a ZodError into a more generic ValidationError format, preserving the error codes and paths for each validation issue.
13
+ */
14
+ function mapZodError(error) {
15
+ const fields = {};
16
+ for (const issue of error.issues) {
17
+ const path = formatPath(issue.path);
18
+ (fields[path] ??= []).push({
19
+ code: issue.code,
20
+ });
21
+ }
22
+ return new errors_1.ValidationError("failed", {
23
+ details: {
24
+ fields,
25
+ },
26
+ });
27
+ }
28
+ /**
29
+ * Formats a path array into a string representation.
30
+ *
31
+ * @param path - An array of strings and/or numbers representing the path to a validation issue
32
+ *
33
+ * @returns A string representation of the path, where each segment is separated by a dot (.) and array indices are enclosed in square brackets ([]).
34
+ *
35
+ * @remarks This function is used to convert the path information from Zod's error structure into a more readable format for the ValidationError.
36
+ */
37
+ function formatPath(path) {
38
+ if (path.length === 0) {
39
+ return "$";
40
+ }
41
+ let result = "";
42
+ for (const segment of path) {
43
+ if (typeof segment === "number") {
44
+ result += `[${segment}]`;
45
+ continue;
46
+ }
47
+ if (result.length > 0) {
48
+ result += ".";
49
+ }
50
+ result += String(segment);
51
+ }
52
+ return result;
53
+ }
54
+ //# sourceMappingURL=zod-error-mapper.js.map
@@ -0,0 +1,25 @@
1
+ import { failure, success } from "@comity/primitives/result";
2
+ import { mapZodError } from "./internal/zod-error-mapper.js";
3
+ /**
4
+ * Zod-based validator implementation.
5
+ */
6
+ export class ZodValidator {
7
+ #schema;
8
+ /**
9
+ * @param schema - Zod schema to validate against
10
+ */
11
+ constructor(schema) {
12
+ this.#schema = schema;
13
+ }
14
+ /**
15
+ * @inheritdoc
16
+ */
17
+ async validate(value) {
18
+ const result = await this.#schema.safeParseAsync(value);
19
+ if (result.success) {
20
+ return success(value);
21
+ }
22
+ return failure(mapZodError(result.error));
23
+ }
24
+ }
25
+ //# sourceMappingURL=adapter.js.map
@@ -0,0 +1,2 @@
1
+ export { ZodValidator } from "./adapter.js";
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,51 @@
1
+ import { ValidationError } from "@comity/validation/errors";
2
+ /**
3
+ * Maps a ZodError to a ValidationError.
4
+ *
5
+ * @param error - The ZodError to map
6
+ *
7
+ * @returns A ValidationError representing the ZodError
8
+ *
9
+ * @remarks This function transforms the structure of a ZodError into a more generic ValidationError format, preserving the error codes and paths for each validation issue.
10
+ */
11
+ export function mapZodError(error) {
12
+ const fields = {};
13
+ for (const issue of error.issues) {
14
+ const path = formatPath(issue.path);
15
+ (fields[path] ??= []).push({
16
+ code: issue.code,
17
+ });
18
+ }
19
+ return new ValidationError("failed", {
20
+ details: {
21
+ fields,
22
+ },
23
+ });
24
+ }
25
+ /**
26
+ * Formats a path array into a string representation.
27
+ *
28
+ * @param path - An array of strings and/or numbers representing the path to a validation issue
29
+ *
30
+ * @returns A string representation of the path, where each segment is separated by a dot (.) and array indices are enclosed in square brackets ([]).
31
+ *
32
+ * @remarks This function is used to convert the path information from Zod's error structure into a more readable format for the ValidationError.
33
+ */
34
+ function formatPath(path) {
35
+ if (path.length === 0) {
36
+ return "$";
37
+ }
38
+ let result = "";
39
+ for (const segment of path) {
40
+ if (typeof segment === "number") {
41
+ result += `[${segment}]`;
42
+ continue;
43
+ }
44
+ if (result.length > 0) {
45
+ result += ".";
46
+ }
47
+ result += String(segment);
48
+ }
49
+ return result;
50
+ }
51
+ //# sourceMappingURL=zod-error-mapper.js.map
@@ -0,0 +1,18 @@
1
+ import type { Result } from "@comity/primitives/result";
2
+ import type { Validator } from "@comity/validation";
3
+ import type { ValidationError } from "@comity/validation/errors";
4
+ import type { ZodType } from "zod";
5
+ /**
6
+ * Zod-based validator implementation.
7
+ */
8
+ export declare class ZodValidator<T> implements Validator<T> {
9
+ #private;
10
+ /**
11
+ * @param schema - Zod schema to validate against
12
+ */
13
+ constructor(schema: ZodType<T>);
14
+ /**
15
+ * @inheritdoc
16
+ */
17
+ validate(value: T): Promise<Result<T, ValidationError>>;
18
+ }
@@ -0,0 +1 @@
1
+ export { ZodValidator } from "./adapter.js";
@@ -0,0 +1,12 @@
1
+ import type { ZodError } from "zod";
2
+ import { ValidationError } from "@comity/validation/errors";
3
+ /**
4
+ * Maps a ZodError to a ValidationError.
5
+ *
6
+ * @param error - The ZodError to map
7
+ *
8
+ * @returns A ValidationError representing the ZodError
9
+ *
10
+ * @remarks This function transforms the structure of a ZodError into a more generic ValidationError format, preserving the error codes and paths for each validation issue.
11
+ */
12
+ export declare function mapZodError(error: ZodError): ValidationError;
package/package.json ADDED
@@ -0,0 +1,79 @@
1
+ {
2
+ "name": "@comity/validation-zod",
3
+ "version": "0.9.0",
4
+ "description": "Zod adapter for @comity/validation contracts.",
5
+ "type": "module",
6
+ "private": false,
7
+ "author": "Filippo Bovo <hello@filippobovo.com>",
8
+ "license": "MIT",
9
+ "comity": {
10
+ "layer": "technology-adapter",
11
+ "implements": "@comity/validation"
12
+ },
13
+ "homepage": "https://github.com/comityjs/framework#readme",
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "https://github.com/comityjs/framework.git"
17
+ },
18
+ "bugs": {
19
+ "url": "https://github.com/comityjs/framework/issues"
20
+ },
21
+ "engines": {
22
+ "node": ">=24.0.0"
23
+ },
24
+ "keywords": [
25
+ "comity",
26
+ "comityjs",
27
+ "adapter",
28
+ "typescript",
29
+ "validation",
30
+ "zod"
31
+ ],
32
+ "files": [
33
+ "./dist",
34
+ "!./dist/**/*.map"
35
+ ],
36
+ "main": "./dist/cjs/index.js",
37
+ "module": "./dist/esm/index.js",
38
+ "types": "./dist/types/index.d.ts",
39
+ "exports": {
40
+ ".": {
41
+ "import": {
42
+ "types": "./dist/types/index.d.ts",
43
+ "default": "./dist/esm/index.js"
44
+ },
45
+ "require": {
46
+ "types": "./dist/types/index.d.ts",
47
+ "default": "./dist/cjs/index.js"
48
+ }
49
+ },
50
+ "./package.json": "./package.json"
51
+ },
52
+ "typesVersions": {
53
+ "*": {}
54
+ },
55
+ "publishConfig": {
56
+ "registry": "https://registry.npmjs.org",
57
+ "access": "public"
58
+ },
59
+ "sideEffects": false,
60
+ "peerDependencies": {
61
+ "zod": "^4",
62
+ "@comity/validation": "0.9.0"
63
+ },
64
+ "dependencies": {
65
+ "@comity/primitives": "0.9.0",
66
+ "@comity/validation": "0.9.0"
67
+ },
68
+ "devDependencies": {
69
+ "@types/node": "^24.13.4",
70
+ "typescript": "^5.9.3"
71
+ },
72
+ "scripts": {
73
+ "build": "node ../../scripts/build.mjs",
74
+ "dev": "node ../../scripts/build.mjs --watch",
75
+ "test": "vitest run --coverage",
76
+ "type-check": "tsc -p tsconfig.json --noEmit",
77
+ "lint": "eslint --ext .ts src"
78
+ }
79
+ }