Strings, URLs and UUIDs
Strings
p.string accepts JavaScript strings and returns StringType.
const Username = p.string
.trim()
.lowercase()
.min(3)
.max(32)
.regex(/^[a-z][a-z0-9]+$/);
Username.parse(" Ada01 "); // "ada01"Transforms run in declaration order before validators:
| Method | Result |
|---|---|
trim() |
Remove leading and trailing whitespace |
lowercase() |
Convert to lowercase |
uppercase() |
Convert to uppercase |
String-specific validators are:
| Method | Requirement |
|---|---|
isotime(options?) |
ISO time string |
regex(pattern, options?) |
Regular-expression match |
email(options?) |
Email address |
startsWith(prefix, options?) |
Required prefix |
endsWith(suffix, options?) |
Required suffix |
min(n, options?) |
At least n characters |
max(n, options?) |
At most n characters |
length(from, to, options?) |
Inclusive character range |
Cardinality bounds must be finite, nonnegative integers. Each validator accepts
a message string or { message }; the message may also be a function of the
parsed value.
const Password = p.string.min(12, "Use at least 12 characters");
const Slug = p.string.startsWith("/", { message: "Start with /" });Like other primitive schemas, strings also support check(), optional(),
default() and derive(). Strings are Storable with datatype string.
URLs
p.url accepts a URL instance. p.loose.url also accepts an absolute URL
string and returns a URL:
p.loose.url.parse("https://primate.run/docs");
// URL { ... }Invalid or relative strings fail validation. URLType is Storable with datatype
url.
UUIDs
p.uuid validates the standard hyphenated UUID shape. Use its methods when a
specific version is required:
const AnyUUID = p.uuid;
const LegacyId = p.uuid.v4();
const OrderedId = p.uuid.v7();The corresponding public types are UUIDType, UUIDV4Type and UUIDV7Type.
All three parse to strings and are Storable with distinct uuid, uuid_v4 and
uuid_v7 datatypes. Primate Stores use the datatype when generating keys; the
generic uuid form generates UUIDv7 keys. See Store identifiers.