@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.
@@ -0,0 +1,77 @@
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 valuesArr = values;
12
+ const isAllStrings = valuesArr.every((v) => typeof v === "string");
13
+ const getMessage = (key, params) => {
14
+ if (i18n) {
15
+ const currentLocale = _chunkQ7TUNJD4cjs.getLocale.call(void 0, );
16
+ const customMessages = i18n[currentLocale];
17
+ if (customMessages && customMessages[key]) {
18
+ const template = customMessages[key];
19
+ return template.replace(/\$\{(\w+)}/g, (_, k) => _nullishCoalesce(_optionalChain([params, 'optionalAccess', _2 => _2[k]]), () => ( "")));
20
+ }
21
+ }
22
+ return _chunkQ7TUNJD4cjs.t.call(void 0, `common.oneOf.${key}`, params);
23
+ };
24
+ const isEmpty = (v) => v === "" || v === null || v === void 0;
25
+ let valueSchema;
26
+ if (isAllStrings && valuesArr.length > 0) {
27
+ valueSchema = _zod.z.enum(valuesArr, {
28
+ error: (issue) => {
29
+ if (isEmpty(issue.input)) return getMessage("required");
30
+ return getMessage("invalid", { values: valuesArr.join(", ") });
31
+ }
32
+ });
33
+ } else if (valuesArr.length >= 2) {
34
+ const literals = valuesArr.map((v) => _zod.z.literal(v));
35
+ valueSchema = _zod.z.union(literals, {
36
+ error: () => getMessage("invalid", { values: valuesArr.join(", ") })
37
+ });
38
+ } else if (valuesArr.length === 1) {
39
+ valueSchema = _zod.z.literal(valuesArr[0]);
40
+ } else {
41
+ valueSchema = _zod.z.never();
42
+ }
43
+ if (!caseSensitive) {
44
+ valueSchema = _zod.z.string().transform((v) => {
45
+ const match = valuesArr.find((val) => typeof val === "string" && val.toLowerCase() === v.toLowerCase());
46
+ return _nullishCoalesce(match, () => ( v));
47
+ }).pipe(valueSchema);
48
+ }
49
+ if (transform) {
50
+ valueSchema = valueSchema.transform((v) => transform(v));
51
+ }
52
+ const fallback = defaultValue;
53
+ const emptySchema = _zod.z.union([_zod.z.literal(""), _zod.z.null(), _zod.z.undefined()]).transform(() => fallback);
54
+ if (isRequired && fallback === null) {
55
+ const emptyRejectSchema = _zod.z.union([_zod.z.literal(""), _zod.z.null(), _zod.z.undefined()]).refine(() => false, { message: getMessage("required") });
56
+ if (!isAllStrings) {
57
+ const numCoerceSchema = _zod.z.string().transform((v) => {
58
+ const n = Number(v);
59
+ return isNaN(n) ? v : n;
60
+ }).pipe(valueSchema);
61
+ return _zod.z.union([valueSchema, numCoerceSchema, emptyRejectSchema]);
62
+ }
63
+ return _zod.z.union([valueSchema, emptyRejectSchema]);
64
+ }
65
+ if (!isAllStrings) {
66
+ const numCoerceSchema = _zod.z.string().transform((v) => {
67
+ const n = Number(v);
68
+ return isNaN(n) ? v : n;
69
+ }).pipe(valueSchema);
70
+ return _zod.z.union([valueSchema, numCoerceSchema, emptySchema]);
71
+ }
72
+ return _zod.z.union([valueSchema, emptySchema]);
73
+ }
74
+
75
+
76
+
77
+ exports.oneOf = oneOf;
@@ -8,6 +8,7 @@ var _zod = require('zod');
8
8
  function manyOf(required, options) {
9
9
  const { values = [], defaultValue = null, min, max, allowDuplicates = false, caseSensitive = true, transform, i18n } = _nullishCoalesce(options, () => ( {}));
10
10
  const isRequired = _nullishCoalesce(required, () => ( false));
11
+ const valuesArr = values;
11
12
  const getMessage = (key, params) => {
12
13
  if (i18n) {
13
14
  const currentLocale = _chunkQ7TUNJD4cjs.getLocale.call(void 0, );
@@ -20,44 +21,24 @@ function manyOf(required, options) {
20
21
  return _chunkQ7TUNJD4cjs.t.call(void 0, `common.manyOf.${key}`, params);
21
22
  };
22
23
  const normalizeItem = (item) => {
23
- const hasNumbers = values.some((v) => typeof v === "number");
24
+ const hasNumbers = valuesArr.some((v) => typeof v === "number");
24
25
  if (hasNumbers && typeof item === "string" && !isNaN(Number(item)) && item.trim() !== "") {
25
26
  const numVal = Number(item);
26
- if (values.includes(numVal)) return numVal;
27
+ if (valuesArr.includes(numVal)) return numVal;
27
28
  }
28
29
  if (!caseSensitive && typeof item === "string") {
29
- const match = values.find((v) => typeof v === "string" && v.toLowerCase() === item.toLowerCase());
30
+ const match = valuesArr.find((v) => typeof v === "string" && v.toLowerCase() === item.toLowerCase());
30
31
  if (match !== void 0) return match;
31
32
  return item;
32
33
  }
33
34
  return item;
34
35
  };
35
- const preprocessFn = (val) => {
36
- if (val === null || val === void 0 || val === "") {
37
- return defaultValue;
38
- }
39
- if (!Array.isArray(val)) {
40
- return [normalizeItem(val)];
41
- }
42
- return val.map(normalizeItem);
43
- };
44
- const baseSchema = _zod.z.preprocess(preprocessFn, _zod.z.any());
45
- const schema = baseSchema.superRefine((val, ctx) => {
46
- if (val === null) {
47
- if (isRequired) {
48
- ctx.addIssue({ code: "custom", message: getMessage("required") });
49
- }
50
- return;
51
- }
52
- if (!Array.isArray(val)) {
53
- ctx.addIssue({ code: "custom", message: getMessage("invalid", { values: values.join(", ") }) });
54
- return;
55
- }
36
+ const arraySchema = _zod.z.array(_zod.z.any()).transform((arr) => arr.map(normalizeItem)).superRefine((val, ctx) => {
56
37
  for (const item of val) {
57
- if (!values.includes(item)) {
38
+ if (!valuesArr.includes(item)) {
58
39
  ctx.addIssue({
59
40
  code: "custom",
60
- message: getMessage("invalid", { values: values.join(", ") })
41
+ message: getMessage("invalid", { values: valuesArr.join(", ") })
61
42
  });
62
43
  return;
63
44
  }
@@ -81,10 +62,17 @@ function manyOf(required, options) {
81
62
  return;
82
63
  }
83
64
  }).transform((val) => {
84
- if (val === null || !Array.isArray(val) || !transform) return val;
65
+ if (!transform) return val;
85
66
  return transform(val);
86
67
  });
87
- return schema;
68
+ const singleToArray = _zod.z.union([_zod.z.string(), _zod.z.number()]).transform((v) => [normalizeItem(v)]).pipe(arraySchema);
69
+ const fallback = defaultValue;
70
+ if (isRequired && fallback === null) {
71
+ const emptyRejectSchema = _zod.z.union([_zod.z.literal(""), _zod.z.null(), _zod.z.undefined()]).refine(() => false, { message: getMessage("required") });
72
+ return _zod.z.union([arraySchema, singleToArray, emptyRejectSchema]);
73
+ }
74
+ const emptySchema = _zod.z.union([_zod.z.literal(""), _zod.z.null(), _zod.z.undefined()]).transform(() => fallback);
75
+ return _zod.z.union([arraySchema, singleToArray, emptySchema]);
88
76
  }
89
77
 
90
78
 
@@ -8,6 +8,7 @@ import { z } from "zod";
8
8
  function manyOf(required, options) {
9
9
  const { values = [], defaultValue = null, min, max, allowDuplicates = false, caseSensitive = true, transform, i18n } = options ?? {};
10
10
  const isRequired = required ?? false;
11
+ const valuesArr = values;
11
12
  const getMessage = (key, params) => {
12
13
  if (i18n) {
13
14
  const currentLocale = getLocale();
@@ -20,44 +21,24 @@ function manyOf(required, options) {
20
21
  return t(`common.manyOf.${key}`, params);
21
22
  };
22
23
  const normalizeItem = (item) => {
23
- const hasNumbers = values.some((v) => typeof v === "number");
24
+ const hasNumbers = valuesArr.some((v) => typeof v === "number");
24
25
  if (hasNumbers && typeof item === "string" && !isNaN(Number(item)) && item.trim() !== "") {
25
26
  const numVal = Number(item);
26
- if (values.includes(numVal)) return numVal;
27
+ if (valuesArr.includes(numVal)) return numVal;
27
28
  }
28
29
  if (!caseSensitive && typeof item === "string") {
29
- const match = values.find((v) => typeof v === "string" && v.toLowerCase() === item.toLowerCase());
30
+ const match = valuesArr.find((v) => typeof v === "string" && v.toLowerCase() === item.toLowerCase());
30
31
  if (match !== void 0) return match;
31
32
  return item;
32
33
  }
33
34
  return item;
34
35
  };
35
- const preprocessFn = (val) => {
36
- if (val === null || val === void 0 || val === "") {
37
- return defaultValue;
38
- }
39
- if (!Array.isArray(val)) {
40
- return [normalizeItem(val)];
41
- }
42
- return val.map(normalizeItem);
43
- };
44
- const baseSchema = z.preprocess(preprocessFn, z.any());
45
- const schema = baseSchema.superRefine((val, ctx) => {
46
- if (val === null) {
47
- if (isRequired) {
48
- ctx.addIssue({ code: "custom", message: getMessage("required") });
49
- }
50
- return;
51
- }
52
- if (!Array.isArray(val)) {
53
- ctx.addIssue({ code: "custom", message: getMessage("invalid", { values: values.join(", ") }) });
54
- return;
55
- }
36
+ const arraySchema = z.array(z.any()).transform((arr) => arr.map(normalizeItem)).superRefine((val, ctx) => {
56
37
  for (const item of val) {
57
- if (!values.includes(item)) {
38
+ if (!valuesArr.includes(item)) {
58
39
  ctx.addIssue({
59
40
  code: "custom",
60
- message: getMessage("invalid", { values: values.join(", ") })
41
+ message: getMessage("invalid", { values: valuesArr.join(", ") })
61
42
  });
62
43
  return;
63
44
  }
@@ -81,10 +62,17 @@ function manyOf(required, options) {
81
62
  return;
82
63
  }
83
64
  }).transform((val) => {
84
- if (val === null || !Array.isArray(val) || !transform) return val;
65
+ if (!transform) return val;
85
66
  return transform(val);
86
67
  });
87
- return schema;
68
+ const singleToArray = z.union([z.string(), z.number()]).transform((v) => [normalizeItem(v)]).pipe(arraySchema);
69
+ const fallback = defaultValue;
70
+ if (isRequired && fallback === null) {
71
+ const emptyRejectSchema = z.union([z.literal(""), z.null(), z.undefined()]).refine(() => false, { message: getMessage("required") });
72
+ return z.union([arraySchema, singleToArray, emptyRejectSchema]);
73
+ }
74
+ const emptySchema = z.union([z.literal(""), z.null(), z.undefined()]).transform(() => fallback);
75
+ return z.union([arraySchema, singleToArray, emptySchema]);
88
76
  }
89
77
 
90
78
  export {
@@ -0,0 +1,77 @@
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 valuesArr = values;
12
+ const isAllStrings = valuesArr.every((v) => typeof v === "string");
13
+ const getMessage = (key, params) => {
14
+ if (i18n) {
15
+ const currentLocale = getLocale();
16
+ const customMessages = i18n[currentLocale];
17
+ if (customMessages && customMessages[key]) {
18
+ const template = customMessages[key];
19
+ return template.replace(/\$\{(\w+)}/g, (_, k) => params?.[k] ?? "");
20
+ }
21
+ }
22
+ return t(`common.oneOf.${key}`, params);
23
+ };
24
+ const isEmpty = (v) => v === "" || v === null || v === void 0;
25
+ let valueSchema;
26
+ if (isAllStrings && valuesArr.length > 0) {
27
+ valueSchema = z.enum(valuesArr, {
28
+ error: (issue) => {
29
+ if (isEmpty(issue.input)) return getMessage("required");
30
+ return getMessage("invalid", { values: valuesArr.join(", ") });
31
+ }
32
+ });
33
+ } else if (valuesArr.length >= 2) {
34
+ const literals = valuesArr.map((v) => z.literal(v));
35
+ valueSchema = z.union(literals, {
36
+ error: () => getMessage("invalid", { values: valuesArr.join(", ") })
37
+ });
38
+ } else if (valuesArr.length === 1) {
39
+ valueSchema = z.literal(valuesArr[0]);
40
+ } else {
41
+ valueSchema = z.never();
42
+ }
43
+ if (!caseSensitive) {
44
+ valueSchema = z.string().transform((v) => {
45
+ const match = valuesArr.find((val) => typeof val === "string" && val.toLowerCase() === v.toLowerCase());
46
+ return match ?? v;
47
+ }).pipe(valueSchema);
48
+ }
49
+ if (transform) {
50
+ valueSchema = valueSchema.transform((v) => transform(v));
51
+ }
52
+ const fallback = defaultValue;
53
+ const emptySchema = z.union([z.literal(""), z.null(), z.undefined()]).transform(() => fallback);
54
+ if (isRequired && fallback === null) {
55
+ const emptyRejectSchema = z.union([z.literal(""), z.null(), z.undefined()]).refine(() => false, { message: getMessage("required") });
56
+ if (!isAllStrings) {
57
+ const numCoerceSchema = z.string().transform((v) => {
58
+ const n = Number(v);
59
+ return isNaN(n) ? v : n;
60
+ }).pipe(valueSchema);
61
+ return z.union([valueSchema, numCoerceSchema, emptyRejectSchema]);
62
+ }
63
+ return z.union([valueSchema, emptyRejectSchema]);
64
+ }
65
+ if (!isAllStrings) {
66
+ const numCoerceSchema = z.string().transform((v) => {
67
+ const n = Number(v);
68
+ return isNaN(n) ? v : n;
69
+ }).pipe(valueSchema);
70
+ return z.union([valueSchema, numCoerceSchema, emptySchema]);
71
+ }
72
+ return z.union([valueSchema, emptySchema]);
73
+ }
74
+
75
+ export {
76
+ oneOf
77
+ };
@@ -1,7 +1,7 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
- var _chunkBZWQPSHOcjs = require('../chunk-BZWQPSHO.cjs');
3
+ var _chunk5U5ERV2Pcjs = require('../chunk-5U5ERV2P.cjs');
4
4
  require('../chunk-Q7TUNJD4.cjs');
5
5
 
6
6
 
7
- exports.manyOf = _chunkBZWQPSHOcjs.manyOf;
7
+ exports.manyOf = _chunk5U5ERV2Pcjs.manyOf;
@@ -7,8 +7,10 @@ import { L as Locale } from '../config-CABSSvAp.cjs';
7
7
  * Provides multi-select validation that restricts input to an array of values
8
8
  * from a predefined set, with min/max selection, duplicate control, and transformation.
9
9
  *
10
+ * Avoids z.preprocess() to preserve z.input types for React Hook Form compatibility.
11
+ *
10
12
  * @author Ong Hoe Yuan
11
- * @version 0.2.2
13
+ * @version 0.2.5
12
14
  */
13
15
 
14
16
  /**
@@ -32,11 +34,11 @@ type ManyOfMessages = {
32
34
  * Configuration options for manyOf validation
33
35
  *
34
36
  * @template IsRequired - Whether the field is required (affects return type)
35
- * @template T - The type of allowed values
37
+ * @template V - The tuple type of allowed values, preserving literal types
36
38
  *
37
39
  * @interface ManyOfOptions
38
- * @property {T[]} values - Array of allowed values
39
- * @property {T[] | null} [defaultValue] - Default value when input is empty
40
+ * @property {V} values - Array of allowed values
41
+ * @property {V[number][] | null} [defaultValue] - Default value when input is empty
40
42
  * @property {number} [min] - Minimum number of selections
41
43
  * @property {number} [max] - Maximum number of selections
42
44
  * @property {boolean} [allowDuplicates=false] - Whether to allow duplicate selections
@@ -44,31 +46,33 @@ type ManyOfMessages = {
44
46
  * @property {Function} [transform] - Custom transformation function applied to each value
45
47
  * @property {Record<Locale, ManyOfMessages>} [i18n] - Custom error messages for different locales
46
48
  */
47
- type ManyOfOptions<IsRequired extends boolean = true, T extends string | number = string | number> = {
48
- values: T[];
49
- defaultValue?: IsRequired extends true ? T[] : T[] | null;
49
+ type ManyOfOptions<IsRequired extends boolean = true, V extends readonly (string | number)[] = readonly (string | number)[]> = {
50
+ values: V;
51
+ defaultValue?: IsRequired extends true ? V[number][] : V[number][] | null;
50
52
  min?: number;
51
53
  max?: number;
52
54
  allowDuplicates?: boolean;
53
55
  caseSensitive?: boolean;
54
- transform?: (value: T[]) => T[];
56
+ transform?: (value: V[number][]) => V[number][];
55
57
  i18n?: Partial<Record<Locale, Partial<ManyOfMessages>>>;
56
58
  };
57
59
  /**
58
60
  * Type alias for manyOf validation schema based on required flag
59
61
  *
60
62
  * @template IsRequired - Whether the field is required
61
- * @template T - The type of allowed values
63
+ * @template V - The tuple type of allowed values
62
64
  */
63
- type ManyOfSchema<IsRequired extends boolean, T> = IsRequired extends true ? ZodType<T[]> : ZodType<T[] | null>;
65
+ type ManyOfSchema<IsRequired extends boolean, V extends readonly (string | number)[]> = IsRequired extends true ? ZodType<V[number][], V[number][] | "" | null | undefined> : ZodType<V[number][] | null, V[number][] | "" | null | undefined>;
64
66
  /**
65
67
  * Creates a Zod schema for multi-select validation that restricts values to a predefined set
66
68
  *
69
+ * Avoids z.preprocess() to preserve z.input types for React Hook Form compatibility.
70
+ *
67
71
  * @template IsRequired - Whether the field is required (affects return type)
68
- * @template T - The type of allowed values (string | number)
72
+ * @template V - The tuple type of allowed values (inferred via const type parameter)
69
73
  * @param {IsRequired} [required=false] - Whether the field is required
70
- * @param {ManyOfOptions<IsRequired, T>} options - Configuration options (values is required)
71
- * @returns {ManyOfSchema<IsRequired, T>} Zod schema for manyOf validation
74
+ * @param {ManyOfOptions<IsRequired, V>} options - Configuration options (values is required)
75
+ * @returns {ManyOfSchema<IsRequired, V>} Zod schema for manyOf validation
72
76
  *
73
77
  * @example
74
78
  * ```typescript
@@ -106,6 +110,6 @@ type ManyOfSchema<IsRequired extends boolean, T> = IsRequired extends true ? Zod
106
110
  * itemsSchema.parse([1, 1, 2]) // ✓ [1, 1, 2]
107
111
  * ```
108
112
  */
109
- declare function manyOf<IsRequired extends boolean = false, T extends string | number = string | number>(required?: IsRequired, options?: Omit<ManyOfOptions<IsRequired, T>, "required">): ManyOfSchema<IsRequired, T>;
113
+ declare function manyOf<IsRequired extends boolean = false, const V extends readonly (string | number)[] = readonly (string | number)[]>(required?: IsRequired, options?: Omit<ManyOfOptions<IsRequired, V>, "required">): ManyOfSchema<IsRequired, V>;
110
114
 
111
115
  export { type ManyOfMessages, type ManyOfOptions, type ManyOfSchema, manyOf };
@@ -7,8 +7,10 @@ import { L as Locale } from '../config-CABSSvAp.js';
7
7
  * Provides multi-select validation that restricts input to an array of values
8
8
  * from a predefined set, with min/max selection, duplicate control, and transformation.
9
9
  *
10
+ * Avoids z.preprocess() to preserve z.input types for React Hook Form compatibility.
11
+ *
10
12
  * @author Ong Hoe Yuan
11
- * @version 0.2.2
13
+ * @version 0.2.5
12
14
  */
13
15
 
14
16
  /**
@@ -32,11 +34,11 @@ type ManyOfMessages = {
32
34
  * Configuration options for manyOf validation
33
35
  *
34
36
  * @template IsRequired - Whether the field is required (affects return type)
35
- * @template T - The type of allowed values
37
+ * @template V - The tuple type of allowed values, preserving literal types
36
38
  *
37
39
  * @interface ManyOfOptions
38
- * @property {T[]} values - Array of allowed values
39
- * @property {T[] | null} [defaultValue] - Default value when input is empty
40
+ * @property {V} values - Array of allowed values
41
+ * @property {V[number][] | null} [defaultValue] - Default value when input is empty
40
42
  * @property {number} [min] - Minimum number of selections
41
43
  * @property {number} [max] - Maximum number of selections
42
44
  * @property {boolean} [allowDuplicates=false] - Whether to allow duplicate selections
@@ -44,31 +46,33 @@ type ManyOfMessages = {
44
46
  * @property {Function} [transform] - Custom transformation function applied to each value
45
47
  * @property {Record<Locale, ManyOfMessages>} [i18n] - Custom error messages for different locales
46
48
  */
47
- type ManyOfOptions<IsRequired extends boolean = true, T extends string | number = string | number> = {
48
- values: T[];
49
- defaultValue?: IsRequired extends true ? T[] : T[] | null;
49
+ type ManyOfOptions<IsRequired extends boolean = true, V extends readonly (string | number)[] = readonly (string | number)[]> = {
50
+ values: V;
51
+ defaultValue?: IsRequired extends true ? V[number][] : V[number][] | null;
50
52
  min?: number;
51
53
  max?: number;
52
54
  allowDuplicates?: boolean;
53
55
  caseSensitive?: boolean;
54
- transform?: (value: T[]) => T[];
56
+ transform?: (value: V[number][]) => V[number][];
55
57
  i18n?: Partial<Record<Locale, Partial<ManyOfMessages>>>;
56
58
  };
57
59
  /**
58
60
  * Type alias for manyOf validation schema based on required flag
59
61
  *
60
62
  * @template IsRequired - Whether the field is required
61
- * @template T - The type of allowed values
63
+ * @template V - The tuple type of allowed values
62
64
  */
63
- type ManyOfSchema<IsRequired extends boolean, T> = IsRequired extends true ? ZodType<T[]> : ZodType<T[] | null>;
65
+ type ManyOfSchema<IsRequired extends boolean, V extends readonly (string | number)[]> = IsRequired extends true ? ZodType<V[number][], V[number][] | "" | null | undefined> : ZodType<V[number][] | null, V[number][] | "" | null | undefined>;
64
66
  /**
65
67
  * Creates a Zod schema for multi-select validation that restricts values to a predefined set
66
68
  *
69
+ * Avoids z.preprocess() to preserve z.input types for React Hook Form compatibility.
70
+ *
67
71
  * @template IsRequired - Whether the field is required (affects return type)
68
- * @template T - The type of allowed values (string | number)
72
+ * @template V - The tuple type of allowed values (inferred via const type parameter)
69
73
  * @param {IsRequired} [required=false] - Whether the field is required
70
- * @param {ManyOfOptions<IsRequired, T>} options - Configuration options (values is required)
71
- * @returns {ManyOfSchema<IsRequired, T>} Zod schema for manyOf validation
74
+ * @param {ManyOfOptions<IsRequired, V>} options - Configuration options (values is required)
75
+ * @returns {ManyOfSchema<IsRequired, V>} Zod schema for manyOf validation
72
76
  *
73
77
  * @example
74
78
  * ```typescript
@@ -106,6 +110,6 @@ type ManyOfSchema<IsRequired extends boolean, T> = IsRequired extends true ? Zod
106
110
  * itemsSchema.parse([1, 1, 2]) // ✓ [1, 1, 2]
107
111
  * ```
108
112
  */
109
- declare function manyOf<IsRequired extends boolean = false, T extends string | number = string | number>(required?: IsRequired, options?: Omit<ManyOfOptions<IsRequired, T>, "required">): ManyOfSchema<IsRequired, T>;
113
+ declare function manyOf<IsRequired extends boolean = false, const V extends readonly (string | number)[] = readonly (string | number)[]>(required?: IsRequired, options?: Omit<ManyOfOptions<IsRequired, V>, "required">): ManyOfSchema<IsRequired, V>;
110
114
 
111
115
  export { type ManyOfMessages, type ManyOfOptions, type ManyOfSchema, manyOf };
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  manyOf
3
- } from "../chunk-7X3XPK6A.js";
3
+ } from "../chunk-S4OJGXWC.js";
4
4
  import "../chunk-POIDES2L.js";
5
5
  export {
6
6
  manyOf
@@ -1,7 +1,7 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
- var _chunkFMPHW7IDcjs = require('../chunk-FMPHW7ID.cjs');
3
+ var _chunk5NBWPAHCcjs = require('../chunk-5NBWPAHC.cjs');
4
4
  require('../chunk-Q7TUNJD4.cjs');
5
5
 
6
6
 
7
- exports.oneOf = _chunkFMPHW7IDcjs.oneOf;
7
+ exports.oneOf = _chunk5NBWPAHCcjs.oneOf;
@@ -7,8 +7,11 @@ import { L as Locale } from '../config-CABSSvAp.cjs';
7
7
  * Provides single-select validation that restricts input to a predefined set of allowed values,
8
8
  * with support for case-insensitive matching, default values, and transformation.
9
9
  *
10
+ * Uses z.enum() internally to preserve literal type inference for both z.input and z.output,
11
+ * making it compatible with React Hook Form resolvers and other type-aware form libraries.
12
+ *
10
13
  * @author Ong Hoe Yuan
11
- * @version 0.2.2
14
+ * @version 0.2.5
12
15
  */
13
16
 
14
17
  /**
@@ -26,37 +29,40 @@ type OneOfMessages = {
26
29
  * Configuration options for oneOf validation
27
30
  *
28
31
  * @template IsRequired - Whether the field is required (affects return type)
29
- * @template T - The type of allowed values
32
+ * @template V - The tuple type of allowed values, preserving literal types
30
33
  *
31
34
  * @interface OneOfOptions
32
- * @property {T[]} values - Array of allowed values
33
- * @property {T | null} [defaultValue] - Default value when input is empty
35
+ * @property {V} values - Array of allowed values
36
+ * @property {V[number] | null} [defaultValue] - Default value when input is empty
34
37
  * @property {boolean} [caseSensitive=true] - Whether string matching is case-sensitive
35
38
  * @property {Function} [transform] - Custom transformation function applied after validation
36
39
  * @property {Record<Locale, OneOfMessages>} [i18n] - Custom error messages for different locales
37
40
  */
38
- type OneOfOptions<IsRequired extends boolean = true, T extends string | number = string | number> = {
39
- values: T[];
40
- defaultValue?: IsRequired extends true ? T : T | null;
41
+ type OneOfOptions<IsRequired extends boolean = true, V extends readonly (string | number)[] = readonly (string | number)[]> = {
42
+ values: V;
43
+ defaultValue?: IsRequired extends true ? V[number] : V[number] | null;
41
44
  caseSensitive?: boolean;
42
- transform?: (value: T) => T;
45
+ transform?: (value: V[number]) => V[number];
43
46
  i18n?: Partial<Record<Locale, Partial<OneOfMessages>>>;
44
47
  };
45
48
  /**
46
49
  * Type alias for oneOf validation schema based on required flag
47
50
  *
48
51
  * @template IsRequired - Whether the field is required
49
- * @template T - The type of allowed values
52
+ * @template V - The tuple type of allowed values
50
53
  */
51
- type OneOfSchema<IsRequired extends boolean, T> = IsRequired extends true ? ZodType<T> : ZodType<T | null>;
54
+ type OneOfSchema<IsRequired extends boolean, V extends readonly (string | number)[]> = IsRequired extends true ? ZodType<V[number], V[number] | "" | null | undefined> : ZodType<V[number] | null, V[number] | "" | null | undefined>;
52
55
  /**
53
56
  * Creates a Zod schema for single-select validation that restricts values to a predefined set
54
57
  *
58
+ * Uses z.enum() internally to preserve both z.input and z.output literal types,
59
+ * ensuring compatibility with React Hook Form and other type-aware form libraries.
60
+ *
55
61
  * @template IsRequired - Whether the field is required (affects return type)
56
- * @template T - The type of allowed values (string | number)
62
+ * @template V - The tuple type of allowed values (inferred via const type parameter)
57
63
  * @param {IsRequired} [required=false] - Whether the field is required
58
- * @param {OneOfOptions<IsRequired, T>} options - Configuration options (values is required)
59
- * @returns {OneOfSchema<IsRequired, T>} Zod schema for oneOf validation
64
+ * @param {OneOfOptions<IsRequired, V>} options - Configuration options (values is required)
65
+ * @returns {OneOfSchema<IsRequired, V>} Zod schema for oneOf validation
60
66
  *
61
67
  * @example
62
68
  * ```typescript
@@ -65,7 +71,7 @@ type OneOfSchema<IsRequired extends boolean, T> = IsRequired extends true ? ZodT
65
71
  * roleSchema.parse("admin") // ✓ "admin"
66
72
  * roleSchema.parse(null) // ✓ null
67
73
  *
68
- * // Required
74
+ * // Required — z.input and z.output are both "active" | "inactive" | "pending"
69
75
  * const statusSchema = oneOf(true, { values: ["active", "inactive", "pending"] })
70
76
  * statusSchema.parse("active") // ✓ "active"
71
77
  * statusSchema.parse(null) // ✗ Required
@@ -99,6 +105,6 @@ type OneOfSchema<IsRequired extends boolean, T> = IsRequired extends true ? ZodT
99
105
  * sizeSchema.parse("m") // ✓ "M"
100
106
  * ```
101
107
  */
102
- declare function oneOf<IsRequired extends boolean = false, T extends string | number = string | number>(required?: IsRequired, options?: Omit<OneOfOptions<IsRequired, T>, "required">): OneOfSchema<IsRequired, T>;
108
+ declare function oneOf<IsRequired extends boolean = false, const V extends readonly (string | number)[] = readonly (string | number)[]>(required?: IsRequired, options?: Omit<OneOfOptions<IsRequired, V>, "required">): OneOfSchema<IsRequired, V>;
103
109
 
104
110
  export { type OneOfMessages, type OneOfOptions, type OneOfSchema, oneOf };