@zodec/core 0.0.1

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,110 @@
1
+ # @zodec/core
2
+
3
+ zodec = zod<functional-schema> ++ class-validator<clean-declarative-style>
4
+
5
+ Define schemas inline within the class definitions
6
+
7
+ A TypeScript library that generates model schemas(zod) using *decorators*.
8
+ and *keep validation logic close to your models*.
9
+
10
+
11
+ ## Installation
12
+
13
+ ```bash
14
+ npm install @zodec/core zod reflect-metadata
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ### Basic Example: Pizza Schema
20
+
21
+ Decorate the class attributes with `@zatt` decorators to automatically generate a Zod schema:
22
+
23
+ ```typescript
24
+ import "reflect-metadata";
25
+ import { z } from "zod";
26
+ import { zatt, Zodec } from "@zodec/core";
27
+
28
+ class Pizza {
29
+ @zatt(z.string())
30
+ name: string;
31
+
32
+ @zatt(z.number().positive())
33
+ price: number;
34
+
35
+ @zatt(z.array(z.string()))
36
+ toppings: string[];
37
+
38
+ @zatt(z.boolean().optional())
39
+ isVeg?: boolean;
40
+ }
41
+
42
+ // Get the generated schema
43
+ const pizzaSchema = Zodec.of(Pizza);
44
+
45
+ // Validate data
46
+ const result = pizzaSchema.parse({
47
+ name: "Margherita",
48
+ price: 12.99,
49
+ toppings: ["mozzarella", "tomato", "basil"],
50
+ isVeg: false
51
+ });
52
+ ```
53
+
54
+
55
+
56
+
57
+ ### Decorators
58
+
59
+ #### `@zclass(zodSchema)`
60
+ Apply a pre-made Zod schema object to an entire class. Use this when you've already created a complete schema elsewhere:
61
+
62
+ ```typescript
63
+ const myClassSchema = z.object({
64
+ name: z.string(),
65
+ age: z.number()
66
+ });
67
+
68
+ @zclass(myClassSchema)
69
+ class MyClass {
70
+ }
71
+ ```
72
+
73
+ #### `@zatt(zodSchema)`
74
+ Decorate individual class properties with their Zod schemas. Each property gets its own validation rules:
75
+
76
+ ```typescript
77
+ class Product {
78
+ @zatt(z.string().min(1))
79
+ name: string;
80
+
81
+ @zatt(z.number().positive())
82
+ price: number;
83
+ }
84
+ ```
85
+
86
+
87
+ ## Getting the Schema
88
+
89
+ Use `Zodec.of()` to retrieve the generated schema:
90
+
91
+ ```typescript
92
+ const schema = Zodec.of(Pizza);
93
+
94
+ if(schema){
95
+ const validated = schema.parse(data);
96
+ }
97
+ ```
98
+
99
+ > ⚠️ This library requires `reflect-metadata` to be imported once in your app entry point.
100
+
101
+
102
+ ## Benefits
103
+
104
+ - **Type-safe**: Leverages TypeScript types alongside Zod validation
105
+ - **Declarative**: Define schemas inline with your class definitions
106
+ - **Reusable**: Generate schemas once, validate multiple times
107
+ - **Composable**: Nest objects and arrays with full Zod support
108
+
109
+
110
+
@@ -0,0 +1,3 @@
1
+ export declare const ZODEC__SCHEMA = "zodec:schema";
2
+ export declare const ZODEC__TYPE = "zodec:type";
3
+ //# sourceMappingURL=constants.zodec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.zodec.d.ts","sourceRoot":"","sources":["../src/constants.zodec.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,aAAa,iBAAiB,CAAA;AAC3C,eAAO,MAAM,WAAW,eAAe,CAAA"}
@@ -0,0 +1,11 @@
1
+ import * as z from "zod";
2
+ /**
3
+ * zatt: zod attribute
4
+ * decorate a property with a zod schema
5
+ * this extends the class schema
6
+ * > use .describe() | for descriptions
7
+ * @param attributeSchema
8
+ * @returns
9
+ */
10
+ export declare function zatt<T>(attributeSchema: z.ZodType<T>): PropertyDecorator;
11
+ //# sourceMappingURL=zatt.decorator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"zatt.decorator.d.ts","sourceRoot":"","sources":["../../src/decorators/zatt.decorator.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AAEzB;;;;;;;GAOG;AACH,wBAAgB,IAAI,CAAC,CAAC,EAAE,eAAe,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,iBAAiB,CAuBxE"}
@@ -0,0 +1,4 @@
1
+ import 'reflect-metadata';
2
+ import * as z from 'zod';
3
+ export declare function zclass(schema: z.ZodType<any>): ClassDecorator;
4
+ //# sourceMappingURL=zclass.decorator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"zclass.decorator.d.ts","sourceRoot":"","sources":["../../src/decorators/zclass.decorator.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAE1B,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AAEzB,wBAAgB,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,cAAc,CAW7D"}
package/dist/index.cjs ADDED
@@ -0,0 +1,94 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // packages/zodec/core/src/index.ts
31
+ var src_exports = {};
32
+ __export(src_exports, {
33
+ ZODEC__SCHEMA: () => ZODEC__SCHEMA,
34
+ ZODEC__TYPE: () => ZODEC__TYPE,
35
+ Zodec: () => Zodec,
36
+ ZodifyProvider: () => ZodifyProvider,
37
+ zatt: () => zatt,
38
+ zclass: () => zclass
39
+ });
40
+ module.exports = __toCommonJS(src_exports);
41
+
42
+ // packages/zodec/core/src/zodec.ts
43
+ var import_reflect_metadata = require("reflect-metadata");
44
+
45
+ // packages/zodec/core/src/constants.zodec.ts
46
+ var ZODEC__SCHEMA = "zodec:schema";
47
+ var ZODEC__TYPE = "zodec:type";
48
+
49
+ // packages/zodec/core/src/zodec.ts
50
+ var ZodifyProvider = class {
51
+ };
52
+ var Zodec = class {
53
+ static of(target) {
54
+ return Reflect.getMetadata(ZODEC__SCHEMA, target);
55
+ }
56
+ };
57
+
58
+ // packages/zodec/core/src/decorators/zatt.decorator.ts
59
+ var z = __toESM(require("zod"), 1);
60
+ function zatt(attributeSchema) {
61
+ return (target, propertyKey) => {
62
+ try {
63
+ let targetSchema = Reflect.getMetadata(ZODEC__SCHEMA, target.constructor);
64
+ if (!targetSchema) {
65
+ targetSchema = z.object({});
66
+ }
67
+ if (attributeSchema) {
68
+ targetSchema = targetSchema.extend({
69
+ [propertyKey.toString()]: attributeSchema
70
+ });
71
+ }
72
+ Reflect.defineMetadata(ZODEC__SCHEMA, targetSchema, target.constructor);
73
+ } catch (ex) {
74
+ console.error("zatt/decorator/ex", ex);
75
+ }
76
+ };
77
+ }
78
+
79
+ // packages/zodec/core/src/decorators/zclass.decorator.ts
80
+ var import_reflect_metadata2 = require("reflect-metadata");
81
+ function zclass(schema) {
82
+ return (target) => {
83
+ Reflect.defineMetadata(ZODEC__SCHEMA, schema, target);
84
+ };
85
+ }
86
+ // Annotate the CommonJS export names for ESM import in node:
87
+ 0 && (module.exports = {
88
+ ZODEC__SCHEMA,
89
+ ZODEC__TYPE,
90
+ Zodec,
91
+ ZodifyProvider,
92
+ zatt,
93
+ zclass
94
+ });
@@ -0,0 +1,5 @@
1
+ export * from 'src/zodec';
2
+ export * from 'src/decorators/zatt.decorator';
3
+ export * from 'src/decorators/zclass.decorator';
4
+ export * from 'src/constants.zodec';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAA;AACzB,cAAc,+BAA+B,CAAA;AAC7C,cAAc,iCAAiC,CAAA;AAC/C,cAAc,qBAAqB,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,52 @@
1
+ // packages/zodec/core/src/zodec.ts
2
+ import "reflect-metadata";
3
+
4
+ // packages/zodec/core/src/constants.zodec.ts
5
+ var ZODEC__SCHEMA = "zodec:schema";
6
+ var ZODEC__TYPE = "zodec:type";
7
+
8
+ // packages/zodec/core/src/zodec.ts
9
+ var ZodifyProvider = class {
10
+ };
11
+ var Zodec = class {
12
+ static of(target) {
13
+ return Reflect.getMetadata(ZODEC__SCHEMA, target);
14
+ }
15
+ };
16
+
17
+ // packages/zodec/core/src/decorators/zatt.decorator.ts
18
+ import * as z from "zod";
19
+ function zatt(attributeSchema) {
20
+ return (target, propertyKey) => {
21
+ try {
22
+ let targetSchema = Reflect.getMetadata(ZODEC__SCHEMA, target.constructor);
23
+ if (!targetSchema) {
24
+ targetSchema = z.object({});
25
+ }
26
+ if (attributeSchema) {
27
+ targetSchema = targetSchema.extend({
28
+ [propertyKey.toString()]: attributeSchema
29
+ });
30
+ }
31
+ Reflect.defineMetadata(ZODEC__SCHEMA, targetSchema, target.constructor);
32
+ } catch (ex) {
33
+ console.error("zatt/decorator/ex", ex);
34
+ }
35
+ };
36
+ }
37
+
38
+ // packages/zodec/core/src/decorators/zclass.decorator.ts
39
+ import "reflect-metadata";
40
+ function zclass(schema) {
41
+ return (target) => {
42
+ Reflect.defineMetadata(ZODEC__SCHEMA, schema, target);
43
+ };
44
+ }
45
+ export {
46
+ ZODEC__SCHEMA,
47
+ ZODEC__TYPE,
48
+ Zodec,
49
+ ZodifyProvider,
50
+ zatt,
51
+ zclass
52
+ };
@@ -0,0 +1,8 @@
1
+ import "reflect-metadata";
2
+ import { ZodType } from "zod";
3
+ export declare class ZodifyProvider {
4
+ }
5
+ export declare class Zodec {
6
+ static of(target: any): ZodType<any> | undefined;
7
+ }
8
+ //# sourceMappingURL=zodec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"zodec.d.ts","sourceRoot":"","sources":["../src/zodec.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAC1B,OAAO,EAAE,OAAO,EAAE,MAAM,KAAK,CAAC;AAK9B,qBAAa,cAAc;CAE1B;AAID,qBAAa,KAAK;IACd,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,GAAG,SAAS;CAGnD"}
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@zodec/core",
3
+ "version": "0.0.1",
4
+ "type": "module",
5
+ "main": "./dist/index.cjs",
6
+ "module": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ "./package.json": "./package.json",
10
+ ".": {
11
+ "development": "./src/index.ts",
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.js",
14
+ "require": "./dist/index.cjs",
15
+ "default": "./dist/index.js"
16
+ }
17
+ },
18
+ "files": [
19
+ "dist",
20
+ "!**/*.tsbuildinfo"
21
+ ],
22
+ "nx": {
23
+ "name": "zodec-core",
24
+ "targets": {
25
+ "build": {
26
+ "executor": "@nx/esbuild:esbuild",
27
+ "outputs": [
28
+ "{options.outputPath}"
29
+ ],
30
+ "options": {
31
+ "outputPath": "packages/zodec/core/dist",
32
+ "main": "packages/zodec/core/src/index.ts",
33
+ "tsConfig": "packages/zodec/core/tsconfig.lib.json",
34
+ "format": [
35
+ "esm",
36
+ "cjs"
37
+ ],
38
+ "declarationRootDir": "packages/zodec/core/src"
39
+ }
40
+ }
41
+ }
42
+ },
43
+ "dependencies": {},
44
+ "peerDependencies": {
45
+ "zod": "^4.3.5",
46
+ "reflect-metadata": "^0.1.14"
47
+ },
48
+ "publishConfig": {
49
+ "access": "public"
50
+ }
51
+ }