@gunubin/vorm-zod 0.1.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 gunubin
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,30 @@
1
+ # @gunubin/vorm-zod
2
+
3
+ Zod schema adapter for vorm — convert Zod checks to validation rules.
4
+
5
+ Part of the [vorm](https://github.com/gunubin/vorm) monorepo.
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm install @gunubin/vorm-core @gunubin/vorm-zod zod
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```ts
16
+ import { z } from 'zod';
17
+ import { fromZod } from '@gunubin/vorm-zod';
18
+ import { vo } from '@gunubin/vorm-core';
19
+
20
+ const emailSchema = z.string().email('INVALID_EMAIL').min(1, 'REQUIRED');
21
+ const Email = vo('Email', fromZod(emailSchema));
22
+ ```
23
+
24
+ `fromZod()` extracts Zod's built-in checks (`min`, `max`, `email`, `regex`) and converts them to vorm `ValidationRule[]`. Works with `ZodBranded` schemas too.
25
+
26
+ For full documentation, see the [vorm README](https://github.com/gunubin/vorm#readme).
27
+
28
+ ## License
29
+
30
+ MIT
package/dist/index.cjs ADDED
@@ -0,0 +1,94 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ fromZod: () => fromZod
24
+ });
25
+ module.exports = __toCommonJS(index_exports);
26
+
27
+ // src/from-zod.ts
28
+ function extractChecks(schema) {
29
+ const def = schema._def;
30
+ if (def.typeName === "ZodBranded" && def.type) {
31
+ return extractChecks(def.type);
32
+ }
33
+ return def.checks ?? [];
34
+ }
35
+ function checkToRule(check) {
36
+ const code = check.message ?? check.kind;
37
+ switch (check.kind) {
38
+ case "min":
39
+ return {
40
+ code,
41
+ validate: (value) => {
42
+ if (typeof value === "string") return value.length >= (check.value ?? 0);
43
+ if (typeof value === "number") {
44
+ return check.inclusive !== false ? value >= (check.value ?? 0) : value > (check.value ?? 0);
45
+ }
46
+ return true;
47
+ }
48
+ };
49
+ case "max":
50
+ return {
51
+ code,
52
+ validate: (value) => {
53
+ if (typeof value === "string") return value.length <= (check.value ?? Infinity);
54
+ if (typeof value === "number") {
55
+ return check.inclusive !== false ? value <= (check.value ?? Infinity) : value < (check.value ?? Infinity);
56
+ }
57
+ return true;
58
+ }
59
+ };
60
+ case "email":
61
+ return {
62
+ code,
63
+ validate: (value) => {
64
+ if (typeof value === "string") {
65
+ return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value);
66
+ }
67
+ return true;
68
+ }
69
+ };
70
+ case "regex":
71
+ return {
72
+ code,
73
+ validate: (value) => {
74
+ if (typeof value === "string" && check.regex) {
75
+ return check.regex.test(value);
76
+ }
77
+ return true;
78
+ }
79
+ };
80
+ default:
81
+ return {
82
+ code,
83
+ validate: () => true
84
+ };
85
+ }
86
+ }
87
+ function fromZod(schema) {
88
+ const checks = extractChecks(schema);
89
+ return checks.map((check) => checkToRule(check));
90
+ }
91
+ // Annotate the CommonJS export names for ESM import in node:
92
+ 0 && (module.exports = {
93
+ fromZod
94
+ });
@@ -0,0 +1,6 @@
1
+ import { ValidationRule } from '@gunubin/vorm-core';
2
+ import { z } from 'zod';
3
+
4
+ declare function fromZod<T>(schema: z.ZodType<T>): ValidationRule<T>[];
5
+
6
+ export { fromZod };
@@ -0,0 +1,6 @@
1
+ import { ValidationRule } from '@gunubin/vorm-core';
2
+ import { z } from 'zod';
3
+
4
+ declare function fromZod<T>(schema: z.ZodType<T>): ValidationRule<T>[];
5
+
6
+ export { fromZod };
package/dist/index.js ADDED
@@ -0,0 +1,67 @@
1
+ // src/from-zod.ts
2
+ function extractChecks(schema) {
3
+ const def = schema._def;
4
+ if (def.typeName === "ZodBranded" && def.type) {
5
+ return extractChecks(def.type);
6
+ }
7
+ return def.checks ?? [];
8
+ }
9
+ function checkToRule(check) {
10
+ const code = check.message ?? check.kind;
11
+ switch (check.kind) {
12
+ case "min":
13
+ return {
14
+ code,
15
+ validate: (value) => {
16
+ if (typeof value === "string") return value.length >= (check.value ?? 0);
17
+ if (typeof value === "number") {
18
+ return check.inclusive !== false ? value >= (check.value ?? 0) : value > (check.value ?? 0);
19
+ }
20
+ return true;
21
+ }
22
+ };
23
+ case "max":
24
+ return {
25
+ code,
26
+ validate: (value) => {
27
+ if (typeof value === "string") return value.length <= (check.value ?? Infinity);
28
+ if (typeof value === "number") {
29
+ return check.inclusive !== false ? value <= (check.value ?? Infinity) : value < (check.value ?? Infinity);
30
+ }
31
+ return true;
32
+ }
33
+ };
34
+ case "email":
35
+ return {
36
+ code,
37
+ validate: (value) => {
38
+ if (typeof value === "string") {
39
+ return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value);
40
+ }
41
+ return true;
42
+ }
43
+ };
44
+ case "regex":
45
+ return {
46
+ code,
47
+ validate: (value) => {
48
+ if (typeof value === "string" && check.regex) {
49
+ return check.regex.test(value);
50
+ }
51
+ return true;
52
+ }
53
+ };
54
+ default:
55
+ return {
56
+ code,
57
+ validate: () => true
58
+ };
59
+ }
60
+ }
61
+ function fromZod(schema) {
62
+ const checks = extractChecks(schema);
63
+ return checks.map((check) => checkToRule(check));
64
+ }
65
+ export {
66
+ fromZod
67
+ };
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "@gunubin/vorm-zod",
3
+ "version": "0.1.0",
4
+ "description": "Zod schema adapter for vorm — convert Zod checks to validation rules",
5
+ "keywords": [
6
+ "form",
7
+ "zod",
8
+ "validation",
9
+ "adapter",
10
+ "typescript"
11
+ ],
12
+ "license": "MIT",
13
+ "author": "gunubin",
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "https://github.com/gunubin/vorm.git",
17
+ "directory": "packages/zod"
18
+ },
19
+ "homepage": "https://github.com/gunubin/vorm#readme",
20
+ "bugs": {
21
+ "url": "https://github.com/gunubin/vorm/issues"
22
+ },
23
+ "sideEffects": false,
24
+ "type": "module",
25
+ "main": "./dist/index.cjs",
26
+ "module": "./dist/index.js",
27
+ "types": "./dist/index.d.ts",
28
+ "exports": {
29
+ ".": {
30
+ "types": "./dist/index.d.ts",
31
+ "import": "./dist/index.js",
32
+ "require": "./dist/index.cjs"
33
+ }
34
+ },
35
+ "files": [
36
+ "dist"
37
+ ],
38
+ "publishConfig": {
39
+ "access": "public"
40
+ },
41
+ "peerDependencies": {
42
+ "zod": "^3.25.0"
43
+ },
44
+ "dependencies": {
45
+ "@gunubin/vorm-core": "0.1.0"
46
+ },
47
+ "devDependencies": {
48
+ "zod": "^3.25.0",
49
+ "typescript": "^5.9.3",
50
+ "vitest": "^3.0.5"
51
+ },
52
+ "scripts": {
53
+ "build": "tsup",
54
+ "typecheck": "tsc --noEmit",
55
+ "test": "vitest run",
56
+ "test:watch": "vitest"
57
+ }
58
+ }