@mxpicture/zod-meta 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/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # zod-meta-lib
2
+
3
+ Example Node library using TypeScript and Zod. Produces:
4
+
5
+ - CommonJS build at `dist/cjs`
6
+ - ESM build at `dist/esm`
7
+ - Type declarations at `dist/types`
8
+
9
+ Usage (after npm publish or local link):
10
+
11
+ ESM:
12
+
13
+ ```js
14
+ import { validateUser } from "zod-meta-lib";
15
+ ```
16
+
17
+ CommonJS:
18
+
19
+ ```js
20
+ const { validateUser } = require("zod-meta-lib");
21
+ ```
22
+
23
+ Build:
24
+
25
+ ```bash
26
+ npm install
27
+ npm run build
28
+ ```
29
+
30
+ ## Setup husky
31
+
32
+ ```bash
33
+ npm install --save-dev @commitlint/config-conventional @commitlint/cli husky commitizen cz-conventional-changelog husky
34
+ npx husky-init
35
+ npm i
36
+ npm run prepare
37
+ npx husky add .husky/commit-msg 'npx --no -- commitlint --edit "$1"'
38
+ ```
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Meta = void 0;
4
+ const common_1 = require("./common");
5
+ class Meta {
6
+ constructor(props) {
7
+ this.props = props;
8
+ }
9
+ items() {
10
+ return (0, common_1.metaItems)(this.props);
11
+ }
12
+ }
13
+ exports.Meta = Meta;
@@ -0,0 +1,243 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.metaItems = exports.castEnum = exports.castOptional = exports.castRecord = exports.castObject = exports.castArray = exports.castDate = exports.castInt = exports.castNumber = exports.castString = exports.castBool = void 0;
4
+ const types_1 = require("./types");
5
+ const castType = (type, prop) => (prop._zod.def.type === type ? prop : undefined);
6
+ const castBool = (p) => castType(types_1.MetaType.boolean, p);
7
+ exports.castBool = castBool;
8
+ const castString = (p) => castType(types_1.MetaType.string, p);
9
+ exports.castString = castString;
10
+ const castNumber = (p) => castType(types_1.MetaType.number, p);
11
+ exports.castNumber = castNumber;
12
+ const castInt = (p) => castType(types_1.MetaType.int, p);
13
+ exports.castInt = castInt;
14
+ const castDate = (p) => castType(types_1.MetaType.date, p);
15
+ exports.castDate = castDate;
16
+ const castArray = (p) => castType(types_1.MetaType.array, p);
17
+ exports.castArray = castArray;
18
+ const castObject = (p) => castType(types_1.MetaType.object, p);
19
+ exports.castObject = castObject;
20
+ const castRecord = (p) => castType(types_1.MetaType.record, p);
21
+ exports.castRecord = castRecord;
22
+ const castOptional = (p) => castType(types_1.MetaInternalType.optional, p);
23
+ exports.castOptional = castOptional;
24
+ const castEnum = (p) => castType(types_1.MetaType.enum, p);
25
+ exports.castEnum = castEnum;
26
+ const metaFunctions = {
27
+ boolean: (name, _prop, optional) => {
28
+ const prop = (0, exports.castBool)(_prop);
29
+ if (!prop)
30
+ return;
31
+ const meta = prop.meta();
32
+ return {
33
+ boolean: {
34
+ name,
35
+ title: meta?.title,
36
+ description: meta?.description,
37
+ type: types_1.MetaType.boolean,
38
+ optional,
39
+ },
40
+ };
41
+ },
42
+ string: (name, _prop, optional) => {
43
+ const prop = (0, exports.castString)(_prop);
44
+ if (!prop)
45
+ return;
46
+ const meta = prop.meta();
47
+ const checks = metaChecks(prop._zod.def);
48
+ return {
49
+ string: {
50
+ name,
51
+ title: meta?.title,
52
+ description: meta?.description,
53
+ type: types_1.MetaType.string,
54
+ minLength: checks.min_length?.minimum,
55
+ maxLength: checks.max_length?.maximum,
56
+ exactLength: checks.length_equals?.length,
57
+ regex: checks.regex?.pattern?.toString() ??
58
+ checks.string_format?.pattern?.toString(),
59
+ format: checks.regex?.format ?? checks.string_format?.format,
60
+ optional,
61
+ },
62
+ };
63
+ },
64
+ number: (name, _prop, optional) => {
65
+ const prop = (0, exports.castNumber)(_prop);
66
+ if (!prop)
67
+ return;
68
+ const meta = prop.meta();
69
+ const checks = metaChecks(prop._zod.def);
70
+ return {
71
+ number: {
72
+ name,
73
+ title: meta?.title,
74
+ description: meta?.description,
75
+ type: types_1.MetaType.number,
76
+ minValue: checks.greater_than?.value,
77
+ minInclusive: checks.greater_than?.inclusive,
78
+ maxValue: checks.less_than?.value,
79
+ maxInclusive: checks.less_than?.inclusive,
80
+ multipleOf: checks.multiple_of?.value,
81
+ format: checks.number_format?.format,
82
+ optional,
83
+ },
84
+ };
85
+ },
86
+ int: (name, _prop, optional) => {
87
+ const prop = (0, exports.castInt)(_prop);
88
+ if (!prop)
89
+ return;
90
+ const meta = prop.meta();
91
+ const checks = metaChecks(prop._zod.def);
92
+ return {
93
+ int: {
94
+ name,
95
+ title: meta?.title,
96
+ description: meta?.description,
97
+ type: types_1.MetaType.int,
98
+ minValue: checks.greater_than?.value,
99
+ minInclusive: checks.greater_than?.inclusive,
100
+ maxValue: checks.less_than?.value,
101
+ maxInclusive: checks.less_than?.inclusive,
102
+ multipleOf: checks.multiple_of?.value,
103
+ format: checks.number_format?.format,
104
+ optional,
105
+ },
106
+ };
107
+ },
108
+ date: (name, _prop, optional) => {
109
+ const prop = (0, exports.castDate)(_prop);
110
+ if (!prop)
111
+ return;
112
+ const meta = prop.meta();
113
+ return {
114
+ date: {
115
+ name,
116
+ title: meta?.title,
117
+ description: meta?.description,
118
+ type: types_1.MetaType.date,
119
+ optional,
120
+ },
121
+ };
122
+ },
123
+ array: (name, _prop, optional) => {
124
+ const prop = (0, exports.castArray)(_prop);
125
+ if (!prop)
126
+ return;
127
+ const meta = prop.meta();
128
+ const inner = prop.unwrap();
129
+ const checks = metaChecks(prop._zod.def);
130
+ return {
131
+ array: {
132
+ name,
133
+ title: meta?.title,
134
+ description: meta?.description,
135
+ type: types_1.MetaType.array,
136
+ item: metaItem(`${name}[]`, inner),
137
+ minLength: checks.min_length?.minimum,
138
+ maxLength: checks.max_length?.maximum,
139
+ exactLength: checks.length_equals?.length,
140
+ optional,
141
+ },
142
+ };
143
+ },
144
+ record: (name, _prop, optional) => {
145
+ const prop = (0, exports.castRecord)(_prop);
146
+ if (!prop)
147
+ return;
148
+ const meta = prop.meta();
149
+ return {
150
+ record: {
151
+ name,
152
+ title: meta?.title,
153
+ description: meta?.description,
154
+ type: types_1.MetaType.record,
155
+ keyType: metaItem("keyType", prop.def.keyType),
156
+ valueType: metaItem("valueType", prop.def.valueType),
157
+ optional,
158
+ },
159
+ };
160
+ },
161
+ object: (name, _prop, optional) => {
162
+ const prop = (0, exports.castObject)(_prop);
163
+ if (!prop)
164
+ return;
165
+ const meta = prop.meta();
166
+ return {
167
+ object: {
168
+ name,
169
+ title: meta?.title,
170
+ description: meta?.description,
171
+ type: types_1.MetaType.object,
172
+ properties: (0, exports.metaItems)(prop.def.shape),
173
+ optional,
174
+ },
175
+ };
176
+ },
177
+ enum: (name, _prop, optional) => {
178
+ const prop = (0, exports.castEnum)(_prop);
179
+ if (!prop)
180
+ return;
181
+ const meta = prop.meta();
182
+ const entries = Object.entries(prop.def.entries).map(([key, value]) => {
183
+ const titles = Array.isArray(meta?.enumEntryTitles)
184
+ ? meta.enumEntryTitles
185
+ : [];
186
+ return {
187
+ key,
188
+ value,
189
+ title: titles.find((t) => t.value === value)?.title,
190
+ };
191
+ });
192
+ return {
193
+ enum: {
194
+ name,
195
+ title: meta?.title,
196
+ description: meta?.description,
197
+ type: types_1.MetaType.enum,
198
+ entries,
199
+ optional,
200
+ },
201
+ };
202
+ },
203
+ optional: (name, _prop) => {
204
+ const prop = (0, exports.castOptional)(_prop);
205
+ if (!prop)
206
+ return;
207
+ const inner = prop.unwrap();
208
+ return metaItem(name, inner, true);
209
+ },
210
+ };
211
+ const metaCheck = (def) => {
212
+ const checks = {};
213
+ const found = types_1.MetaCheckTypeList.find((i) => i.value === def.check);
214
+ if (!found)
215
+ throw new Error(`Check type ${def.check} not supported`);
216
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
217
+ checks[found.value] = def;
218
+ return checks;
219
+ };
220
+ const metaChecks = (type) => {
221
+ let results = {};
222
+ const checks = type.def?.checks ??
223
+ type.checks ??
224
+ [];
225
+ for (const check of checks)
226
+ results = { ...results, ...metaCheck(check._zod.def) };
227
+ return results;
228
+ };
229
+ const metaItem = (name, prop, optional) => {
230
+ for (const f of Object.values(metaFunctions)) {
231
+ const item = f(name, prop, optional);
232
+ if (item)
233
+ return item;
234
+ }
235
+ throw new Error(`Property ${name} (${prop._zod.def.type}) failed`);
236
+ };
237
+ const metaItems = (props) => {
238
+ const items = [];
239
+ for (const [name, prop] of Object.entries(props))
240
+ items.push(metaItem(name, prop));
241
+ return items;
242
+ };
243
+ exports.metaItems = metaItems;
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
19
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
20
+ };
21
+ var __importStar = (this && this.__importStar) || (function () {
22
+ var ownKeys = function(o) {
23
+ ownKeys = Object.getOwnPropertyNames || function (o) {
24
+ var ar = [];
25
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
26
+ return ar;
27
+ };
28
+ return ownKeys(o);
29
+ };
30
+ return function (mod) {
31
+ if (mod && mod.__esModule) return mod;
32
+ var result = {};
33
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
34
+ __setModuleDefault(result, mod);
35
+ return result;
36
+ };
37
+ })();
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ __exportStar(require("./lib"), exports);
40
+ // A default export can help some consumers using require()
41
+ const _all = __importStar(require("./lib"));
42
+ exports.default = _all;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./Meta"), exports);
18
+ __exportStar(require("./common"), exports);
19
+ __exportStar(require("./types"), exports);
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ // eslint-disable-next-line @typescript-eslint/triple-slash-reference
3
+ /// <reference path="./types/zod.d.ts" />
4
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
5
+ if (k2 === undefined) k2 = k;
6
+ var desc = Object.getOwnPropertyDescriptor(m, k);
7
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
8
+ desc = { enumerable: true, get: function() { return m[k]; } };
9
+ }
10
+ Object.defineProperty(o, k2, desc);
11
+ }) : (function(o, m, k, k2) {
12
+ if (k2 === undefined) k2 = k;
13
+ o[k2] = m[k];
14
+ }));
15
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
17
+ };
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ // Re-export your public types/exports so this file becomes the root .d.ts for the package.
20
+ // Adjust the relative path if your source layout differs.
21
+ __exportStar(require("./lib"), exports);
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MetaCheckTypeList = exports.MetaCheckType = exports.MetaInternalType = exports.MetaTypeList = exports.MetaType = void 0;
4
+ var MetaType;
5
+ (function (MetaType) {
6
+ MetaType["string"] = "string";
7
+ MetaType["number"] = "number";
8
+ MetaType["int"] = "int";
9
+ MetaType["date"] = "date";
10
+ MetaType["boolean"] = "boolean";
11
+ MetaType["array"] = "array";
12
+ MetaType["record"] = "record";
13
+ MetaType["object"] = "object";
14
+ MetaType["enum"] = "enum";
15
+ })(MetaType || (exports.MetaType = MetaType = {}));
16
+ exports.MetaTypeList = Object.entries(MetaType).map(([key, value]) => ({
17
+ key,
18
+ value,
19
+ }));
20
+ var MetaInternalType;
21
+ (function (MetaInternalType) {
22
+ MetaInternalType["optional"] = "optional";
23
+ })(MetaInternalType || (exports.MetaInternalType = MetaInternalType = {}));
24
+ var MetaCheckType;
25
+ (function (MetaCheckType) {
26
+ MetaCheckType["less_than"] = "less_than";
27
+ MetaCheckType["greater_than"] = "greater_than";
28
+ MetaCheckType["multiple_of"] = "multiple_of";
29
+ MetaCheckType["number_format"] = "number_format";
30
+ MetaCheckType["bigint_format"] = "bigint_format";
31
+ MetaCheckType["min_size"] = "min_size";
32
+ MetaCheckType["max_size"] = "max_size";
33
+ MetaCheckType["size_equals"] = "size_equals";
34
+ MetaCheckType["min_length"] = "min_length";
35
+ MetaCheckType["max_length"] = "max_length";
36
+ MetaCheckType["length_equals"] = "length_equals";
37
+ MetaCheckType["string_format"] = "string_format";
38
+ MetaCheckType["regex"] = "regex";
39
+ })(MetaCheckType || (exports.MetaCheckType = MetaCheckType = {}));
40
+ exports.MetaCheckTypeList = Object.entries(MetaCheckType).map(([key, value]) => ({
41
+ key,
42
+ value,
43
+ }));
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Meta = void 0;
4
+ const common_1 = require("./common");
5
+ class Meta {
6
+ constructor(props) {
7
+ this.props = props;
8
+ }
9
+ items() {
10
+ return (0, common_1.metaItems)(this.props);
11
+ }
12
+ }
13
+ exports.Meta = Meta;
@@ -0,0 +1,243 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.metaItems = exports.castEnum = exports.castOptional = exports.castRecord = exports.castObject = exports.castArray = exports.castDate = exports.castInt = exports.castNumber = exports.castString = exports.castBool = void 0;
4
+ const types_1 = require("./types");
5
+ const castType = (type, prop) => (prop._zod.def.type === type ? prop : undefined);
6
+ const castBool = (p) => castType(types_1.MetaType.boolean, p);
7
+ exports.castBool = castBool;
8
+ const castString = (p) => castType(types_1.MetaType.string, p);
9
+ exports.castString = castString;
10
+ const castNumber = (p) => castType(types_1.MetaType.number, p);
11
+ exports.castNumber = castNumber;
12
+ const castInt = (p) => castType(types_1.MetaType.int, p);
13
+ exports.castInt = castInt;
14
+ const castDate = (p) => castType(types_1.MetaType.date, p);
15
+ exports.castDate = castDate;
16
+ const castArray = (p) => castType(types_1.MetaType.array, p);
17
+ exports.castArray = castArray;
18
+ const castObject = (p) => castType(types_1.MetaType.object, p);
19
+ exports.castObject = castObject;
20
+ const castRecord = (p) => castType(types_1.MetaType.record, p);
21
+ exports.castRecord = castRecord;
22
+ const castOptional = (p) => castType(types_1.MetaInternalType.optional, p);
23
+ exports.castOptional = castOptional;
24
+ const castEnum = (p) => castType(types_1.MetaType.enum, p);
25
+ exports.castEnum = castEnum;
26
+ const metaFunctions = {
27
+ boolean: (name, _prop, optional) => {
28
+ const prop = (0, exports.castBool)(_prop);
29
+ if (!prop)
30
+ return;
31
+ const meta = prop.meta();
32
+ return {
33
+ boolean: {
34
+ name,
35
+ title: meta?.title,
36
+ description: meta?.description,
37
+ type: types_1.MetaType.boolean,
38
+ optional,
39
+ },
40
+ };
41
+ },
42
+ string: (name, _prop, optional) => {
43
+ const prop = (0, exports.castString)(_prop);
44
+ if (!prop)
45
+ return;
46
+ const meta = prop.meta();
47
+ const checks = metaChecks(prop._zod.def);
48
+ return {
49
+ string: {
50
+ name,
51
+ title: meta?.title,
52
+ description: meta?.description,
53
+ type: types_1.MetaType.string,
54
+ minLength: checks.min_length?.minimum,
55
+ maxLength: checks.max_length?.maximum,
56
+ exactLength: checks.length_equals?.length,
57
+ regex: checks.regex?.pattern?.toString() ??
58
+ checks.string_format?.pattern?.toString(),
59
+ format: checks.regex?.format ?? checks.string_format?.format,
60
+ optional,
61
+ },
62
+ };
63
+ },
64
+ number: (name, _prop, optional) => {
65
+ const prop = (0, exports.castNumber)(_prop);
66
+ if (!prop)
67
+ return;
68
+ const meta = prop.meta();
69
+ const checks = metaChecks(prop._zod.def);
70
+ return {
71
+ number: {
72
+ name,
73
+ title: meta?.title,
74
+ description: meta?.description,
75
+ type: types_1.MetaType.number,
76
+ minValue: checks.greater_than?.value,
77
+ minInclusive: checks.greater_than?.inclusive,
78
+ maxValue: checks.less_than?.value,
79
+ maxInclusive: checks.less_than?.inclusive,
80
+ multipleOf: checks.multiple_of?.value,
81
+ format: checks.number_format?.format,
82
+ optional,
83
+ },
84
+ };
85
+ },
86
+ int: (name, _prop, optional) => {
87
+ const prop = (0, exports.castInt)(_prop);
88
+ if (!prop)
89
+ return;
90
+ const meta = prop.meta();
91
+ const checks = metaChecks(prop._zod.def);
92
+ return {
93
+ int: {
94
+ name,
95
+ title: meta?.title,
96
+ description: meta?.description,
97
+ type: types_1.MetaType.int,
98
+ minValue: checks.greater_than?.value,
99
+ minInclusive: checks.greater_than?.inclusive,
100
+ maxValue: checks.less_than?.value,
101
+ maxInclusive: checks.less_than?.inclusive,
102
+ multipleOf: checks.multiple_of?.value,
103
+ format: checks.number_format?.format,
104
+ optional,
105
+ },
106
+ };
107
+ },
108
+ date: (name, _prop, optional) => {
109
+ const prop = (0, exports.castDate)(_prop);
110
+ if (!prop)
111
+ return;
112
+ const meta = prop.meta();
113
+ return {
114
+ date: {
115
+ name,
116
+ title: meta?.title,
117
+ description: meta?.description,
118
+ type: types_1.MetaType.date,
119
+ optional,
120
+ },
121
+ };
122
+ },
123
+ array: (name, _prop, optional) => {
124
+ const prop = (0, exports.castArray)(_prop);
125
+ if (!prop)
126
+ return;
127
+ const meta = prop.meta();
128
+ const inner = prop.unwrap();
129
+ const checks = metaChecks(prop._zod.def);
130
+ return {
131
+ array: {
132
+ name,
133
+ title: meta?.title,
134
+ description: meta?.description,
135
+ type: types_1.MetaType.array,
136
+ item: metaItem(`${name}[]`, inner),
137
+ minLength: checks.min_length?.minimum,
138
+ maxLength: checks.max_length?.maximum,
139
+ exactLength: checks.length_equals?.length,
140
+ optional,
141
+ },
142
+ };
143
+ },
144
+ record: (name, _prop, optional) => {
145
+ const prop = (0, exports.castRecord)(_prop);
146
+ if (!prop)
147
+ return;
148
+ const meta = prop.meta();
149
+ return {
150
+ record: {
151
+ name,
152
+ title: meta?.title,
153
+ description: meta?.description,
154
+ type: types_1.MetaType.record,
155
+ keyType: metaItem("keyType", prop.def.keyType),
156
+ valueType: metaItem("valueType", prop.def.valueType),
157
+ optional,
158
+ },
159
+ };
160
+ },
161
+ object: (name, _prop, optional) => {
162
+ const prop = (0, exports.castObject)(_prop);
163
+ if (!prop)
164
+ return;
165
+ const meta = prop.meta();
166
+ return {
167
+ object: {
168
+ name,
169
+ title: meta?.title,
170
+ description: meta?.description,
171
+ type: types_1.MetaType.object,
172
+ properties: (0, exports.metaItems)(prop.def.shape),
173
+ optional,
174
+ },
175
+ };
176
+ },
177
+ enum: (name, _prop, optional) => {
178
+ const prop = (0, exports.castEnum)(_prop);
179
+ if (!prop)
180
+ return;
181
+ const meta = prop.meta();
182
+ const entries = Object.entries(prop.def.entries).map(([key, value]) => {
183
+ const titles = Array.isArray(meta?.enumEntryTitles)
184
+ ? meta.enumEntryTitles
185
+ : [];
186
+ return {
187
+ key,
188
+ value,
189
+ title: titles.find((t) => t.value === value)?.title,
190
+ };
191
+ });
192
+ return {
193
+ enum: {
194
+ name,
195
+ title: meta?.title,
196
+ description: meta?.description,
197
+ type: types_1.MetaType.enum,
198
+ entries,
199
+ optional,
200
+ },
201
+ };
202
+ },
203
+ optional: (name, _prop) => {
204
+ const prop = (0, exports.castOptional)(_prop);
205
+ if (!prop)
206
+ return;
207
+ const inner = prop.unwrap();
208
+ return metaItem(name, inner, true);
209
+ },
210
+ };
211
+ const metaCheck = (def) => {
212
+ const checks = {};
213
+ const found = types_1.MetaCheckTypeList.find((i) => i.value === def.check);
214
+ if (!found)
215
+ throw new Error(`Check type ${def.check} not supported`);
216
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
217
+ checks[found.value] = def;
218
+ return checks;
219
+ };
220
+ const metaChecks = (type) => {
221
+ let results = {};
222
+ const checks = type.def?.checks ??
223
+ type.checks ??
224
+ [];
225
+ for (const check of checks)
226
+ results = { ...results, ...metaCheck(check._zod.def) };
227
+ return results;
228
+ };
229
+ const metaItem = (name, prop, optional) => {
230
+ for (const f of Object.values(metaFunctions)) {
231
+ const item = f(name, prop, optional);
232
+ if (item)
233
+ return item;
234
+ }
235
+ throw new Error(`Property ${name} (${prop._zod.def.type}) failed`);
236
+ };
237
+ const metaItems = (props) => {
238
+ const items = [];
239
+ for (const [name, prop] of Object.entries(props))
240
+ items.push(metaItem(name, prop));
241
+ return items;
242
+ };
243
+ exports.metaItems = metaItems;
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
19
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
20
+ };
21
+ var __importStar = (this && this.__importStar) || (function () {
22
+ var ownKeys = function(o) {
23
+ ownKeys = Object.getOwnPropertyNames || function (o) {
24
+ var ar = [];
25
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
26
+ return ar;
27
+ };
28
+ return ownKeys(o);
29
+ };
30
+ return function (mod) {
31
+ if (mod && mod.__esModule) return mod;
32
+ var result = {};
33
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
34
+ __setModuleDefault(result, mod);
35
+ return result;
36
+ };
37
+ })();
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ __exportStar(require("./lib"), exports);
40
+ // A default export can help some consumers using require()
41
+ const _all = __importStar(require("./lib"));
42
+ exports.default = _all;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./Meta"), exports);
18
+ __exportStar(require("./common"), exports);
19
+ __exportStar(require("./types"), exports);
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ // eslint-disable-next-line @typescript-eslint/triple-slash-reference
3
+ /// <reference path="./types/zod.d.ts" />
4
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
5
+ if (k2 === undefined) k2 = k;
6
+ var desc = Object.getOwnPropertyDescriptor(m, k);
7
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
8
+ desc = { enumerable: true, get: function() { return m[k]; } };
9
+ }
10
+ Object.defineProperty(o, k2, desc);
11
+ }) : (function(o, m, k, k2) {
12
+ if (k2 === undefined) k2 = k;
13
+ o[k2] = m[k];
14
+ }));
15
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
17
+ };
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ // Re-export your public types/exports so this file becomes the root .d.ts for the package.
20
+ // Adjust the relative path if your source layout differs.
21
+ __exportStar(require("./lib"), exports);
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MetaCheckTypeList = exports.MetaCheckType = exports.MetaInternalType = exports.MetaTypeList = exports.MetaType = void 0;
4
+ var MetaType;
5
+ (function (MetaType) {
6
+ MetaType["string"] = "string";
7
+ MetaType["number"] = "number";
8
+ MetaType["int"] = "int";
9
+ MetaType["date"] = "date";
10
+ MetaType["boolean"] = "boolean";
11
+ MetaType["array"] = "array";
12
+ MetaType["record"] = "record";
13
+ MetaType["object"] = "object";
14
+ MetaType["enum"] = "enum";
15
+ })(MetaType || (exports.MetaType = MetaType = {}));
16
+ exports.MetaTypeList = Object.entries(MetaType).map(([key, value]) => ({
17
+ key,
18
+ value,
19
+ }));
20
+ var MetaInternalType;
21
+ (function (MetaInternalType) {
22
+ MetaInternalType["optional"] = "optional";
23
+ })(MetaInternalType || (exports.MetaInternalType = MetaInternalType = {}));
24
+ var MetaCheckType;
25
+ (function (MetaCheckType) {
26
+ MetaCheckType["less_than"] = "less_than";
27
+ MetaCheckType["greater_than"] = "greater_than";
28
+ MetaCheckType["multiple_of"] = "multiple_of";
29
+ MetaCheckType["number_format"] = "number_format";
30
+ MetaCheckType["bigint_format"] = "bigint_format";
31
+ MetaCheckType["min_size"] = "min_size";
32
+ MetaCheckType["max_size"] = "max_size";
33
+ MetaCheckType["size_equals"] = "size_equals";
34
+ MetaCheckType["min_length"] = "min_length";
35
+ MetaCheckType["max_length"] = "max_length";
36
+ MetaCheckType["length_equals"] = "length_equals";
37
+ MetaCheckType["string_format"] = "string_format";
38
+ MetaCheckType["regex"] = "regex";
39
+ })(MetaCheckType || (exports.MetaCheckType = MetaCheckType = {}));
40
+ exports.MetaCheckTypeList = Object.entries(MetaCheckType).map(([key, value]) => ({
41
+ key,
42
+ value,
43
+ }));
@@ -0,0 +1,7 @@
1
+ import { z } from "zod";
2
+ import { MetaItem } from "./types";
3
+ export declare class Meta {
4
+ protected readonly props: z.ZodRawShape;
5
+ constructor(props: z.ZodRawShape);
6
+ items(): MetaItem[];
7
+ }
@@ -0,0 +1,14 @@
1
+ import { z } from "zod";
2
+ import { $ZodType } from "zod/v4/core";
3
+ import { MetaItem } from "./types";
4
+ export declare const castBool: (p: $ZodType) => z.ZodBoolean | undefined;
5
+ export declare const castString: (p: $ZodType) => z.ZodString | undefined;
6
+ export declare const castNumber: (p: $ZodType) => z.ZodNumber | undefined;
7
+ export declare const castInt: (p: $ZodType) => z.ZodInt | undefined;
8
+ export declare const castDate: (p: $ZodType) => z.ZodDate | undefined;
9
+ export declare const castArray: (p: $ZodType) => z.ZodArray<z.core.$ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>> | undefined;
10
+ export declare const castObject: (p: $ZodType) => z.ZodObject<z.core.$ZodLooseShape, z.core.$strip> | undefined;
11
+ export declare const castRecord: (p: $ZodType) => z.ZodRecord<z.core.$ZodRecordKey, z.core.$ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>> | undefined;
12
+ export declare const castOptional: (p: $ZodType) => z.ZodOptional<z.core.$ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>> | undefined;
13
+ export declare const castEnum: (p: $ZodType) => z.ZodEnum<Readonly<Record<string, z.core.util.EnumValue>>> | undefined;
14
+ export declare const metaItems: (props: z.ZodRawShape) => MetaItem[];
@@ -0,0 +1,3 @@
1
+ export * from "./lib";
2
+ import * as _all from "./lib";
3
+ export default _all;
@@ -0,0 +1,3 @@
1
+ export * from "./Meta";
2
+ export * from "./common";
3
+ export * from "./types";
@@ -0,0 +1 @@
1
+ export * from "./lib";
@@ -0,0 +1,140 @@
1
+ import { $ZodCheckBigIntFormatDef, $ZodCheckGreaterThanDef, $ZodCheckLengthEqualsDef, $ZodCheckLessThanDef, $ZodCheckMaxLengthDef, $ZodCheckMaxSizeDef, $ZodCheckMinLengthDef, $ZodCheckMinSizeDef, $ZodCheckMultipleOfDef, $ZodCheckNumberFormatDef, $ZodCheckRegexDef, $ZodCheckSizeEqualsDef, $ZodCheckStringFormatDef, $ZodNumberFormats, $ZodStringFormats, $ZodType } from "zod/v4/core";
2
+ export declare enum MetaType {
3
+ string = "string",
4
+ number = "number",
5
+ int = "int",
6
+ date = "date",
7
+ boolean = "boolean",
8
+ array = "array",
9
+ record = "record",
10
+ object = "object",
11
+ enum = "enum"
12
+ }
13
+ export declare const MetaTypeList: {
14
+ key: string;
15
+ value: MetaType;
16
+ }[];
17
+ export declare enum MetaInternalType {
18
+ optional = "optional"
19
+ }
20
+ export declare enum MetaCheckType {
21
+ less_than = "less_than",
22
+ greater_than = "greater_than",
23
+ multiple_of = "multiple_of",
24
+ number_format = "number_format",
25
+ bigint_format = "bigint_format",
26
+ min_size = "min_size",
27
+ max_size = "max_size",
28
+ size_equals = "size_equals",
29
+ min_length = "min_length",
30
+ max_length = "max_length",
31
+ length_equals = "length_equals",
32
+ string_format = "string_format",
33
+ regex = "regex"
34
+ }
35
+ export declare const MetaCheckTypeList: {
36
+ key: string;
37
+ value: MetaCheckType;
38
+ }[];
39
+ export interface MetaChecks {
40
+ [MetaCheckType.bigint_format]?: $ZodCheckBigIntFormatDef;
41
+ [MetaCheckType.less_than]?: $ZodCheckLessThanDef;
42
+ [MetaCheckType.greater_than]?: $ZodCheckGreaterThanDef;
43
+ [MetaCheckType.multiple_of]?: $ZodCheckMultipleOfDef;
44
+ [MetaCheckType.number_format]?: $ZodCheckNumberFormatDef;
45
+ [MetaCheckType.min_size]?: $ZodCheckMinSizeDef;
46
+ [MetaCheckType.max_size]?: $ZodCheckMaxSizeDef;
47
+ [MetaCheckType.size_equals]?: $ZodCheckSizeEqualsDef;
48
+ [MetaCheckType.min_length]?: $ZodCheckMinLengthDef;
49
+ [MetaCheckType.max_length]?: $ZodCheckMaxLengthDef;
50
+ [MetaCheckType.length_equals]?: $ZodCheckLengthEqualsDef;
51
+ [MetaCheckType.string_format]?: $ZodCheckStringFormatDef;
52
+ [MetaCheckType.regex]?: $ZodCheckRegexDef;
53
+ }
54
+ export interface MetaBase {
55
+ type: MetaType;
56
+ name: string;
57
+ title?: string;
58
+ description?: string;
59
+ optional?: boolean;
60
+ }
61
+ export interface MetaString extends MetaBase {
62
+ type: MetaType.string;
63
+ minLength?: number;
64
+ maxLength?: number;
65
+ exactLength?: number;
66
+ regex?: string;
67
+ format?: $ZodStringFormats | string;
68
+ }
69
+ export interface MetaBool extends MetaBase {
70
+ type: MetaType.boolean;
71
+ }
72
+ export interface MetaNumber extends MetaBase {
73
+ type: MetaType.number;
74
+ minValue?: number;
75
+ minInclusive?: boolean;
76
+ maxValue?: number;
77
+ maxInclusive?: boolean;
78
+ multipleOf?: number;
79
+ format?: $ZodNumberFormats;
80
+ }
81
+ export interface MetaInt extends MetaBase {
82
+ type: MetaType.int;
83
+ minValue?: number;
84
+ minInclusive?: boolean;
85
+ maxValue?: number;
86
+ maxInclusive?: boolean;
87
+ multipleOf?: number;
88
+ format?: $ZodNumberFormats;
89
+ }
90
+ export interface MetaDate extends MetaBase {
91
+ type: MetaType.date;
92
+ }
93
+ export interface MetaArray extends MetaBase {
94
+ type: MetaType.array;
95
+ item: MetaItem;
96
+ minLength?: number;
97
+ maxLength?: number;
98
+ exactLength?: number;
99
+ }
100
+ export interface MetaRecord extends MetaBase {
101
+ type: MetaType.record;
102
+ keyType: MetaItem;
103
+ valueType: MetaItem;
104
+ }
105
+ export interface MetaObject extends MetaBase {
106
+ type: MetaType.object;
107
+ properties: MetaItem | MetaItem[];
108
+ }
109
+ export interface MetaEnumEntry {
110
+ key: string;
111
+ value: number | string;
112
+ title?: string;
113
+ }
114
+ export interface MetaEnumEntryTitle {
115
+ value: string;
116
+ title?: string;
117
+ }
118
+ export interface MetaEnum extends MetaBase {
119
+ type: MetaType.enum;
120
+ entries: MetaEnumEntry[];
121
+ }
122
+ export interface MetaOptional extends MetaBase {
123
+ type: MetaType.record;
124
+ inner: MetaItem;
125
+ }
126
+ export interface MetaItem {
127
+ boolean?: MetaBool;
128
+ string?: MetaString;
129
+ number?: MetaNumber;
130
+ int?: MetaInt;
131
+ date?: MetaDate;
132
+ array?: MetaArray;
133
+ record?: MetaRecord;
134
+ object?: MetaObject;
135
+ enum?: MetaEnum;
136
+ }
137
+ export type MetaFunc = (name: string, prop: $ZodType, optional?: boolean) => MetaItem | undefined;
138
+ export type MetaFuncs = {
139
+ [key in MetaType | MetaInternalType]: MetaFunc;
140
+ };
package/package.json ADDED
@@ -0,0 +1,63 @@
1
+ {
2
+ "name": "@mxpicture/zod-meta",
3
+ "version": "0.1.0",
4
+ "description": "Zod Metadata Extraction, publishing both CommonJS and ESM builds",
5
+ "author": "MXPicture",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/MXPicture/node-library-zod-meta"
10
+ },
11
+ "main": "dist/cjs/index.js",
12
+ "module": "dist/esm/index.js",
13
+ "types": "dist/types/package.d.ts",
14
+ "exports": {
15
+ ".": {
16
+ "types": "./dist/types/package.d.ts",
17
+ "import": "./dist/esm/index.js",
18
+ "require": "./dist/cjs/index.js"
19
+ },
20
+ "./package.json": "./package.json"
21
+ },
22
+ "files": [
23
+ "dist/"
24
+ ],
25
+ "engines": {
26
+ "node": "24"
27
+ },
28
+ "scripts": {
29
+ "commit": "git-cz",
30
+ "prepare": "husky install && npm run build",
31
+ "clean": "rm -rf dist .tsbuildinfo",
32
+ "build:types": "tsc -p tsconfig.types.json",
33
+ "build:cjs": "tsc -p tsconfig.cjs.json",
34
+ "build:esm": "tsc -p tsconfig.esm.json",
35
+ "build": "npm run clean && npm run build:types && npm run build:cjs && npm run build:esm",
36
+ "lint": "eslint \"src/**/*.{ts,tsx}\" --ext .ts,.tsx",
37
+ "update": "ncu -u && npm install"
38
+ },
39
+ "config": {
40
+ "commitizen": {
41
+ "path": "./node_modules/cz-conventional-changelog"
42
+ }
43
+ },
44
+ "dependencies": {
45
+ "zod": "^4.3.5"
46
+ },
47
+ "devDependencies": {
48
+ "@commitlint/cli": "^20.3.1",
49
+ "@commitlint/config-conventional": "^20.3.1",
50
+ "@eslint/compat": "^2.0.1",
51
+ "@types/node": "^25.0.9",
52
+ "@typescript-eslint/eslint-plugin": "^8.53.1",
53
+ "@typescript-eslint/parser": "^8.53.1",
54
+ "commitizen": "^4.3.1",
55
+ "cz-conventional-changelog": "^3.3.0",
56
+ "eslint": "^9.39.2",
57
+ "eslint-config-google": "^0.14.0",
58
+ "eslint-plugin-import": "^2.32.0",
59
+ "eslint-plugin-tsdoc": "^0.5.0",
60
+ "husky": "^8.0.0",
61
+ "typescript": "^5.9.3"
62
+ }
63
+ }