@hy_ong/zod-kit 0.2.3 → 0.2.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.
@@ -1,61 +0,0 @@
1
- import {
2
- getLocale,
3
- t
4
- } from "./chunk-POIDES2L.js";
5
-
6
- // src/validators/common/one-of.ts
7
- import { z } from "zod";
8
- function oneOf(required, options) {
9
- const { values = [], defaultValue = null, caseSensitive = true, transform, i18n } = options ?? {};
10
- const isRequired = required ?? false;
11
- const getMessage = (key, params) => {
12
- if (i18n) {
13
- const currentLocale = getLocale();
14
- const customMessages = i18n[currentLocale];
15
- if (customMessages && customMessages[key]) {
16
- const template = customMessages[key];
17
- return template.replace(/\$\{(\w+)}/g, (_, k) => params?.[k] ?? "");
18
- }
19
- }
20
- return t(`common.oneOf.${key}`, params);
21
- };
22
- const preprocessFn = (val) => {
23
- if (val === "" || val === null || val === void 0) {
24
- return defaultValue;
25
- }
26
- const hasNumbers = values.some((v) => typeof v === "number");
27
- if (hasNumbers && typeof val === "string" && !isNaN(Number(val)) && val.trim() !== "") {
28
- const numVal = Number(val);
29
- if (values.includes(numVal)) return numVal;
30
- }
31
- if (!caseSensitive && typeof val === "string") {
32
- const match = values.find((v) => typeof v === "string" && v.toLowerCase() === val.toLowerCase());
33
- if (match !== void 0) return match;
34
- return val;
35
- }
36
- return val;
37
- };
38
- const baseSchema = z.preprocess(preprocessFn, z.any());
39
- const schema = baseSchema.superRefine((val, ctx) => {
40
- if (val === null) {
41
- if (isRequired) {
42
- ctx.addIssue({ code: "custom", message: getMessage("required") });
43
- }
44
- return;
45
- }
46
- if (!values.includes(val)) {
47
- ctx.addIssue({
48
- code: "custom",
49
- message: getMessage("invalid", { values: values.join(", ") })
50
- });
51
- }
52
- }).transform((val) => {
53
- if (val === null || !transform) return val;
54
- return transform(val);
55
- });
56
- return schema;
57
- }
58
-
59
- export {
60
- oneOf
61
- };
@@ -1,61 +0,0 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
2
-
3
-
4
- var _chunkQ7TUNJD4cjs = require('./chunk-Q7TUNJD4.cjs');
5
-
6
- // src/validators/common/one-of.ts
7
- var _zod = require('zod');
8
- function oneOf(required, options) {
9
- const { values = [], defaultValue = null, caseSensitive = true, transform, i18n } = _nullishCoalesce(options, () => ( {}));
10
- const isRequired = _nullishCoalesce(required, () => ( false));
11
- const getMessage = (key, params) => {
12
- if (i18n) {
13
- const currentLocale = _chunkQ7TUNJD4cjs.getLocale.call(void 0, );
14
- const customMessages = i18n[currentLocale];
15
- if (customMessages && customMessages[key]) {
16
- const template = customMessages[key];
17
- return template.replace(/\$\{(\w+)}/g, (_, k) => _nullishCoalesce(_optionalChain([params, 'optionalAccess', _2 => _2[k]]), () => ( "")));
18
- }
19
- }
20
- return _chunkQ7TUNJD4cjs.t.call(void 0, `common.oneOf.${key}`, params);
21
- };
22
- const preprocessFn = (val) => {
23
- if (val === "" || val === null || val === void 0) {
24
- return defaultValue;
25
- }
26
- const hasNumbers = values.some((v) => typeof v === "number");
27
- if (hasNumbers && typeof val === "string" && !isNaN(Number(val)) && val.trim() !== "") {
28
- const numVal = Number(val);
29
- if (values.includes(numVal)) return numVal;
30
- }
31
- if (!caseSensitive && typeof val === "string") {
32
- const match = values.find((v) => typeof v === "string" && v.toLowerCase() === val.toLowerCase());
33
- if (match !== void 0) return match;
34
- return val;
35
- }
36
- return val;
37
- };
38
- const baseSchema = _zod.z.preprocess(preprocessFn, _zod.z.any());
39
- const schema = baseSchema.superRefine((val, ctx) => {
40
- if (val === null) {
41
- if (isRequired) {
42
- ctx.addIssue({ code: "custom", message: getMessage("required") });
43
- }
44
- return;
45
- }
46
- if (!values.includes(val)) {
47
- ctx.addIssue({
48
- code: "custom",
49
- message: getMessage("invalid", { values: values.join(", ") })
50
- });
51
- }
52
- }).transform((val) => {
53
- if (val === null || !transform) return val;
54
- return transform(val);
55
- });
56
- return schema;
57
- }
58
-
59
-
60
-
61
- exports.oneOf = oneOf;