Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | 1x 1x 3x 3x 3x 1x 1x 1x 1x 1x 4x 1x 1x 1x 1x 1x | import { z } from 'zod'
/** 空文字を undefined に寄せる(任意入力フィールドに便利) */
export const optionalEmptyToUndef = () =>
// ❶ 先に '' を undefined に変換
z.literal('').transform(() => undefined).or(
// ❷ それ以外の string | undefined
z.string().optional(),
)
/**
* 必須の数値(coerce + int / positive は呼び出し側で付け外し可)
* Zod v3 の型に合わせて、ここでは「型エラー用メッセージ」だけ持たせる。
* 「必須です」のメッセージを出したい場合は、呼び出し側で .refine(...) で追加する前提。
*/
export function coerceIntRequired(msgs: { required: string; type: string }) {
// createParams は { message?: string; error?: string | ErrorMap } だけなので message を使う
return z.coerce.number({ message: msgs.type })
}
/** 長さ系の薄いラッパ(好みで使う) */
export const stringMax = (max: number, message: string) =>
z.string().max(max, message)
export const stringMaxOptional = (max: number, message: string) =>
z.string().max(max, message).optional()
/** 2桁の国コード(共通化しておくと楽) */
export const countryCode2 = (requiredMsg: string, lenMsg: string) =>
z
.string({ message: requiredMsg }) // ← required_error ではなく message を使う
.length(2, lenMsg)
.transform((s) => s.toUpperCase())
|