truefit/formats
Every export of truefit/formats, with its signature and doc comment: base64, base64url, cuid, cuid2, date, datetime and 18 more, plus 1 type.
import { base64, base64url, cuid, … } from 'truefit/formats';Constants
base64
RFC 4648 §4: the base 64 alphabet (A to Z, a to z, 0 to 9, + and /) in groups of
four, the last padded with =. The empty string is the encoding of no bytes. No line breaks (§3.1)
and no missing padding (§3.2); the URL-safe alphabet of §5 is another format.
const base64: Readonly<{
name: "base64";
pattern: RegExp;
}>;base64url
RFC 4648 §5, base 64 with the URL and filename safe alphabet (- and _ for + and /), and
with no padding, as §3.2 allows where the length is known and as JWT (RFC 7515 §2) writes it. So
= is refused, and so is a length that leaves one character over, which encodes no whole byte.
const base64url: Readonly<{
name: "base64url";
pattern: RegExp;
}>;cuid
A cuid (version 1, deprecated by its author): c and then 24 lower-case letters and digits, 25 in all.
const cuid: Readonly<{
name: "cuid";
pattern: RegExp;
}>;cuid2
A cuid2: a lower-case letter, then lower-case letters and digits, from 2 to 32 in all (the library's own bounds; 24 is its default).
const cuid2: Readonly<{
name: "cuid2";
pattern: RegExp;
}>;date
RFC 3339 §5.6 full-date, with the restriction of §5.7 that the day is one the month has: the
29th of February only in a leap year of the Gregorian calendar.
const date: Readonly<{
name: "date";
pattern: RegExp;
}>;datetime
RFC 3339 §5.6 date-time: a full-date, T, a time with seconds and any fraction, and Z or a
numeric offset with its colon. T and Z may be lower case, as the grammar's note allows. A space
for the T, a missing offset and +0200 are not in the grammar.
const datetime: Readonly<{
name: "date-time";
pattern: RegExp;
}>;duration
RFC 3339 Appendix A duration, which is what JSON Schema's duration format names: P, then
years, months and days, a T with hours, minutes and seconds, or weeks alone; whole numbers, no
sign. The grammar is strict about order and about gaps: a minute may follow an hour, not skip it,
so PT1H1S and P1Y1D are refused, P1DT2H and PT36H accepted.
const duration: Readonly<{
name: "duration";
pattern: RegExp;
}>;The WHATWG HTML standard's "valid email address" (§4.10.5.1.5), which is the pattern a browser
holds <input type=email> to. It is a wilful violation of RFC 5322, as the standard says: no
quoted local part, no comment, no address literal. It asks for no dot in the domain, so a@b
passes, and it allows a dot anywhere in the local part.
const email: Readonly<{
name: "email";
pattern: RegExp;
}>;emoji
One or more emoji of Unicode's recommended set (UTS #51, RGI_Emoji), as the engine's own Unicode
tables have them: a flag, a family and a skin tone are each one. So "123" is refused, which a
test of single code points would let through, since a digit is an emoji component.
const emoji: Readonly<{
name: "emoji";
pattern: RegExp;
}>;hex
One or more of RFC 5234's HEXDIG (appendix B.1), in either case, as ABNF reads its own
letters: the digits of a number written in base 16, or of bytes written two digits each (RFC 4648
§8). No 0x in front, and not the empty string, which is no number.
const hex: Readonly<{
name: "hex";
pattern: RegExp;
}>;hostname
A host name as RFC 1123 §2.1 has it: RFC 952's labels of letters, digits and hyphens joined by
dots, a label neither starting nor ending with a hyphen, and a digit allowed first. A label is
at most 63 characters and the whole name at most 253 (RFC 1035 §2.3.4). One label is a host
name, so localhost passes. No trailing dot, no underscore, and no character outside ASCII: an
internationalised name is written in its xn-- form.
const hostname: Readonly<{
name: "hostname";
pattern: RegExp;
}>;httpUrl
The same, with the scheme http or https and a host: what may be offered as a link.
const httpUrl: Readonly<{
name: "http-url";
test: (text: string) => boolean;
}>;ipv4
RFC 3986 §3.2.2 IPv4address: four decimal octets from 0 to 255, with no leading zero.
const ipv4: Readonly<{
name: "ipv4";
pattern: RegExp;
}>;ipv6
RFC 3986 §3.2.2 IPv6address, which is the text form of RFC 4291 §2.2: eight groups of one to
four hex digits, one run of zero groups written ::, and the last two groups optionally as an
IPv4 address. No zone (%eth0), which is no part of the address, and no brackets.
const ipv6: Readonly<{
name: "ipv6";
pattern: RegExp;
}>;json
A string that is a JSON text (RFC 8259 §2), as JSON.parse reads one: any JSON value, white space around it allowed. A test, so emitWith hands it over.
const json: Readonly<{
name: "json";
test: (text: string) => boolean;
}>;jwt
const jwt: Readonly<{
name: "jwt";
test: (text: string) => boolean;
}>;localTime
RFC 3339 §5.6 partial-time: hh:mm:ss with any fraction, and no offset, which is ISO 8601's
local time in its extended format with seconds. It is what zod and valibot call an ISO time. No
leap second: with no offset there is no telling whether the minute had one.
const localTime: Readonly<{
name: "local-time";
pattern: RegExp;
}>;nanoid
A Nano ID of the library's defaults: 21 characters of its URL-safe alphabet, A–Z, a–z, 0–9, _ and -.
const nanoid: Readonly<{
name: "nanoid";
pattern: RegExp;
}>;regex
A string that is itself a regular expression, read as ECMA-262 reads one under the u flag,
which is how JSON Schema's regex format and its pattern keyword read them. So ^a+$ passes and
( does not; neither does \-, which only a pattern without the flag reads.
const regex: Readonly<{
name: "regex";
test: (text: string) => boolean;
}>;time
RFC 3339 §5.6 full-time: hours, minutes and seconds with any fraction, then Z or a numeric
offset with its colon. It is the time half of datetime, and what JSON Schema's time format
names. A time with no offset is localTime. A leap second is accepted where datetime accepts
it: 23:59:60 at Z or ±00:00.
const time: Readonly<{
name: "time";
pattern: RegExp;
}>;ulid
A ULID (github.com/ulid/spec): 26 characters of Crockford's base 32, which has no I, L, O or
U. Read in either case, as the spec says it is. The first is at most 7: 26 characters hold 130
bits, and a ULID is 128.
const ulid: Readonly<{
name: "ulid";
pattern: RegExp;
}>;url
The WHATWG URL standard: a string its parser reads with no base URL, so an absolute URL, and
that it does not have to cut first. Any scheme: mailto:, urn:, data: and javascript:
pass, as they do in zod, valibot and arktype; use httpUrl for a link. The parser is the
platform's (URL.canParse, in every Node this package supports and every current browser), so
what it forgives is forgiven here: a space in a path, a host that is not ASCII.
const url: Readonly<{
name: "url";
test: (text: string) => boolean;
}>;uuid
RFC 9562 §4: the 8-4-4-4-12 text form, in either case, with a version it defines (1 to 8) and
the variant it specifies (the nibble 8, 9, a or b). Also the Nil and the Max UUID (§5.9, §5.10),
which have neither. No braces, no urn:uuid: prefix.
const uuid: Readonly<{
name: "uuid";
pattern: RegExp;
}>;uuidVersion
A UUID of one version (RFC 9562 §4.2): the uuid format with that digit where the version stands,
and the RFC's variant. uuidVersion(4) refuses a version-1 UUID, and the Nil and Max UUIDs, which
have no version.
const uuidVersion: (version: UuidVersion) => Format;Types
UuidVersion
The UUID versions RFC 9562 defines.
type UuidVersion = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8;