colinhacks/zod · error · Error
Unrecognized hash format: ${format}
Error message
Unrecognized hash format: ${format} What it means
Thrown by the mini `hash()` schema factory (mini/schemas.ts:535) when the composed `${alg}_${enc}` format string (e.g. `sha256_base64`) does not resolve to a known entry in `core.regexes`. The supported algorithms are md5/sha1/sha256/sha384/sha512 and encodings hex/base64/base64url; any other combination — or any of those that lacks a prebuilt regex — fails immediately.
Source
Thrown at packages/zod/src/v4/mini/schemas.ts:535
}
// @__NO_SIDE_EFFECTS__
export function hex(_params?: string | core.$ZodStringFormatParams): ZodMiniCustomStringFormat<"hex"> {
return core._stringFormat(ZodMiniCustomStringFormat, "hex", core.regexes.hex, _params) as any;
}
// @__NO_SIDE_EFFECTS__
export function hash<Alg extends util.HashAlgorithm, Enc extends util.HashEncoding = "hex">(
alg: Alg,
params?: {
enc?: Enc;
} & core.$ZodStringFormatParams
): ZodMiniCustomStringFormat<`${Alg}_${Enc}`> {
const enc = params?.enc ?? "hex";
const format = `${alg}_${enc}` as const;
const regex = core.regexes[format as keyof typeof core.regexes] as RegExp;
// check for unrecognized format
if (!regex) throw new Error(`Unrecognized hash format: ${format}`);
return core._stringFormat(ZodMiniCustomStringFormat, format, regex, params) as any;
}
// ZodMiniNumber
interface _ZodMiniNumber<T extends core.$ZodNumberInternals<unknown> = core.$ZodNumberInternals<unknown>>
extends _ZodMiniType<T>,
core.$ZodNumber<T["input"]> {
_zod: T;
}
export interface ZodMiniNumber<Input = unknown>
extends _ZodMiniNumber<core.$ZodNumberInternals<Input>>,
core.$ZodNumber<Input> {}
export const ZodMiniNumber: core.$constructor<ZodMiniNumber> = /*@__PURE__*/ core.$constructor(
"ZodMiniNumber",
(inst, def) => {
core.$ZodNumber.init(inst, def);
ZodMiniType.init(inst, def);
}View on GitHub (pinned to 912f0f51b0)
Solutions
- Use one of the supported algorithms: md5, sha1, sha256, sha384, sha512.
- Use one of the supported encodings: hex, base64, base64url (default is hex).
- For unsupported hashes, fall back to a custom string format: `z.string().check(...)` with your own regex or use `z.iso`/custom format if available.
Example fix
// before
const H = z.hash('sha3', { enc: 'utf8' });
// after
const H = z.hash('sha256', { enc: 'hex' }); Defensive patterns
Strategy: validation
Validate before calling
const ALGS = ['md5','sha1','sha256','sha384','sha512'] as const;
const ENCS = ['hex','base64','base64url'] as const;
function isHashCombo(a: string, e: string): boolean {
return (ALGS as readonly string[]).includes(a) && (ENCS as readonly string[]).includes(e);
}
if (!isHashCombo(alg, enc)) throw new Error('unsupported hash combo');
const H = z.hash(alg, { enc }); Type guard
type Alg = 'md5'|'sha1'|'sha256'|'sha384'|'sha512';
type Enc = 'hex'|'base64'|'base64url';
function isAlg(x: string): x is Alg { return ['md5','sha1','sha256','sha384','sha512'].includes(x); }
function isEnc(x: string): x is Enc { return ['hex','base64','base64url'].includes(x); } Prevention
- Constrain algorithm/encoding inputs to the supported literal unions at the type level.
- For unsupported hashes (sha3, blake2b), build a custom string-format schema with your own regex.
When it happens
Trigger: Calling `z.hash('md5', { enc: 'utf8' })`, `z.hash('sha3', { enc: 'hex' })`, or any combination where `core.regexes` does not contain the `${alg}_${enc}` key. Also reachable if `params.enc` is set to an unsupported value.
Common situations: Asking for a hash algorithm not in the supported set (e.g. sha3-256, blake2b); asking for an encoding outside hex/base64/base64url; passing a typo'd algorithm string; version mismatch where a regex entry was removed.
Related errors
- Invalid hex string length
- Unrecognized hash format: ${format}
- Invalid UUID version: "${def.version}"
- Invalid discriminated union option at index "${def.options.i
- Invalid discriminated union option at index "${def.options.i
AI-assisted analysis of colinhacks/zod@912f0f51b0 (2026-08-03).
Data as JSON: /data/errors/75f46f7c36be1650.json.
Report an issue: GitHub.