@hg-ts/validation 0.1.40 → 0.1.42

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/.eslintcache CHANGED
@@ -1 +1 @@
1
- [{"/Volumes/Work/ts/hg/framework/packages/async-context/src/async-context.not-found.exception.ts":"1","/Volumes/Work/ts/hg/framework/packages/async-context/src/async-context.provider.ts":"2","/Volumes/Work/ts/hg/framework/packages/async-context/src/async-context.test-suite.ts":"3","/Volumes/Work/ts/hg/framework/packages/async-context/src/index.ts":"4","/Volumes/Work/ts/hg/framework/packages/validation/src/extensions.ts":"5","/Volumes/Work/ts/hg/framework/packages/validation/src/index.ts":"6","/Volumes/Work/ts/hg/framework/packages/validation/src/dto.ts":"7"},{"size":186,"mtime":1698022585968,"results":"8","hashOfConfig":"9"},{"size":2161,"mtime":1698022585968,"results":"10","hashOfConfig":"9"},{"size":3714,"mtime":1698022585968,"results":"11","hashOfConfig":"9"},{"size":95,"mtime":1698022585968,"results":"12","hashOfConfig":"9"},{"size":1335,"mtime":1713905441410,"results":"13","hashOfConfig":"14"},{"size":88,"mtime":1713662918071,"results":"15","hashOfConfig":"14"},{"size":595,"mtime":1713903104633,"results":"16","hashOfConfig":"14"},{"filePath":"17","messages":"18","suppressedMessages":"19","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"1o9szx6",{"filePath":"20","messages":"21","suppressedMessages":"22","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"23","messages":"24","suppressedMessages":"25","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"26","messages":"27","suppressedMessages":"28","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"29","messages":"30","suppressedMessages":"31","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"szyv4m",{"filePath":"32","messages":"33","suppressedMessages":"34","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"35","messages":"36","suppressedMessages":"37","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"/Volumes/Work/ts/hg/framework/packages/async-context/src/async-context.not-found.exception.ts",[],[],"/Volumes/Work/ts/hg/framework/packages/async-context/src/async-context.provider.ts",[],[],"/Volumes/Work/ts/hg/framework/packages/async-context/src/async-context.test-suite.ts",[],[],"/Volumes/Work/ts/hg/framework/packages/async-context/src/index.ts",[],[],"/Volumes/Work/ts/hg/framework/packages/validation/src/extensions.ts",[],["38"],"/Volumes/Work/ts/hg/framework/packages/validation/src/index.ts",[],[],"/Volumes/Work/ts/hg/framework/packages/validation/src/dto.ts",[],[],{"ruleId":"39","severity":2,"message":"40","line":17,"column":12,"nodeType":"41","messageId":"42","endLine":17,"endColumn":21,"fix":"43","suppressions":"44"},"@typescript-eslint/consistent-type-definitions","Use a `type` instead of an `interface`.","Identifier","typeOverInterface",{"range":"45","text":"46"},["47"],[283,370],"type ZodSchema<Output = any, Def extends ZodTypeDef = ZodTypeDef, Input = Output> = ",{"kind":"48","justification":"49"},"directive",""]
1
+ [{"/Volumes/Work/ts/hg/framework/packages/async-context/src/async-context.not-found.exception.ts":"1","/Volumes/Work/ts/hg/framework/packages/async-context/src/async-context.provider.ts":"2","/Volumes/Work/ts/hg/framework/packages/async-context/src/async-context.test-suite.ts":"3","/Volumes/Work/ts/hg/framework/packages/async-context/src/index.ts":"4","/Volumes/Work/ts/hg/framework/packages/validation/src/extensions.ts":"5","/Volumes/Work/ts/hg/framework/packages/validation/src/index.ts":"6","/Volumes/Work/ts/hg/framework/packages/validation/src/dto.ts":"7","/Volumes/Work/ts/hg/framework/packages/validation/src/validation.exception.ts":"8"},{"size":186,"mtime":1698022585968,"results":"9","hashOfConfig":"10"},{"size":2161,"mtime":1698022585968,"results":"11","hashOfConfig":"10"},{"size":3714,"mtime":1698022585968,"results":"12","hashOfConfig":"10"},{"size":95,"mtime":1698022585968,"results":"13","hashOfConfig":"10"},{"size":1335,"mtime":1713905441410,"results":"14","hashOfConfig":"15"},{"size":170,"mtime":1720368776629,"results":"16","hashOfConfig":"15"},{"size":1415,"mtime":1720368838726,"results":"17","hashOfConfig":"15"},{"size":508,"mtime":1720369302113,"results":"18","hashOfConfig":"15"},{"filePath":"19","messages":"20","suppressedMessages":"21","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"1o9szx6",{"filePath":"22","messages":"23","suppressedMessages":"24","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"25","messages":"26","suppressedMessages":"27","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"28","messages":"29","suppressedMessages":"30","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"31","messages":"32","suppressedMessages":"33","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"szyv4m",{"filePath":"34","messages":"35","suppressedMessages":"36","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"37","messages":"38","suppressedMessages":"39","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"40","messages":"41","suppressedMessages":"42","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"/Volumes/Work/ts/hg/framework/packages/async-context/src/async-context.not-found.exception.ts",[],[],"/Volumes/Work/ts/hg/framework/packages/async-context/src/async-context.provider.ts",[],[],"/Volumes/Work/ts/hg/framework/packages/async-context/src/async-context.test-suite.ts",[],[],"/Volumes/Work/ts/hg/framework/packages/async-context/src/index.ts",[],[],"/Volumes/Work/ts/hg/framework/packages/validation/src/extensions.ts",[],["43"],"/Volumes/Work/ts/hg/framework/packages/validation/src/index.ts",[],[],"/Volumes/Work/ts/hg/framework/packages/validation/src/dto.ts",[],[],"/Volumes/Work/ts/hg/framework/packages/validation/src/validation.exception.ts",[],[],{"ruleId":"44","severity":2,"message":"45","line":17,"column":12,"nodeType":"46","messageId":"47","endLine":17,"endColumn":21,"fix":"48","suppressions":"49"},"@typescript-eslint/consistent-type-definitions","Use a `type` instead of an `interface`.","Identifier","typeOverInterface",{"range":"50","text":"51"},["52"],[283,370],"type ZodSchema<Output = any, Def extends ZodTypeDef = ZodTypeDef, Input = Output> = ",{"kind":"53","justification":"54"},"directive",""]
package/dist/dto.d.ts CHANGED
@@ -1,9 +1,12 @@
1
+ /// <reference types="@hg-ts/types" />
1
2
  import { ZodSchema, ZodTypeDef } from 'zod';
2
3
  export type ZodDto<TOutput = any> = {
3
4
  new (): TOutput;
4
5
  isZodDto: true;
5
6
  schema: ZodSchema<TOutput, ZodTypeDef, unknown>;
6
7
  create(input: unknown): TOutput;
8
+ createAsync(input: unknown): Promise<TOutput>;
7
9
  };
8
10
  export declare function createZodDto<TOutput = any>(schema: ZodSchema<TOutput, ZodTypeDef, unknown>): ZodDto<TOutput>;
11
+ export declare function isZodDto(ctor: Class<any, any[]>): ctor is ZodDto;
9
12
  //# sourceMappingURL=dto.d.ts.map
package/dist/dto.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"dto.d.ts","sourceRoot":"","sources":["../src/dto.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,SAAS,EACT,UAAU,EACV,MAAM,KAAK,CAAC;AAEb,MAAM,MAAM,MAAM,CAAC,OAAO,GAAG,GAAG,IAAI;IACnC,QAAQ,OAAO,CAAC;IAChB,QAAQ,EAAE,IAAI,CAAC;IACf,MAAM,EAAE,SAAS,CAAC,OAAO,EAAE,UAAU,EAAE,OAAO,CAAC,CAAC;IAChD,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC;CAChC,CAAA;AAED,wBAAgB,YAAY,CAAC,OAAO,GAAG,GAAG,EACzC,MAAM,EAAE,SAAS,CAAC,OAAO,EAAE,UAAU,EAAE,OAAO,CAAC,GAC7C,MAAM,CAAC,OAAO,CAAC,CAWjB"}
1
+ {"version":3,"file":"dto.d.ts","sourceRoot":"","sources":["../src/dto.ts"],"names":[],"mappings":";AAAA,OAAO,EAEN,SAAS,EACT,UAAU,EACV,MAAM,KAAK,CAAC;AAGb,MAAM,MAAM,MAAM,CAAC,OAAO,GAAG,GAAG,IAAI;IACnC,QAAQ,OAAO,CAAC;IAChB,QAAQ,EAAE,IAAI,CAAC;IACf,MAAM,EAAE,SAAS,CAAC,OAAO,EAAE,UAAU,EAAE,OAAO,CAAC,CAAC;IAChD,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC;IAChC,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CAC9C,CAAA;AAED,wBAAgB,YAAY,CAAC,OAAO,GAAG,GAAG,EACzC,MAAM,EAAE,SAAS,CAAC,OAAO,EAAE,UAAU,EAAE,OAAO,CAAC,GAC7C,MAAM,CAAC,OAAO,CAAC,CAqCjB;AAED,wBAAgB,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,GAAG,IAAI,IAAI,MAAM,CAEhE"}
package/dist/dto.js CHANGED
@@ -1,15 +1,44 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createZodDto = void 0;
3
+ exports.isZodDto = exports.createZodDto = void 0;
4
+ const zod_1 = require("zod");
5
+ const validation_exception_1 = require("./validation.exception");
4
6
  function createZodDto(schema) {
5
7
  class AugmentedZodDto {
6
8
  static isZodDto = true;
7
9
  static schema = schema;
8
10
  static create(input) {
9
- return this.schema.parse(input);
11
+ try {
12
+ return this.schema.parse(input);
13
+ }
14
+ catch (error) {
15
+ if (error instanceof zod_1.ZodError) {
16
+ AugmentedZodDto.formatError(error);
17
+ }
18
+ throw error;
19
+ }
20
+ }
21
+ static async createAsync(input) {
22
+ try {
23
+ const result = await this.schema.parseAsync(input);
24
+ return result;
25
+ }
26
+ catch (error) {
27
+ if (error instanceof zod_1.ZodError) {
28
+ AugmentedZodDto.formatError(error);
29
+ }
30
+ throw error;
31
+ }
32
+ }
33
+ static formatError(error) {
34
+ throw new validation_exception_1.ValidationException(error);
10
35
  }
11
36
  }
12
37
  return AugmentedZodDto;
13
38
  }
14
39
  exports.createZodDto = createZodDto;
40
+ function isZodDto(ctor) {
41
+ return 'isZodDto' in ctor && ctor.isZodDto === true;
42
+ }
43
+ exports.isZodDto = isZodDto;
15
44
  //# sourceMappingURL=dto.js.map
package/dist/dto.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"dto.js","sourceRoot":"","sources":["../src/dto.ts"],"names":[],"mappings":";;;AAYA,SAAgB,YAAY,CAC3B,MAA+C;IAE/C,MAAM,eAAe;QACb,MAAM,CAAC,QAAQ,GAAG,IAAI,CAAC;QACvB,MAAM,CAAC,MAAM,GAAG,MAAM,CAAC;QAEvB,MAAM,CAAC,MAAM,CAAC,KAAc;YAClC,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAoB,CAAC;QACpD,CAAC;;IAGF,OAAO,eAA6C,CAAC;AACtD,CAAC;AAbD,oCAaC"}
1
+ {"version":3,"file":"dto.js","sourceRoot":"","sources":["../src/dto.ts"],"names":[],"mappings":";;;AAAA,6BAIa;AACb,iEAA6D;AAU7D,SAAgB,YAAY,CAC3B,MAA+C;IAE/C,MAAM,eAAe;QACb,MAAM,CAAC,QAAQ,GAAG,IAAI,CAAC;QACvB,MAAM,CAAC,MAAM,GAAG,MAAM,CAAC;QAEvB,MAAM,CAAC,MAAM,CAAC,KAAc;YAClC,IAAI;gBACH,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAoB,CAAC;aACnD;YAAC,OAAO,KAAc,EAAE;gBACxB,IAAI,KAAK,YAAY,cAAQ,EAAE;oBAC9B,eAAe,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;iBACnC;gBAED,MAAM,KAAK,CAAC;aACZ;QACF,CAAC;QAEM,MAAM,CAAC,KAAK,CAAC,WAAW,CAAC,KAAc;YAC7C,IAAI;gBACH,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;gBAEnD,OAAO,MAAyB,CAAC;aACjC;YAAC,OAAO,KAAc,EAAE;gBACxB,IAAI,KAAK,YAAY,cAAQ,EAAE;oBAC9B,eAAe,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;iBACnC;gBAED,MAAM,KAAK,CAAC;aACZ;QACF,CAAC;QAES,MAAM,CAAC,WAAW,CAAC,KAAe;YAC3C,MAAM,IAAI,0CAAmB,CAAC,KAAK,CAAC,CAAC;QACtC,CAAC;;IAGF,OAAO,eAA6C,CAAC;AACtD,CAAC;AAvCD,oCAuCC;AAED,SAAgB,QAAQ,CAAC,IAAuB;IAC/C,OAAO,UAAU,IAAI,IAAI,IAAI,IAAI,CAAC,QAAQ,KAAK,IAAI,CAAC;AACrD,CAAC;AAFD,4BAEC"}
package/dist/index.d.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  import './extensions';
2
- export * from 'zod';
3
2
  import zod from 'zod';
3
+ export { ZodDto, isZodDto } from './dto';
4
+ export * from './validation.exception';
5
+ export * from 'zod';
4
6
  export default zod;
5
7
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,cAAc,CAAC;AACtB,cAAc,KAAK,CAAC;AACpB,OAAO,GAAG,MAAM,KAAK,CAAC;AAEtB,eAAe,GAAG,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,cAAc,CAAC;AACtB,OAAO,GAAG,MAAM,KAAK,CAAC;AAEtB,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AACzC,cAAc,wBAAwB,CAAC;AACvC,cAAc,KAAK,CAAC;AACpB,eAAe,GAAG,CAAC"}
package/dist/index.js CHANGED
@@ -1,8 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isZodDto = void 0;
3
4
  const tslib_1 = require("tslib");
4
5
  require("./extensions");
5
- tslib_1.__exportStar(require("zod"), exports);
6
6
  const zod_1 = tslib_1.__importDefault(require("zod"));
7
+ var dto_1 = require("./dto");
8
+ Object.defineProperty(exports, "isZodDto", { enumerable: true, get: function () { return dto_1.isZodDto; } });
9
+ tslib_1.__exportStar(require("./validation.exception"), exports);
10
+ tslib_1.__exportStar(require("zod"), exports);
7
11
  exports.default = zod_1.default;
8
12
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,wBAAsB;AACtB,8CAAoB;AACpB,sDAAsB;AAEtB,kBAAe,aAAG,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;AAAA,wBAAsB;AACtB,sDAAsB;AAEtB,6BAAyC;AAAxB,+FAAA,QAAQ,OAAA;AACzB,iEAAuC;AACvC,8CAAoB;AACpB,kBAAe,aAAG,CAAC"}
@@ -0,0 +1,11 @@
1
+ import { BaseException, JSONBaseException } from '@hg-ts/exception';
2
+ import { ZodError, ZodIssue } from 'zod';
3
+ export type JSONValidationException = JSONBaseException & {
4
+ errors: ZodIssue[];
5
+ };
6
+ export declare class ValidationException extends BaseException {
7
+ errors: ZodIssue[];
8
+ constructor(error: ZodError);
9
+ toJSON(): JSONValidationException;
10
+ }
11
+ //# sourceMappingURL=validation.exception.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validation.exception.d.ts","sourceRoot":"","sources":["../src/validation.exception.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,aAAa,EACb,iBAAiB,EACjB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,KAAK,CAAC;AAEzC,MAAM,MAAM,uBAAuB,GAAG,iBAAiB,GAAG;IACzD,MAAM,EAAE,QAAQ,EAAE,CAAC;CACnB,CAAA;AAED,qBAAa,mBAAoB,SAAQ,aAAa;IAC9C,MAAM,EAAE,QAAQ,EAAE,CAAC;gBACP,KAAK,EAAE,QAAQ;IAMlB,MAAM,IAAI,uBAAuB;CAMjD"}
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ValidationException = void 0;
4
+ const exception_1 = require("@hg-ts/exception");
5
+ class ValidationException extends exception_1.BaseException {
6
+ errors;
7
+ constructor(error) {
8
+ super('Validation failed');
9
+ this.errors = error.issues;
10
+ }
11
+ toJSON() {
12
+ return {
13
+ ...super.toJSON(),
14
+ errors: this.errors,
15
+ };
16
+ }
17
+ }
18
+ exports.ValidationException = ValidationException;
19
+ //# sourceMappingURL=validation.exception.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validation.exception.js","sourceRoot":"","sources":["../src/validation.exception.ts"],"names":[],"mappings":";;;AAAA,gDAG0B;AAO1B,MAAa,mBAAoB,SAAQ,yBAAa;IAC9C,MAAM,CAAa;IAC1B,YAAmB,KAAe;QACjC,KAAK,CAAC,mBAAmB,CAAC,CAAC;QAE3B,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC;IAC5B,CAAC;IAEe,MAAM;QACrB,OAAO;YACN,GAAG,KAAK,CAAC,MAAM,EAAE;YACjB,MAAM,EAAE,IAAI,CAAC,MAAM;SACnB,CAAC;IACH,CAAC;CACD;AAdD,kDAcC"}
package/dist-esm/dto.d.ts CHANGED
@@ -1,9 +1,12 @@
1
+ /// <reference types="@hg-ts/types" />
1
2
  import { ZodSchema, ZodTypeDef } from 'zod';
2
3
  export type ZodDto<TOutput = any> = {
3
4
  new (): TOutput;
4
5
  isZodDto: true;
5
6
  schema: ZodSchema<TOutput, ZodTypeDef, unknown>;
6
7
  create(input: unknown): TOutput;
8
+ createAsync(input: unknown): Promise<TOutput>;
7
9
  };
8
10
  export declare function createZodDto<TOutput = any>(schema: ZodSchema<TOutput, ZodTypeDef, unknown>): ZodDto<TOutput>;
11
+ export declare function isZodDto(ctor: Class<any, any[]>): ctor is ZodDto;
9
12
  //# sourceMappingURL=dto.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"dto.d.ts","sourceRoot":"","sources":["../src/dto.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,SAAS,EACT,UAAU,EACV,MAAM,KAAK,CAAC;AAEb,MAAM,MAAM,MAAM,CAAC,OAAO,GAAG,GAAG,IAAI;IACnC,QAAQ,OAAO,CAAC;IAChB,QAAQ,EAAE,IAAI,CAAC;IACf,MAAM,EAAE,SAAS,CAAC,OAAO,EAAE,UAAU,EAAE,OAAO,CAAC,CAAC;IAChD,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC;CAChC,CAAA;AAED,wBAAgB,YAAY,CAAC,OAAO,GAAG,GAAG,EACzC,MAAM,EAAE,SAAS,CAAC,OAAO,EAAE,UAAU,EAAE,OAAO,CAAC,GAC7C,MAAM,CAAC,OAAO,CAAC,CAWjB"}
1
+ {"version":3,"file":"dto.d.ts","sourceRoot":"","sources":["../src/dto.ts"],"names":[],"mappings":";AAAA,OAAO,EAEN,SAAS,EACT,UAAU,EACV,MAAM,KAAK,CAAC;AAGb,MAAM,MAAM,MAAM,CAAC,OAAO,GAAG,GAAG,IAAI;IACnC,QAAQ,OAAO,CAAC;IAChB,QAAQ,EAAE,IAAI,CAAC;IACf,MAAM,EAAE,SAAS,CAAC,OAAO,EAAE,UAAU,EAAE,OAAO,CAAC,CAAC;IAChD,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC;IAChC,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CAC9C,CAAA;AAED,wBAAgB,YAAY,CAAC,OAAO,GAAG,GAAG,EACzC,MAAM,EAAE,SAAS,CAAC,OAAO,EAAE,UAAU,EAAE,OAAO,CAAC,GAC7C,MAAM,CAAC,OAAO,CAAC,CAqCjB;AAED,wBAAgB,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,GAAG,IAAI,IAAI,MAAM,CAEhE"}
package/dist-esm/dto.js CHANGED
@@ -1,11 +1,39 @@
1
+ import { ZodError, } from 'zod';
2
+ import { ValidationException } from './validation.exception';
1
3
  export function createZodDto(schema) {
2
4
  class AugmentedZodDto {
3
5
  static isZodDto = true;
4
6
  static schema = schema;
5
7
  static create(input) {
6
- return this.schema.parse(input);
8
+ try {
9
+ return this.schema.parse(input);
10
+ }
11
+ catch (error) {
12
+ if (error instanceof ZodError) {
13
+ AugmentedZodDto.formatError(error);
14
+ }
15
+ throw error;
16
+ }
17
+ }
18
+ static async createAsync(input) {
19
+ try {
20
+ const result = await this.schema.parseAsync(input);
21
+ return result;
22
+ }
23
+ catch (error) {
24
+ if (error instanceof ZodError) {
25
+ AugmentedZodDto.formatError(error);
26
+ }
27
+ throw error;
28
+ }
29
+ }
30
+ static formatError(error) {
31
+ throw new ValidationException(error);
7
32
  }
8
33
  }
9
34
  return AugmentedZodDto;
10
35
  }
36
+ export function isZodDto(ctor) {
37
+ return 'isZodDto' in ctor && ctor.isZodDto === true;
38
+ }
11
39
  //# sourceMappingURL=dto.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"dto.js","sourceRoot":"","sources":["../src/dto.ts"],"names":[],"mappings":"AAYA,MAAM,UAAU,YAAY,CAC3B,MAA+C;IAE/C,MAAM,eAAe;QACb,MAAM,CAAC,QAAQ,GAAG,IAAI,CAAC;QACvB,MAAM,CAAC,MAAM,GAAG,MAAM,CAAC;QAEvB,MAAM,CAAC,MAAM,CAAC,KAAc;YAClC,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAoB,CAAC;QACpD,CAAC;;IAGF,OAAO,eAA6C,CAAC;AACtD,CAAC"}
1
+ {"version":3,"file":"dto.js","sourceRoot":"","sources":["../src/dto.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,QAAQ,GAGR,MAAM,KAAK,CAAC;AACb,OAAO,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;AAU7D,MAAM,UAAU,YAAY,CAC3B,MAA+C;IAE/C,MAAM,eAAe;QACb,MAAM,CAAC,QAAQ,GAAG,IAAI,CAAC;QACvB,MAAM,CAAC,MAAM,GAAG,MAAM,CAAC;QAEvB,MAAM,CAAC,MAAM,CAAC,KAAc;YAClC,IAAI;gBACH,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAoB,CAAC;aACnD;YAAC,OAAO,KAAc,EAAE;gBACxB,IAAI,KAAK,YAAY,QAAQ,EAAE;oBAC9B,eAAe,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;iBACnC;gBAED,MAAM,KAAK,CAAC;aACZ;QACF,CAAC;QAEM,MAAM,CAAC,KAAK,CAAC,WAAW,CAAC,KAAc;YAC7C,IAAI;gBACH,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;gBAEnD,OAAO,MAAyB,CAAC;aACjC;YAAC,OAAO,KAAc,EAAE;gBACxB,IAAI,KAAK,YAAY,QAAQ,EAAE;oBAC9B,eAAe,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;iBACnC;gBAED,MAAM,KAAK,CAAC;aACZ;QACF,CAAC;QAES,MAAM,CAAC,WAAW,CAAC,KAAe;YAC3C,MAAM,IAAI,mBAAmB,CAAC,KAAK,CAAC,CAAC;QACtC,CAAC;;IAGF,OAAO,eAA6C,CAAC;AACtD,CAAC;AAED,MAAM,UAAU,QAAQ,CAAC,IAAuB;IAC/C,OAAO,UAAU,IAAI,IAAI,IAAI,IAAI,CAAC,QAAQ,KAAK,IAAI,CAAC;AACrD,CAAC"}
@@ -1,5 +1,7 @@
1
1
  import './extensions';
2
- export * from 'zod';
3
2
  import zod from 'zod';
3
+ export { ZodDto, isZodDto } from './dto';
4
+ export * from './validation.exception';
5
+ export * from 'zod';
4
6
  export default zod;
5
7
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,cAAc,CAAC;AACtB,cAAc,KAAK,CAAC;AACpB,OAAO,GAAG,MAAM,KAAK,CAAC;AAEtB,eAAe,GAAG,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,cAAc,CAAC;AACtB,OAAO,GAAG,MAAM,KAAK,CAAC;AAEtB,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AACzC,cAAc,wBAAwB,CAAC;AACvC,cAAc,KAAK,CAAC;AACpB,eAAe,GAAG,CAAC"}
package/dist-esm/index.js CHANGED
@@ -1,5 +1,7 @@
1
1
  import './extensions';
2
- export * from 'zod';
3
2
  import zod from 'zod';
3
+ export { isZodDto } from './dto';
4
+ export * from './validation.exception';
5
+ export * from 'zod';
4
6
  export default zod;
5
7
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,cAAc,CAAC;AACtB,cAAc,KAAK,CAAC;AACpB,OAAO,GAAG,MAAM,KAAK,CAAC;AAEtB,eAAe,GAAG,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,cAAc,CAAC;AACtB,OAAO,GAAG,MAAM,KAAK,CAAC;AAEtB,OAAO,EAAU,QAAQ,EAAE,MAAM,OAAO,CAAC;AACzC,cAAc,wBAAwB,CAAC;AACvC,cAAc,KAAK,CAAC;AACpB,eAAe,GAAG,CAAC"}
@@ -0,0 +1,11 @@
1
+ import { BaseException, JSONBaseException } from '@hg-ts/exception';
2
+ import { ZodError, ZodIssue } from 'zod';
3
+ export type JSONValidationException = JSONBaseException & {
4
+ errors: ZodIssue[];
5
+ };
6
+ export declare class ValidationException extends BaseException {
7
+ errors: ZodIssue[];
8
+ constructor(error: ZodError);
9
+ toJSON(): JSONValidationException;
10
+ }
11
+ //# sourceMappingURL=validation.exception.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validation.exception.d.ts","sourceRoot":"","sources":["../src/validation.exception.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,aAAa,EACb,iBAAiB,EACjB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,KAAK,CAAC;AAEzC,MAAM,MAAM,uBAAuB,GAAG,iBAAiB,GAAG;IACzD,MAAM,EAAE,QAAQ,EAAE,CAAC;CACnB,CAAA;AAED,qBAAa,mBAAoB,SAAQ,aAAa;IAC9C,MAAM,EAAE,QAAQ,EAAE,CAAC;gBACP,KAAK,EAAE,QAAQ;IAMlB,MAAM,IAAI,uBAAuB;CAMjD"}
@@ -0,0 +1,15 @@
1
+ import { BaseException, } from '@hg-ts/exception';
2
+ export class ValidationException extends BaseException {
3
+ errors;
4
+ constructor(error) {
5
+ super('Validation failed');
6
+ this.errors = error.issues;
7
+ }
8
+ toJSON() {
9
+ return {
10
+ ...super.toJSON(),
11
+ errors: this.errors,
12
+ };
13
+ }
14
+ }
15
+ //# sourceMappingURL=validation.exception.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validation.exception.js","sourceRoot":"","sources":["../src/validation.exception.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,aAAa,GAEb,MAAM,kBAAkB,CAAC;AAO1B,MAAM,OAAO,mBAAoB,SAAQ,aAAa;IAC9C,MAAM,CAAa;IAC1B,YAAmB,KAAe;QACjC,KAAK,CAAC,mBAAmB,CAAC,CAAC;QAE3B,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC;IAC5B,CAAC;IAEe,MAAM;QACrB,OAAO;YACN,GAAG,KAAK,CAAC,MAAM,EAAE;YACjB,MAAM,EAAE,IAAI,CAAC,MAAM;SACnB,CAAC;IACH,CAAC;CACD"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hg-ts/validation",
3
- "version": "0.1.40",
3
+ "version": "0.1.42",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist-esm/index.js",
6
6
  "repository": "git@gitlab.com:hyper-graph/framework.git",
@@ -12,10 +12,10 @@
12
12
  "lint:fix": "hg lint:fix"
13
13
  },
14
14
  "devDependencies": {
15
- "@hg-ts-config/eslint-config": "0.1.40",
16
- "@hg-ts-config/typescript": "0.1.40",
17
- "@hg-ts/cli": "0.1.40",
18
- "@hg-ts/types": "0.1.40",
15
+ "@hg-ts-config/eslint-config": "0.1.42",
16
+ "@hg-ts-config/typescript": "0.1.42",
17
+ "@hg-ts/cli": "0.1.42",
18
+ "@hg-ts/types": "0.1.42",
19
19
  "@types/node": "20.8.7",
20
20
  "@typescript-eslint/eslint-plugin": "6.8.0",
21
21
  "@typescript-eslint/parser": "6.8.0",
@@ -30,6 +30,7 @@
30
30
  "tslib": "*"
31
31
  },
32
32
  "dependencies": {
33
+ "@hg-ts/exception": "0.1.42",
33
34
  "zod": "3.22.5"
34
35
  }
35
36
  }
package/src/dto.ts CHANGED
@@ -1,13 +1,16 @@
1
1
  import {
2
+ ZodError,
2
3
  ZodSchema,
3
4
  ZodTypeDef,
4
5
  } from 'zod';
6
+ import { ValidationException } from './validation.exception';
5
7
 
6
8
  export type ZodDto<TOutput = any> = {
7
9
  new (): TOutput;
8
10
  isZodDto: true;
9
11
  schema: ZodSchema<TOutput, ZodTypeDef, unknown>;
10
12
  create(input: unknown): TOutput;
13
+ createAsync(input: unknown): Promise<TOutput>;
11
14
  }
12
15
 
13
16
  export function createZodDto<TOutput = any>(
@@ -18,9 +21,39 @@ export function createZodDto<TOutput = any>(
18
21
  public static schema = schema;
19
22
 
20
23
  public static create(input: unknown): AugmentedZodDto {
21
- return this.schema.parse(input) as AugmentedZodDto;
24
+ try {
25
+ return this.schema.parse(input) as AugmentedZodDto;
26
+ } catch (error: unknown) {
27
+ if (error instanceof ZodError) {
28
+ AugmentedZodDto.formatError(error);
29
+ }
30
+
31
+ throw error;
32
+ }
33
+ }
34
+
35
+ public static async createAsync(input: unknown): Promise<AugmentedZodDto> {
36
+ try {
37
+ const result = await this.schema.parseAsync(input);
38
+
39
+ return result as AugmentedZodDto;
40
+ } catch (error: unknown) {
41
+ if (error instanceof ZodError) {
42
+ AugmentedZodDto.formatError(error);
43
+ }
44
+
45
+ throw error;
46
+ }
47
+ }
48
+
49
+ protected static formatError(error: ZodError): never {
50
+ throw new ValidationException(error);
22
51
  }
23
52
  }
24
53
 
25
54
  return AugmentedZodDto as unknown as ZodDto<TOutput>;
26
55
  }
56
+
57
+ export function isZodDto(ctor: Class<any, any[]>): ctor is ZodDto {
58
+ return 'isZodDto' in ctor && ctor.isZodDto === true;
59
+ }
package/src/index.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  import './extensions';
2
- export * from 'zod';
3
2
  import zod from 'zod';
4
3
 
4
+ export { ZodDto, isZodDto } from './dto';
5
+ export * from './validation.exception';
6
+ export * from 'zod';
5
7
  export default zod;
@@ -0,0 +1,25 @@
1
+ import {
2
+ BaseException,
3
+ JSONBaseException,
4
+ } from '@hg-ts/exception';
5
+ import { ZodError, ZodIssue } from 'zod';
6
+
7
+ export type JSONValidationException = JSONBaseException & {
8
+ errors: ZodIssue[];
9
+ }
10
+
11
+ export class ValidationException extends BaseException {
12
+ public errors: ZodIssue[];
13
+ public constructor(error: ZodError) {
14
+ super('Validation failed');
15
+
16
+ this.errors = error.issues;
17
+ }
18
+
19
+ public override toJSON(): JSONValidationException {
20
+ return {
21
+ ...super.toJSON(),
22
+ errors: this.errors,
23
+ };
24
+ }
25
+ }