phecda-zod 1.0.4 → 2.0.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/dist/index.d.mts CHANGED
@@ -3,5 +3,6 @@ import { ZodTypeDef, ZodSchema, z } from 'zod';
3
3
  declare function zodToClass<TOutput = any, TDef extends ZodTypeDef = ZodTypeDef, TInput = TOutput>(zod: ZodSchema<TOutput, TDef, TInput>): (new (data?: Partial<z.infer<ZodSchema<TOutput, TDef, TInput>>>) => z.infer<ZodSchema<TOutput, TDef, TInput>>) & {
4
4
  schema: ZodSchema<TOutput, TDef, TInput>;
5
5
  };
6
+ declare function parse<Construct extends ReturnType<typeof zodToClass>>(C: Construct, data: ConstructorParameters<Construct>[0]): z.SafeParseReturnType<any, any>;
6
7
 
7
- export { zodToClass };
8
+ export { parse, zodToClass };
package/dist/index.d.ts CHANGED
@@ -3,5 +3,6 @@ import { ZodTypeDef, ZodSchema, z } from 'zod';
3
3
  declare function zodToClass<TOutput = any, TDef extends ZodTypeDef = ZodTypeDef, TInput = TOutput>(zod: ZodSchema<TOutput, TDef, TInput>): (new (data?: Partial<z.infer<ZodSchema<TOutput, TDef, TInput>>>) => z.infer<ZodSchema<TOutput, TDef, TInput>>) & {
4
4
  schema: ZodSchema<TOutput, TDef, TInput>;
5
5
  };
6
+ declare function parse<Construct extends ReturnType<typeof zodToClass>>(C: Construct, data: ConstructorParameters<Construct>[0]): z.SafeParseReturnType<any, any>;
6
7
 
7
- export { zodToClass };
8
+ export { parse, zodToClass };
package/dist/index.js CHANGED
@@ -21,23 +21,15 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  // src/index.ts
22
22
  var src_exports = {};
23
23
  __export(src_exports, {
24
+ parse: () => parse,
24
25
  zodToClass: () => zodToClass
25
26
  });
26
27
  module.exports = __toCommonJS(src_exports);
27
28
  var import_phecda_core = require("phecda-core");
28
- function ZodTo(cb) {
29
- return (proto, key) => {
30
- (0, import_phecda_core.setStateKey)(proto, key);
31
- (0, import_phecda_core.setHandler)(proto, key, {
32
- async pipe(instance, addError) {
33
- const ret = cb(instance, addError);
34
- if (ret) {
35
- for (const key2 in instance) {
36
- if (key2 !== "_value") delete instance[key2];
37
- }
38
- for (const key2 in ret) instance[key2] = ret[key2];
39
- }
40
- }
29
+ function ZodTo(zod) {
30
+ return (proto) => {
31
+ (0, import_phecda_core.setMeta)(proto, void 0, void 0, {
32
+ zod
41
33
  });
42
34
  };
43
35
  }
@@ -48,20 +40,22 @@ function zodToClass(zod) {
48
40
  __name(this, "Z");
49
41
  }
50
42
  constructor(data) {
51
- for (const key in data)
52
- this[key] = data[key];
43
+ Object.assign(this, data);
53
44
  }
54
45
  static schema = zod;
55
46
  };
56
- (0, import_phecda_core.addDecoToClass)(Z, void 0, ZodTo((ins, addError) => {
57
- const result = zod.safeParse(ins);
58
- if (!result.success) result.error.issues.forEach(({ message }) => addError(message));
59
- else return result.data;
60
- }));
47
+ (0, import_phecda_core.addDecoToClass)(Z, void 0, ZodTo(zod));
61
48
  return Z;
62
49
  }
63
50
  __name(zodToClass, "zodToClass");
51
+ function parse(C, data) {
52
+ const meta = (0, import_phecda_core.getMeta)(C);
53
+ const zod = meta.find((item) => !!item.zod).zod;
54
+ return zod.safeParse(data);
55
+ }
56
+ __name(parse, "parse");
64
57
  // Annotate the CommonJS export names for ESM import in node:
65
58
  0 && (module.exports = {
59
+ parse,
66
60
  zodToClass
67
61
  });
package/dist/index.mjs CHANGED
@@ -2,20 +2,11 @@ var __defProp = Object.defineProperty;
2
2
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
3
 
4
4
  // src/index.ts
5
- import { addDecoToClass, setHandler, setStateKey } from "phecda-core";
6
- function ZodTo(cb) {
7
- return (proto, key) => {
8
- setStateKey(proto, key);
9
- setHandler(proto, key, {
10
- async pipe(instance, addError) {
11
- const ret = cb(instance, addError);
12
- if (ret) {
13
- for (const key2 in instance) {
14
- if (key2 !== "_value") delete instance[key2];
15
- }
16
- for (const key2 in ret) instance[key2] = ret[key2];
17
- }
18
- }
5
+ import { addDecoToClass, getMeta, setMeta } from "phecda-core";
6
+ function ZodTo(zod) {
7
+ return (proto) => {
8
+ setMeta(proto, void 0, void 0, {
9
+ zod
19
10
  });
20
11
  };
21
12
  }
@@ -26,19 +17,21 @@ function zodToClass(zod) {
26
17
  __name(this, "Z");
27
18
  }
28
19
  constructor(data) {
29
- for (const key in data)
30
- this[key] = data[key];
20
+ Object.assign(this, data);
31
21
  }
32
22
  static schema = zod;
33
23
  };
34
- addDecoToClass(Z, void 0, ZodTo((ins, addError) => {
35
- const result = zod.safeParse(ins);
36
- if (!result.success) result.error.issues.forEach(({ message }) => addError(message));
37
- else return result.data;
38
- }));
24
+ addDecoToClass(Z, void 0, ZodTo(zod));
39
25
  return Z;
40
26
  }
41
27
  __name(zodToClass, "zodToClass");
28
+ function parse(C, data) {
29
+ const meta = getMeta(C);
30
+ const zod = meta.find((item) => !!item.zod).zod;
31
+ return zod.safeParse(data);
32
+ }
33
+ __name(parse, "parse");
42
34
  export {
35
+ parse,
43
36
  zodToClass
44
37
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "phecda-zod",
3
- "version": "1.0.4",
3
+ "version": "2.0.0",
4
4
  "description": "use zod instead of decorator/reflect-metadata",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -15,7 +15,7 @@
15
15
  "tsup": "^8.1.0"
16
16
  },
17
17
  "dependencies": {
18
- "phecda-core": "3.1.0",
18
+ "phecda-core": "4.0.0",
19
19
  "zod": "^3.23.5"
20
20
  },
21
21
  "scripts": {