@zodec/core 0.0.4 → 0.0.6

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.
@@ -0,0 +1,2 @@
1
+ export const ZODEC__SCHEMA = "zodec:schema";
2
+ export const ZODEC__TYPE = "zodec:type";
@@ -0,0 +1,32 @@
1
+ import { ZODEC__SCHEMA } from "../constants.zodec.js";
2
+ import * as z from "zod";
3
+ /**
4
+ * zatt: zod attribute
5
+ * decorate a property with a zod schema
6
+ * this extends the class schema
7
+ * > use .describe() | for descriptions
8
+ * @param attributeSchema
9
+ * @returns
10
+ */
11
+ export function zatt(attributeSchema) {
12
+ return (target, propertyKey) => {
13
+ try {
14
+ // check if the target has existing ZODEC__SCHEMA
15
+ let targetSchema = Reflect.getMetadata(ZODEC__SCHEMA, target.constructor);
16
+ if (!targetSchema) {
17
+ // create a new ZodObject schema if none exists
18
+ targetSchema = z.object({});
19
+ }
20
+ // extend the existing schema with the new property
21
+ if (attributeSchema) {
22
+ targetSchema = targetSchema.extend({
23
+ [propertyKey.toString()]: attributeSchema,
24
+ });
25
+ }
26
+ Reflect.defineMetadata(ZODEC__SCHEMA, targetSchema, target.constructor);
27
+ }
28
+ catch (ex) {
29
+ console.error("zatt/decorator/ex", ex);
30
+ }
31
+ };
32
+ }
@@ -0,0 +1,18 @@
1
+ import 'reflect-metadata';
2
+ import { ZODEC__SCHEMA } from '../constants.zodec.js';
3
+ /**
4
+ * decorate a class by providing defined zod schema object
5
+ * @param schema
6
+ * @returns
7
+ */
8
+ export function zclass(schema) {
9
+ return (target) => {
10
+ // schema
11
+ Reflect.defineMetadata(ZODEC__SCHEMA, schema, target);
12
+ // inferred type > is not a runtime entity
13
+ // type inferred = z.infer<typeof schema>;
14
+ // Reflect.defineMetadata(ZODIFY__TYPE, inferred, target)
15
+ // Reflect.defineMetadata(ZODIFY__TYPE, )
16
+ // schema.parse(undefined) as ReturnType<typeof schema.parse>
17
+ };
18
+ }
package/dist/index.d.ts CHANGED
@@ -1,5 +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';
1
+ export * from './zodec.js';
2
+ export * from './decorators/zatt.decorator.js';
3
+ export * from './decorators/zclass.decorator.js';
4
+ export * from './constants.zodec.js';
5
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +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"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;AAC1B,cAAc,gCAAgC,CAAA;AAC9C,cAAc,kCAAkC,CAAA;AAChD,cAAc,sBAAsB,CAAA"}
package/dist/index.js CHANGED
@@ -1,55 +1,4 @@
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 Zodec = class {
10
- /**
11
- * Retrieves the Zod schema associated with a class.
12
- *
13
- * @param target - The class or object to retrieve the schema from.
14
- * @returns The Zod schema if found, otherwise undefined.
15
- */
16
- static of(target) {
17
- return Reflect.getMetadata(ZODEC__SCHEMA, target);
18
- }
19
- };
20
-
21
- // packages/zodec/core/src/decorators/zatt.decorator.ts
22
- import * as z from "zod";
23
- function zatt(attributeSchema) {
24
- return (target, propertyKey) => {
25
- try {
26
- let targetSchema = Reflect.getMetadata(ZODEC__SCHEMA, target.constructor);
27
- if (!targetSchema) {
28
- targetSchema = z.object({});
29
- }
30
- if (attributeSchema) {
31
- targetSchema = targetSchema.extend({
32
- [propertyKey.toString()]: attributeSchema
33
- });
34
- }
35
- Reflect.defineMetadata(ZODEC__SCHEMA, targetSchema, target.constructor);
36
- } catch (ex) {
37
- console.error("zatt/decorator/ex", ex);
38
- }
39
- };
40
- }
41
-
42
- // packages/zodec/core/src/decorators/zclass.decorator.ts
43
- import "reflect-metadata";
44
- function zclass(schema) {
45
- return (target) => {
46
- Reflect.defineMetadata(ZODEC__SCHEMA, schema, target);
47
- };
48
- }
49
- export {
50
- ZODEC__SCHEMA,
51
- ZODEC__TYPE,
52
- Zodec,
53
- zatt,
54
- zclass
55
- };
1
+ export * from './zodec.js';
2
+ export * from './decorators/zatt.decorator.js';
3
+ export * from './decorators/zclass.decorator.js';
4
+ export * from './constants.zodec.js';
package/dist/zodec.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import "reflect-metadata";
2
- import { ZodType } from "zod";
2
+ import { z, ZodType } from "zod";
3
3
  /**
4
4
  * Utility class for schemas and classes.
5
5
  */
