Strings
String decoders for validating and parsing string values.
string
string: Decoder<string> (source)
Accepts and returns strings.
| Input | Result |
|---|---|
| "hello world" | |
| "🚀" | |
| "" | |
| 123 ^^^ Must be string | |
| true ^^^^ Must be string | |
| null ^^^^ Must be string |
nonEmptyString
nonEmptyString: Decoder<string> (source)
Like string, but will reject the empty string or strings containing only whitespace.
Note this is not the same as sized(string, { min: 1 }), which would still accept
whitespace-only strings.
| Input | Result |
|---|---|
| "hello world" | |
| "🚀" | |
| 123 ^^^ Must be string | |
| " " ^^^^ Must be non-empty string | |
| "" ^^ Must be non-empty string |
regex
function regex( pattern: RegExp, message: string ): Decoder<string> (source)
Accepts and returns strings that match the given regular expression.
| Input | Result |
|---|---|
| "90210" | |
| "1234" ^^^^^^ Must be ZIP code | |
| "123456" ^^^^^^^^ Must be ZIP code | |
| 12345 ^^^^^ Must be string |
startsWith
function startsWith( prefix: P ): Decoder<`${P}${string}`> (source)
Available since 2.5.
Accepts and returns strings that start with the given prefix.
| Input | Result |
|---|---|
| "abc" | |
| "abcdefg" | |
| 42 ^^ Must be string | |
| "ab" ^^^^ Must start with 'abc' | |
| "ABC" ^^^^^ Must start with 'abc' |
endsWith
function endsWith( suffix: S ): Decoder<`${string}${S}`> (source)
Available since 2.5.
Accepts and returns strings that end with the given suffix.
| Input | Result |
|---|---|
| "bar" | |
| "foobar" | |
| 42 ^^ Must be string | |
| "Bar" ^^^^^ Must end with 'bar' | |
| "bark" ^^^^^^ Must end with 'bar' |
decimal
decimal: Decoder<string> (source)
Accepts and returns strings with decimal digits only (base-10). To convert these to
numbers, use the numeric decoder.
| Input | Result |
|---|---|
| "42" | |
| "83401648364738" | |
| "" ^^ Must only contain digits | |
| "123abc" ^^^^^^^^ Must only contain digits | |
| "foo" ^^^^^ Must only contain digits | |
| 123 ^^^ Must be string |
hexadecimal
hexadecimal: Decoder<string> (source)
Accepts and returns strings with hexadecimal digits only (base-16).
| Input | Result |
|---|---|
| "0123456789ABCDEF" | |
| "deadbeef" | |
| "abcdefghijklm" ^^^^^^^^^^^^^^^ Must only contain hexadecimal digits | |
| "" ^^ Must only contain hexadecimal digits | |
| "1" |
numeric
numeric: Decoder<number> (source)
Accepts valid numerical strings (in base-10) and returns them as a number. To only accept
numerical strings and keep them as string values, use the decimal decoder.
| Input | Result |
|---|---|
| 42 | |
| 83401648364738 | |
| "" ^^ Must only contain digits | |
| "123abc" ^^^^^^^^ Must only contain digits | |
| "foo" ^^^^^ Must only contain digits | |
| 123 ^^^ Must be string |
email: Decoder<string> (source)
Accepts and returns strings that are syntactically valid email addresses. (This will not mean that the email address actually exist.)
| Input | Result |
|---|---|
| "alice@acme.org" | |
| "foo" ^^^^^ Must be email | |
| "@acme.org" ^^^^^^^^^^^ Must be email | |
| "alice @ acme.org" ^^^^^^^^^^^^^^^^^^ Must be email |
urlString
urlString: Decoder<string> (source)
Available since 2.9.
Accepts strings that are valid URLs, returns the value as a string.
| Input | Result |
|---|---|
| "https://nvie.com" | |
| "git+ssh://github.com/nvie/decoders.git" | |
| <URL> ^^^^^ Must be string | |
| "/search?q=foo" ^^^^^^^^^^^^^^^ Must be URL | |
| "javascript:alert(1)" ^^^^^^^^^^^^^^^^^^^^^ Must be URL |
url
url: Decoder<URL> (source)
Accepts either URL strings or URL instances, returns the value as a URL instance.
| Input | Result |
|---|---|
| URL { href: "https://nvie.com/" } | |
| URL { href: "git+ssh://github.com/nvie/decoders.git" } | |
| URL { href: "https://nvie.com/" } | |
| "/search?q=foo" ^^^^^^^^^^^^^^^ Must be URL | |
| "javascript:alert(1)" ^^^^^^^^^^^^^^^^^^^^^ Must be URL |
httpsUrl
httpsUrl: Decoder<URL> (source)
Accepts URL strings and URL instances, but only HTTPS ones. Returns the value as a URL instance.
| Input | Result |
|---|---|
| URL { href: "https://nvie.com/" } | |
| <URL> ^^^^^ Must be HTTPS URL | |
| <URL> ^^^^^ Must be HTTPS URL |
Tip! If you need to limit URLs to different protocols than HTTP, you can do as the
HTTPS decoder is implemented: by adding further conditions using a .refine() call.
import { url } from 'decoders';
const gitUrl: Decoder<URL> = url.refine(
(value) => value.protocol === 'git:',
'Must be a git:// URL',
);identifier
identifier: Decoder<string> (source)
Available since 2.4.
Accepts and returns strings that are valid identifiers in most programming languages.
| Input | Result |
|---|---|
| "x" | |
| "abc123" | |
| "_123" | |
| "a_b_c_1_2_3" | |
| "123xyz" ^^^^^^^^ Must be valid identifier | |
| "x-y" ^^^^^ Must be valid identifier | |
| "!@#$%^&*()=+" ^^^^^^^^^^^^^^ Must be valid identifier | |
| 42 ^^ Must be string |
nanoid
function nanoid( { size: number } ): Decoder<string> (source)
function nanoid( { min?: number; max?: number } ): Decoder<string> (source)
Available since 2.4.
Accepts and returns Nano ID string values. By
default, expects a standard 21-char nanoid, but you can optionally specify different size
constraints. It assumes the default nanoid alphabet. If you're using a custom alphabet,
use regex() instead.
| Input | Result |
|---|---|
| "1-QskICa3CaPGcKuYYTm1" | |
| "vA4mt7CUWnouU6jTGbMP_" | |
| "123E4567E89B12D3A456426614174000" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Too long, must be 21 chars | |
| "abcdefghijkl" ^^^^^^^^^^^^^^ Too short, must be 21 chars | |
| "$*&(#%*&(" ^^^^^^^^^^^ Must be nano ID |
uuid
uuid: Decoder<string> (source)
Accepts strings that are valid UUIDs (universally unique identifier).
| Input | Result |
|---|---|
| "123e4567-e89b-12d3-a456-426614174000" | |
| "123E4567-E89B-12D3-A456-426614174000" | |
| "123E4567E89B12D3A456426614174000" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Must be uuid | |
| "abcdefgh-ijkl-mnop-qrst-uvwxyz012345" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Must be uuid |
uuidv1
uuidv1: Decoder<string> (source)
Like uuid, but only accepts
UUIDv1
strings.
| Input | Result |
|---|---|
| "123e4567-e89b-12d3-a456-426614174000" | |
| "123e4567-e89b-42d3-a456-426614174000" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Must be uuidv1 |
uuidv4
uuidv4: Decoder<string> (source)
Like uuid, but only accepts
UUIDv4
strings.
| Input | Result |
|---|---|
| "123e4567-e89b-42d3-a456-426614174000" | |
| "123e4567-e89b-12d3-a456-426614174000" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Must be uuidv4 |
Limiting string size
function sized( decoder: Decoder<string>, limit: { size: number } ): Decoder<string> (source)
function sized( decoder: Decoder<string>, limit: { min?: number; max?: number } ): Decoder<string> (source)
Available since 2.9.
Takes any string decoder and rejects values that don't match the given size constraints. Also works with arrays and sets.
SizeOptions accepts min, max, or size (shorthand for exact length).
| Input | Result |
|---|---|
| "hello" | |
| "hi" | |
| "x" ^^^ Too short, must be at least 2 chars | |
| "this string is too long" ^^^^^^^^^^^^^^^^^^^^^^^^^ Too long, must be at most 10 chars | |
| 42 ^^ Must be string |