hh-contracts 0.0.89 → 0.0.91

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.
@@ -15,3 +15,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./create-api"), exports);
18
+ __exportStar(require("./zod-message-key"), exports);
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getZodMessageKey = void 0;
4
+ var zod_message_key_util_1 = require("./zod-message-key.util");
5
+ Object.defineProperty(exports, "getZodMessageKey", { enumerable: true, get: function () { return zod_message_key_util_1.getZodMessageKey; } });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getZodMessageKey = getZodMessageKey;
4
+ // Базовая мапа обработчиков по кодам Zod v4
5
+ const handlers = {
6
+ invalid_type: i => `zod.invalid_type.${i.expected}`,
7
+ invalid_format: i => {
8
+ const format = String(i.format);
9
+ return `zod.invalid_format.${format}`;
10
+ },
11
+ too_small: i => `zod.too_small.${i.origin}.${i.exact ? 'exact' : 'min'}`,
12
+ too_big: i => `zod.too_big.${i.origin}.${i.exact ? 'exact' : 'max'}`,
13
+ not_multiple_of: () => 'zod.not_multiple_of',
14
+ unrecognized_keys: () => 'zod.unrecognized_keys',
15
+ invalid_union: () => 'zod.invalid_union',
16
+ invalid_key: i => `zod.invalid_key.${i.origin}`,
17
+ invalid_element: i => `zod.invalid_element.${i.origin}`,
18
+ invalid_value: () => 'zod.invalid_value',
19
+ custom: i => (typeof i.params?.messageKey === 'string' ? i.params.messageKey : 'zod.custom'),
20
+ };
21
+ function getZodMessageKey(issue) {
22
+ const handler = handlers[issue.code];
23
+ const tail = handler ? handler(issue) : 'zod.unknown';
24
+ return `errors.${tail}`;
25
+ }
@@ -40,7 +40,11 @@ const translate_api_1 = require("../translate.api");
40
40
  const permissions_1 = require("../../permissions");
41
41
  const roles_1 = require("../../roles");
42
42
  const translate_schemas_1 = require("../translate.schemas");
43
- const makeReqVariant = (entity, schema, skipKeys) => z.object({ entity: z.literal(entity), object: schema, skipKeys });
43
+ const makeReqVariant = (entity, schema) => z.object({
44
+ entity: z.literal(entity),
45
+ object: schema,
46
+ skipKeys: z.array(z.string()).optional(),
47
+ });
44
48
  const makeResVariant = (entity, schema) => z.object({
45
49
  entity: z.literal(entity),
46
50
  translations: z.object({}).catchall(schema),
@@ -1 +1,2 @@
1
1
  export * from './create-api';
2
+ export * from './zod-message-key';
@@ -0,0 +1 @@
1
+ export { getZodMessageKey } from './zod-message-key.util';
@@ -0,0 +1,7 @@
1
+ import * as z from 'zod';
2
+
3
+ export type TZodIssue = z.core.$ZodIssue;
4
+ export type TCode = TZodIssue['code'];
5
+ export type TIssueOf<C extends TCode> = Extract<TZodIssue, { code: C }>;
6
+ export type THandler<C extends TCode> = (issue: TIssueOf<C>) => string;
7
+ export type THandlerMap = Partial<{ [C in TCode]: THandler<C> }>;
@@ -0,0 +1,31 @@
1
+ import * as z from 'zod';
2
+ import { THandlerMap, TIssueOf, TZodIssue } from './types';
3
+
4
+ // Базовая мапа обработчиков по кодам Zod v4
5
+ const handlers = {
6
+ invalid_type: i => `zod.invalid_type.${i.expected}`,
7
+ invalid_format: i => {
8
+ const format = String(i.format);
9
+ return `zod.invalid_format.${format}`;
10
+ },
11
+
12
+ too_small: i => `zod.too_small.${i.origin}.${i.exact ? 'exact' : 'min'}`,
13
+ too_big: i => `zod.too_big.${i.origin}.${i.exact ? 'exact' : 'max'}`,
14
+
15
+ not_multiple_of: () => 'zod.not_multiple_of',
16
+
17
+ unrecognized_keys: () => 'zod.unrecognized_keys',
18
+ invalid_union: () => 'zod.invalid_union',
19
+
20
+ invalid_key: i => `zod.invalid_key.${i.origin}`,
21
+ invalid_element: i => `zod.invalid_element.${i.origin}`,
22
+ invalid_value: () => 'zod.invalid_value',
23
+ custom: i => (typeof i.params?.messageKey === 'string' ? i.params.messageKey : 'zod.custom'),
24
+ } satisfies THandlerMap;
25
+
26
+ export function getZodMessageKey(issue: TZodIssue): string {
27
+ const handler = handlers[issue.code] as ((i: TIssueOf<typeof issue.code>) => string) | undefined;
28
+ const tail = handler ? handler(issue) : 'zod.unknown';
29
+
30
+ return `errors.${tail}`;
31
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hh-contracts",
3
- "version": "0.0.89",
3
+ "version": "0.0.91",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "scripts": {
@@ -5,11 +5,12 @@ import { PermissionTranslationsSchema } from '../../permissions';
5
5
  import { RoleTranslationsSchema } from '../../roles';
6
6
  import { BaseTranslateSchema } from '../translate.schemas';
7
7
 
8
- const makeReqVariant = <E extends TEntity, S extends z.ZodTypeAny>(
9
- entity: E,
10
- schema: S,
11
- skipKeys?: readonly (keyof z.infer<S>)[],
12
- ) => z.object({ entity: z.literal(entity), object: schema, skipKeys });
8
+ const makeReqVariant = <E extends TEntity, S extends z.ZodObject>(entity: E, schema: S) =>
9
+ z.object({
10
+ entity: z.literal(entity),
11
+ object: schema,
12
+ skipKeys: z.array(z.string()).optional(),
13
+ });
13
14
 
14
15
  const makeResVariant = <E extends TEntity, S extends z.ZodTypeAny>(entity: E, schema: S) =>
15
16
  z.object({