hh-contracts 0.0.90 → 0.0.92

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.
@@ -29,4 +29,3 @@ __exportStar(require("./role-types.constant"), exports);
29
29
  __exportStar(require("./route-component-key.constants"), exports);
30
30
  __exportStar(require("./slug.constant"), exports);
31
31
  __exportStar(require("./translatable-field.constant"), exports);
32
- __exportStar(require("./zod-error-mapping.constant"), exports);
@@ -36,5 +36,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.makeTranslationsByLang = void 0;
37
37
  const z = __importStar(require("zod"));
38
38
  const language_keys_schema_1 = require("./language-keys.schema");
39
- const makeTranslationsByLang = (schema) => z.partialRecord(language_keys_schema_1.LanguageKeysSchema, schema);
39
+ const makeTranslationsByLang = (schema) => z
40
+ .partialRecord(language_keys_schema_1.LanguageKeysSchema, schema)
41
+ .refine(translations => Object.keys(translations).length > 0, {
42
+ params: {
43
+ messageKey: 'zod.translations.must_not_be_empty',
44
+ },
45
+ });
40
46
  exports.makeTranslationsByLang = makeTranslationsByLang;
@@ -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
+ }
@@ -13,4 +13,3 @@ export * from './role-types.constant';
13
13
  export * from './route-component-key.constants';
14
14
  export * from './slug.constant';
15
15
  export * from './translatable-field.constant';
16
- export * from './zod-error-mapping.constant';
@@ -2,7 +2,13 @@ import * as z from 'zod';
2
2
  import { LanguageKeysSchema } from './language-keys.schema';
3
3
 
4
4
  export const makeTranslationsByLang = <T extends z.ZodObject>(schema: T) =>
5
- z.partialRecord(LanguageKeysSchema, schema);
5
+ z
6
+ .partialRecord(LanguageKeysSchema, schema)
7
+ .refine(translations => Object.keys(translations).length > 0, {
8
+ params: {
9
+ messageKey: 'zod.translations.must_not_be_empty',
10
+ },
11
+ });
6
12
  export type TranslationsByLang<T extends object> = Partial<
7
13
  Record<z.infer<typeof LanguageKeysSchema>, T>
8
14
  >;
@@ -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.90",
3
+ "version": "0.0.92",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "scripts": {
@@ -1,46 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.ZOD_ERROR_MAPPING = void 0;
37
- const z = __importStar(require("zod"));
38
- exports.ZOD_ERROR_MAPPING = {
39
- [z.ZodIssueCode.invalid_type]: 'errors.validation.invalidType',
40
- [z.ZodIssueCode.custom]: 'errors.validation.custom',
41
- [z.ZodIssueCode.invalid_union]: 'errors.validation.invalidUnion',
42
- [z.ZodIssueCode.unrecognized_keys]: 'errors.validation.unrecognizedKeys',
43
- [z.ZodIssueCode.too_small]: 'errors.validation.tooSmall',
44
- [z.ZodIssueCode.too_big]: 'errors.validation.tooBig',
45
- [z.ZodIssueCode.not_multiple_of]: 'errors.validation.notMultipleOf',
46
- };
@@ -1,13 +0,0 @@
1
- import * as z from 'zod';
2
-
3
- export const ZOD_ERROR_MAPPING = {
4
- [z.ZodIssueCode.invalid_type]: 'errors.validation.invalidType',
5
- [z.ZodIssueCode.custom]: 'errors.validation.custom',
6
- [z.ZodIssueCode.invalid_union]: 'errors.validation.invalidUnion',
7
- [z.ZodIssueCode.unrecognized_keys]: 'errors.validation.unrecognizedKeys',
8
- [z.ZodIssueCode.too_small]: 'errors.validation.tooSmall',
9
- [z.ZodIssueCode.too_big]: 'errors.validation.tooBig',
10
- [z.ZodIssueCode.not_multiple_of]: 'errors.validation.notMultipleOf',
11
- };
12
-
13
- export type TZodErorrKey = (typeof ZOD_ERROR_MAPPING)[keyof typeof ZOD_ERROR_MAPPING];