@zodec/core 0.0.4 → 0.0.5
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/dist/constants.zodec.js +2 -0
- package/dist/decorators/zatt.decorator.js +32 -0
- package/dist/decorators/zclass.decorator.js +18 -0
- package/dist/index.d.ts +4 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -55
- package/dist/zodec.js +16 -0
- package/package.json +67 -88
- package/dist/index.cjs +0 -96
|
@@ -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 '
|
|
2
|
-
export * from '
|
|
3
|
-
export * from '
|
|
4
|
-
export * from '
|
|
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
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,
|
|
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
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
import { ZODEC__SCHEMA } from "./constants.zodec.js";
|
|
3
|
+
/**
|
|
4
|
+
* Utility class for schemas and classes.
|
|
5
|
+
*/
|
|
6
|
+
export class Zodec {
|
|
7
|
+
/**
|
|
8
|
+
* Retrieves the Zod schema associated with a class.
|
|
9
|
+
*
|
|
10
|
+
* @param target - The class or object to retrieve the schema from.
|
|
11
|
+
* @returns The Zod schema if found, otherwise undefined.
|
|
12
|
+
*/
|
|
13
|
+
static of(target) {
|
|
14
|
+
return Reflect.getMetadata(ZODEC__SCHEMA, target);
|
|
15
|
+
}
|
|
16
|
+
}
|
package/package.json
CHANGED
|
@@ -1,88 +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": "module",
|
|
17
|
-
"main": "./dist/index.
|
|
18
|
-
"module": "./dist/index.js",
|
|
19
|
-
"types": "./dist/index.d.ts",
|
|
20
|
-
"exports": {
|
|
21
|
-
"./package.json": "./package.json",
|
|
22
|
-
".": {
|
|
23
|
-
"
|
|
24
|
-
"types": "./dist/index.d.ts",
|
|
25
|
-
"import": "./dist/index.js",
|
|
26
|
-
"
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
"
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
"
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
"
|
|
65
|
-
"
|
|
66
|
-
|
|
67
|
-
|
|
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.5",
|
|
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
|
-
});
|