@@ -11,5 +11,6 @@ export declare class Zodec {
11
11
  * @returns The Zod schema if found, otherwise undefined.
12
12
  */
13
13
  static of(target: any): ZodType<any> | undefined;
14
+ static validate(input: any, target?: any): z.ZodSafeParseResult<any>;
14
15
  }
15
16
  //# sourceMappingURL=zodec.d.ts.map
@@ -1 +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;;GAEG;AACH,qBAAa,KAAK;IACd;;;;;OAKG;IACH,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,GAAG,SAAS;CAGnD"}
1
+ {"version":3,"file":"zodec.d.ts","sourceRoot":"","sources":["../src/zodec.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAC1B,OAAO,EAAE,CAAC,EAAE,OAAO,EAAE,MAAM,KAAK,CAAC;AAKjC;;GAEG;AACH,qBAAa,KAAK;IACd;;;;;OAKG;IACH,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,GAAG,SAAS;IAIhD,MAAM,CAAC,QAAQ,CAAC,KAAK,EAAE,GAAG,EAAE,MAAM,GAAE,GAAU,GAAG,CAAC,CAAC,kBAAkB,CAAC,GAAG,CAAC;CA4B7E"}
package/dist/zodec.js ADDED
@@ -0,0 +1,41 @@
1
+ import "reflect-metadata";
2
+ import { z } from "zod";
3
+ import { ZODEC__SCHEMA } from "./constants.zodec.js";
4
+ /**
5
+ * Utility class for schemas and classes.
6
+ */
7
+ export class Zodec {
8
+ /**
9
+ * Retrieves the Zod schema associated with a class.
10
+ *
11
+ * @param target - The class or object to retrieve the schema from.
12
+ * @returns The Zod schema if found, otherwise undefined.
13
+ */
14
+ static of(target) {
15
+ return Reflect.getMetadata(ZODEC__SCHEMA, target);
16
+ }
17
+ static validate(input, target = null) {
18
+ if (!target) {
19
+ target = input?.constructor;
20
+ }
21
+ if (!target) {
22
+ return {
23
+ success: true, data: input
24
+ };
25
+ }
26
+ // console.log("zodec/validate", input, target, target?.name)
27
+ const schema = this.of(target);
28
+ if (schema) {
29
+ let validtn = schema.safeParse(input);
30
+ if (!validtn.success) {
31
+ validtn.data = z.treeifyError(validtn.error);
32
+ }
33
+ return validtn;
34
+ }
35
+ else {
36
+ return {
37
+ success: true, data: input
38
+ };
39
+ }
40
+ }
41
+ }
package/package.json CHANGED
@@ -1,88 +1,67 @@
1
- {
2
- "name": "@zodec/core",
3
- "version": "0.0.4",
4
- "description": "Generate Zod schemas using TypeScript decorators. Define validation inline with class definitions - combining Zod's power with decorator syntax.",
5
- "author": "Vizhy",
6
- "license": "MIT",
7
- "repository": {
8
- "type": "git",
9
- "url": "https://github.com/vizhy/js",
10
- "directory": "packages/zodec/core"
11
- },
12
- "homepage": "https://github.com/vizhy/js/tree/main/packages/zodec/core#readme",
13
- "bugs": {
14
- "url": "https://github.com/vizhy/js/issues"
15
- },
16
- "type": "module",
17
- "main": "./dist/index.cjs",
18
- "module": "./dist/index.js",
19
- "types": "./dist/index.d.ts",
20
- "exports": {
21
- "./package.json": "./package.json",
22
- ".": {
23
- "development": "./src/index.ts",
24
- "types": "./dist/index.d.ts",
25
- "import": "./dist/index.js",
26
- "require": "./dist/index.cjs",
27
- "default": "./dist/index.js"
28
- }
29
- },
30
- "files": [
31
- "dist",
32
- "!**/*.tsbuildinfo"
33
- ],
34
- "nx": {
35
- "name": "zodec-core",
36
- "targets": {
37
- "build": {
38
- "executor": "@nx/esbuild:esbuild",
39
- "outputs": [
40
- "{options.outputPath}"
41
- ],
42
- "options": {
43
- "outputPath": "packages/zodec/core/dist",
44
- "main": "packages/zodec/core/src/index.ts",
45
- "tsConfig": "packages/zodec/core/tsconfig.lib.json",
46
- "format": [
47
- "esm",
48
- "cjs"
49
- ],
50
- "declarationRootDir": "packages/zodec/core/src"
51
- }
52
- }
53
- }
54
- },
55
- "dependencies": {},
56
- "peerDependencies": {
57
- "zod": "^3.0.0 || ^4.0.0",
58
- "reflect-metadata": "^0.1.13 || ^0.2.0"
59
- },
60
- "publishConfig": {
61
- "access": "public"
62
- },
63
- "keywords": [
64
- "zod",
65
- "decorators",
66
- "schema",
67
- "validation",
68
- "typescript",
69
- "class-validator",
70
- "class-validator-alternative",
71
- "schema-validation",
72
- "runtime-validation",
73
- "type-safe",
74
- "zod-decorators",
75
- "decorator-validation",
76
- "model",
77
- "dto",
78
- "data-validation",
79
- "functional-schema",
80
- "declarative-validation",
81
- "declarative-style",
82
- "metadata",
83
- "reflection",
84
- "reflect-metadata",
85
- "typescript-decorators",
86
- "zodec"
87
- ]
88
- }
1
+ {
2
+ "name": "@zodec/core",
3
+ "version": "0.0.6",
4
+ "description": "Generate Zod schemas using TypeScript decorators. Define validation inline with class definitions - combining Zod's power with decorator syntax.",
5
+ "author": "Vizhy",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/vizhy/js",
10
+ "directory": "packages/zodec/core"
11
+ },
12
+ "homepage": "https://github.com/vizhy/js/tree/main/packages/zodec/core#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/vizhy/js/issues"
15
+ },
16
+ "type": "module",
17
+ "main": "./dist/index.js",
18
+ "module": "./dist/index.js",
19
+ "types": "./dist/index.d.ts",
20
+ "exports": {
21
+ "./package.json": "./package.json",
22
+ ".": {
23
+ "@vizhy/source": "./src/index.ts",
24
+ "types": "./dist/index.d.ts",
25
+ "import": "./dist/index.js",
26
+ "default": "./dist/index.js"
27
+ }
28
+ },
29
+ "files": [
30
+ "dist",
31
+ "!**/*.tsbuildinfo"
32
+ ],
33
+ "nx": {
34
+ "name": "zodec-core"
35
+ },
36
+ "dependencies": {
37
+ "tslib": "^2.3.0"
38
+ },
39
+ "publishConfig": {
40
+ "access": "public"
41
+ },
42
+ "keywords": [
43
+ "zod",
44
+ "decorators",
45
+ "schema",
46
+ "validation",
47
+ "typescript",
48
+ "class-validator",
49
+ "class-validator-alternative",
50
+ "schema-validation",
51
+ "runtime-validation",
52
+ "type-safe",
53
+ "zod-decorators",
54
+ "decorator-validation",
55
+ "model",
56
+ "dto",
57
+ "data-validation",
58
+ "functional-schema",
59
+ "declarative-validation",
60
+ "declarative-style",
61
+ "metadata",
62
+ "reflection",
63
+ "reflect-metadata",
64
+ "typescript-decorators",
65
+ "zodec"
66
+ ]
67
+ }
package/dist/index.cjs DELETED
@@ -1,96 +0,0 @@
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
- zatt: () => zatt,
37
- zclass: () => zclass
38
- });
39
- module.exports = __toCommonJS(src_exports);
40
-
41
- // packages/zodec/core/src/zodec.ts
42
- var import_reflect_metadata = require("reflect-metadata");
43
-
44
- // packages/zodec/core/src/constants.zodec.ts
45
- var ZODEC__SCHEMA = "zodec:schema";
46
- var ZODEC__TYPE = "zodec:type";
47
-
48
- // packages/zodec/core/src/zodec.ts
49
- var Zodec = class {
50
- /**
51
- * Retrieves the Zod schema associated with a class.
52
- *
53
- * @param target - The class or object to retrieve the schema from.
54
- * @returns The Zod schema if found, otherwise undefined.
55
- */
56
- static of(target) {
57
- return Reflect.getMetadata(ZODEC__SCHEMA, target);
58
- }
59
- };
60
-
61
- // packages/zodec/core/src/decorators/zatt.decorator.ts
62
- var z = __toESM(require("zod"), 1);
63
- function zatt(attributeSchema) {
64
- return (target, propertyKey) => {
65
- try {
66
- let targetSchema = Reflect.getMetadata(ZODEC__SCHEMA, target.constructor);
67
- if (!targetSchema) {
68
- targetSchema = z.object({});
69
- }
70
- if (attributeSchema) {
71
- targetSchema = targetSchema.extend({
72
- [propertyKey.toString()]: attributeSchema
73
- });
74
- }
75
- Reflect.defineMetadata(ZODEC__SCHEMA, targetSchema, target.constructor);
76
- } catch (ex) {
77
- console.error("zatt/decorator/ex", ex);
78
- }
79
- };
80
- }
81
-
82
- // packages/zodec/core/src/decorators/zclass.decorator.ts
83
- var import_reflect_metadata2 = require("reflect-metadata");
84
- function zclass(schema) {
85
- return (target) => {
86
- Reflect.defineMetadata(ZODEC__SCHEMA, schema, target);
87
- };
88
- }
89
- // Annotate the CommonJS export names for ESM import in node:
90
- 0 && (module.exports = {
91
- ZODEC__SCHEMA,
92
- ZODEC__TYPE,
93
- Zodec,
94
- zatt,
95
- zclass
96
- });