semver()
Create a strict semver validator that validates semantic version strings.
semver()
Creates a validator that accepts strict semver strings.
Signature
function semver(): SemverValidator
Accepted Values
Accepts semantic version strings in X.Y.Z format. No v prefix, no ranges (^, ~).
Supports optional pre-release tags and build metadata.
| Input | Result |
|---|---|
"1.0.0" | ✅ "1.0.0" |
"0.0.1" | ✅ "0.0.1" |
"999.999.999" | ✅ "999.999.999" |
"1.2.3-alpha" | ✅ "1.2.3-alpha" |
"1.2.3-alpha.1" | ✅ "1.2.3-alpha.1" |
"1.2.3+build.1" | ✅ "1.2.3+build.1" |
"1.2.3-rc.1+build.2" | ✅ "1.2.3-rc.1+build.2" |
"v1.0.0" | ❌ Invalid — v prefix not allowed |
"1.0" | ❌ Invalid — must have three parts |
"^1.0.0" | ❌ Invalid — range not allowed |
"abc" | ❌ Type error — not a semver string |
Refinements
semver() has no type-specific refinements. It supports only the chainable methods (.optional(), .default(), .describe(), .secret(), .validate()).
Examples
Basic semver
const env = defineEnv({
APP_VERSION: semver(),
})
// env.APP_VERSION: string
With defaults
const env = defineEnv({
API_VERSION: semver().default("1.0.0"),
})
// env.API_VERSION: string (default: "1.0.0")
Optional
const env = defineEnv({
SCHEMA_VERSION: semver().optional(),
})
// env.SCHEMA_VERSION: string | undefinedHow is this guide?
Last updated on Jun 25, 2026