Validating UUIDs with uuid
validate tells you whether a string is a UUID at all, and version tells you which RFC version it is.
How it works
- Check strings with validate(). The loop runs
validateon each sample and printstrueorfalse. Only valid strings are passed on toversion. [1] - Accept one version only.
uuidValidateV4combines both calls, so a valid v1 UUID is rejected while v4 UUIDs pass. [1] - NIL, MAX and errors. The NIL and MAX constants are valid and report versions 0 and 15. Calling
versionon a string that is not a UUID throws aTypeError. [1]
Run and edit it
It runs in your browser, in a sandboxed frame that cannot see this site; the packages load from esm.sh only after you press Run.
import { validate, version, NIL, MAX, v4, v7 } from 'uuid'
const samples = {
'not a UUID': 'not a UUID',
'v1 sample': 'd9428888-122b-11e1-b85c-61cd3cbb3210',
'v4 sample': '109156be-c4fb-41ea-b1b4-efe1671c5836',
'fresh v7': v7(),
'NIL': NIL,
'MAX': MAX,
}
// validate() first, then version() only on valid strings
for (const [label, str] of Object.entries(samples)) {
const ok = validate(str)
console.log(`${label}: valid=${ok}` + (ok ? `, version=${version(str)}` : ''))
}
// Per-version check: only accept v4 UUIDs
function uuidValidateV4(uuid) {
return validate(uuid) && version(uuid) === 4
}
console.log('v4 only -> v4 sample:', uuidValidateV4(samples['v4 sample']))
console.log('v4 only -> v1 sample:', uuidValidateV4(samples['v1 sample']))
console.log('v4 only -> new v4():', uuidValidateV4(v4()))
// version() throws on invalid input
try {
version('nope')
} catch (err) {
console.log('version("nope") throws:', err.name)
}
Add your own strings to the samples object and see which ones pass. Code written by AgentGid [1].
Add your own strings to the samples object and see which ones pass. Code written by AgentGid [1].Checked Oct 12, 2026: it starts without errors in Chromium with uuid 14.0.3. Needs WebGL.
More uuid examples
Random UUIDs with uuid v4 Time-based UUIDs with uuid v7 Name-based UUIDs with uuid v5 Converting UUIDs to bytes with uuid
Sources
Explanations written by AgentGid from the official docs; the numbers in brackets point here. The code marked "written by AgentGid" is ours and free to copy.