@zodec/core 0.0.5 → 0.0.7
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 +1 -1
- package/dist/constants.zodec.js +5 -2
- package/dist/decorators/zatt.decorator.js +9 -5
- package/dist/decorators/zclass.decorator.js +7 -4
- package/dist/index.js +7 -4
- package/dist/zodec.d.ts +8 -1
- package/dist/zodec.d.ts.map +1 -1
- package/dist/zodec.js +41 -4
- package/package.json +67 -67
package/README.md
CHANGED
package/dist/constants.zodec.js
CHANGED
|
@@ -1,2 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ZODEC__TYPE = exports.ZODEC__SCHEMA = void 0;
|
|
4
|
+
exports.ZODEC__SCHEMA = "zodec:schema";
|
|
5
|
+
exports.ZODEC__TYPE = "zodec:type";
|
|
@@ -1,5 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.zatt = zatt;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const constants_zodec_js_1 = require("../constants.zodec.js");
|
|
6
|
+
const z = tslib_1.__importStar(require("zod"));
|
|
3
7
|
/**
|
|
4
8
|
* zatt: zod attribute
|
|
5
9
|
* decorate a property with a zod schema
|
|
@@ -8,11 +12,11 @@ import * as z from "zod";
|
|
|
8
12
|
* @param attributeSchema
|
|
9
13
|
* @returns
|
|
10
14
|
*/
|
|
11
|
-
|
|
15
|
+
function zatt(attributeSchema) {
|
|
12
16
|
return (target, propertyKey) => {
|
|
13
17
|
try {
|
|
14
18
|
// check if the target has existing ZODEC__SCHEMA
|
|
15
|
-
let targetSchema = Reflect.getMetadata(ZODEC__SCHEMA, target.constructor);
|
|
19
|
+
let targetSchema = Reflect.getMetadata(constants_zodec_js_1.ZODEC__SCHEMA, target.constructor);
|
|
16
20
|
if (!targetSchema) {
|
|
17
21
|
// create a new ZodObject schema if none exists
|
|
18
22
|
targetSchema = z.object({});
|
|
@@ -23,7 +27,7 @@ export function zatt(attributeSchema) {
|
|
|
23
27
|
[propertyKey.toString()]: attributeSchema,
|
|
24
28
|
});
|
|
25
29
|
}
|
|
26
|
-
Reflect.defineMetadata(ZODEC__SCHEMA, targetSchema, target.constructor);
|
|
30
|
+
Reflect.defineMetadata(constants_zodec_js_1.ZODEC__SCHEMA, targetSchema, target.constructor);
|
|
27
31
|
}
|
|
28
32
|
catch (ex) {
|
|
29
33
|
console.error("zatt/decorator/ex", ex);
|
|
@@ -1,14 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.zclass = zclass;
|
|
4
|
+
require("reflect-metadata");
|
|
5
|
+
const constants_zodec_js_1 = require("../constants.zodec.js");
|
|
3
6
|
/**
|
|
4
7
|
* decorate a class by providing defined zod schema object
|
|
5
8
|
* @param schema
|
|
6
9
|
* @returns
|
|
7
10
|
*/
|
|
8
|
-
|
|
11
|
+
function zclass(schema) {
|
|
9
12
|
return (target) => {
|
|
10
13
|
// schema
|
|
11
|
-
Reflect.defineMetadata(ZODEC__SCHEMA, schema, target);
|
|
14
|
+
Reflect.defineMetadata(constants_zodec_js_1.ZODEC__SCHEMA, schema, target);
|
|
12
15
|
// inferred type > is not a runtime entity
|
|
13
16
|
// type inferred = z.infer<typeof schema>;
|
|
14
17
|
// Reflect.defineMetadata(ZODIFY__TYPE, inferred, target)
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
tslib_1.__exportStar(require("./zodec.js"), exports);
|
|
5
|
+
tslib_1.__exportStar(require("./decorators/zatt.decorator.js"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./decorators/zclass.decorator.js"), exports);
|
|
7
|
+
tslib_1.__exportStar(require("./constants.zodec.js"), exports);
|
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,12 @@ 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
|
+
/**
|
|
15
|
+
* checks if a target class has schema defined or not
|
|
16
|
+
* @param target
|
|
17
|
+
* @returns
|
|
18
|
+
*/
|
|
19
|
+
static hasSchema(target: any): boolean;
|
|
20
|
+
static validate(input: any, target?: any): z.ZodSafeParseResult<any>;
|
|
14
21
|
}
|
|
15
22
|
//# sourceMappingURL=zodec.d.ts.map
|
package/dist/zodec.d.ts.map
CHANGED
|
@@ -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;
|
|
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;;;;OAIG;IACH,MAAM,CAAC,SAAS,CAAC,MAAM,EAAE,GAAG,GAAG,OAAO;IAItC,MAAM,CAAC,QAAQ,CAAC,KAAK,EAAE,GAAG,EAAE,MAAM,GAAE,GAAU,GAAG,CAAC,CAAC,kBAAkB,CAAC,GAAG,CAAC;CA4B7E"}
|
package/dist/zodec.js
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Zodec = void 0;
|
|
4
|
+
require("reflect-metadata");
|
|
5
|
+
const zod_1 = require("zod");
|
|
6
|
+
const constants_zodec_js_1 = require("./constants.zodec.js");
|
|
3
7
|
/**
|
|
4
8
|
* Utility class for schemas and classes.
|
|
5
9
|
*/
|
|
6
|
-
|
|
10
|
+
class Zodec {
|
|
7
11
|
/**
|
|
8
12
|
* Retrieves the Zod schema associated with a class.
|
|
9
13
|
*
|
|
@@ -11,6 +15,39 @@ export class Zodec {
|
|
|
11
15
|
* @returns The Zod schema if found, otherwise undefined.
|
|
12
16
|
*/
|
|
13
17
|
static of(target) {
|
|
14
|
-
return Reflect.getMetadata(ZODEC__SCHEMA, target);
|
|
18
|
+
return Reflect.getMetadata(constants_zodec_js_1.ZODEC__SCHEMA, target);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* checks if a target class has schema defined or not
|
|
22
|
+
* @param target
|
|
23
|
+
* @returns
|
|
24
|
+
*/
|
|
25
|
+
static hasSchema(target) {
|
|
26
|
+
return Reflect.hasMetadata(constants_zodec_js_1.ZODEC__SCHEMA, target);
|
|
27
|
+
}
|
|
28
|
+
static validate(input, target = null) {
|
|
29
|
+
if (!target) {
|
|
30
|
+
target = input?.constructor;
|
|
31
|
+
}
|
|
32
|
+
if (!target) {
|
|
33
|
+
return {
|
|
34
|
+
success: true, data: input
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
// console.log("zodec/validate", input, target, target?.name)
|
|
38
|
+
const schema = this.of(target);
|
|
39
|
+
if (schema) {
|
|
40
|
+
let validtn = schema.safeParse(input);
|
|
41
|
+
if (!validtn.success) {
|
|
42
|
+
validtn.data = zod_1.z.treeifyError(validtn.error);
|
|
43
|
+
}
|
|
44
|
+
return validtn;
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
return {
|
|
48
|
+
success: true, data: input
|
|
49
|
+
};
|
|
50
|
+
}
|
|
15
51
|
}
|
|
16
52
|
}
|
|
53
|
+
exports.Zodec = Zodec;
|
package/package.json
CHANGED
|
@@ -1,67 +1,67 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@zodec/core",
|
|
3
|
-
"version": "0.0.
|
|
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": "
|
|
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
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@zodec/core",
|
|
3
|
+
"version": "0.0.7",
|
|
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": "commonjs",
|
|
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
|
+
}
|