String Types
The string type and its email and url shortcuts.
The string type validates text. It has two predefined shortcuts that are string with a built-in pattern: Email and URL.
date,time, anddatetimeare not string subtypes — they are their own types with their own values. See Date and Time.For how strings are written (open, quoted, raw), see Strings.
The string family
string
any text
email
string validated against an email pattern
url
string validated against a URL pattern
contact: email, site: url
---
~ a@b.com, 'https://example.com' # ✓
~ notanemail, 'https://example.com' # ✗ invalid-emailQuote values containing
:or spaces (URLs, times-of-day, "Last, First"). An unquotedhttps://x.comis misread because the open string ends at:.
TypeDef
A string MemberDef accepts only the options below. Any other key is invalid.
type
string
string, email, or url. First positional value.
default
string
Value used when the member is omitted. Second positional value.
choices
array of string
Restricts the value to a fixed set. Third positional value.
pattern
string
A regular expression the value must match.
flags
string
Regex flags for pattern (e.g. i).
len
int ≥ 0
Exact length, in Unicode code points.
minLen
int ≥ 0
Minimum length, in Unicode code points.
maxLen
int ≥ 0
Maximum length, in Unicode code points.
format
string
Presentation, write-only. Form used when writing: auto (default), regular, raw.
encloser
string
Presentation, write-only. Quote character used when writing: " (default) or '.
escapeLines
bool
Presentation, write-only. Whether to escape line breaks when writing.
optional
bool
If true, the member may be omitted. Shorthand: ? suffix.
null
bool
If true, the member may be null. Shorthand: * suffix.
lenprecedence. Whenlenis set,minLenandmaxLenare ignored.
Length is measured in Unicode code points — NOT bytes, and NOT UTF-16 code units.
"café"is 4 and"🙂"is 1, even though the first is 5 bytes in UTF-8 and the second is 2 UTF-16 units.This has to be stated because every language's default string length means something different, and three of the common answers are wrong here:
Code points are the only unit that is a property of the text rather than of an encoding or a runtime, and Internet Object is UTF-8 on the wire, where UTF-16 units have no meaning at all.
The failure is invisible in ordinary testing:
"café"measures 4 under all three interpretations, so a wrong implementation passes every ASCII and Latin-1 test and only diverges on characters outside the Basic Multilingual Plane. The conformance corpus probes it directly (validation/strings-constraints.io).
Language
Idiom
"🙂"
Python
len(s)
1
✅
Go
utf8.RuneCountInString(s)
1
✅
Rust
s.chars().count()
1
✅
Go
len(s)
4
✗ bytes
Rust
s.len()
4
✗ bytes
JavaScript
s.length
2
✗ UTF-16 units
Constraints
minLen / maxLen / len
pattern
A regular expression. Use a raw string (r'…') to avoid escaping backslashes.
choices
Quote choices that look like numbers or contain commas, e.g.
["19.02, 72.85"], so they are treated as strings.
Optional, nullable & defaults
valid text
the string
fails a declared constraint
mismatched-* error, named after the keyword — mismatched-min-len, mismatched-pattern, mismatched-choice, …
malformed for a sub-format
invalid-email / invalid-url — email and url are types, so a non-conforming value is malformed rather than out of bounds
N, nullable (*)
null
N, not nullable
forbidden-null error
omitted, default set
the default
omitted, optional (?)
absent
omitted, required
missing-value error
See Also
Last updated
Was this helpful?
