validateHexString
Checks whether a value is a raw hexadecimal string. This helper uses the isHexString type guard internally. A hex string is considered valid when it contains one or more hexadecimal digits (0–9, a–f, A–F) with no prefixes and no whitespace.
This validator does not accept prefixes such as 0x or #. It only verifies
the raw hex content without coercion.
It never throws and never mutates input. Use it when you need a strict hex string before normalization or further semantic validation.
Signature
function validateHexString(value: unknown): string | undefined
Parameters | | | | | | unknown | |
Returns string | undefined — One of: - The original input string if the value is a valid hex string. - undefined if the value is not a hex string. Behavior - Uses isHexString internally. - Only raw hex strings pass. - Rejects prefixes (0x, #), whitespace, and invalid characters. - Rejects empty strings. - Never mutates input. - Never throws. - Never coerces values (e.g., " abcd " is invalid). Examples validateHexString("deadbeef") // "deadbeef"
validateHexString("DEADBEEF") // "DEADBEEF"
validateHexString("0x1234") // undefined
validateHexString("12 34") // undefined
validateHexString(1234) // undefined
Notes - This validator does not normalize or lowercase hex strings. It returns the original input exactly as provided. - Because it depends on isHexString, values with whitespace, prefixes, or invalid characters automatically fail. - Suitable for validating identifiers, checksums, binary encodings, and any context requiring a guaranteed raw hexadecimal string. - The validator is pure, side-effect-free, and never throws.