📈 Announcing ArkType 2.1 📈

Primitives

string

keywords

The following keywords can be referenced in any definition, e.g.:

const  = ("string.email")

const  = ({
	data: "string.json.parse",
	ids: "string.uuid.v4[]"
})
AliasDescription
stringa string
string.alphaonly letters
string.alphanumericonly letters and digits 0-9
string.base64base64-encoded
string.base64.urlbase64url-encoded
string.capitalizea morph from a string to capitalized
string.capitalize.preformattedcapitalized
string.creditCarda credit card number and a credit card number
string.datea string and a parsable date
string.date.epochan integer string representing a safe Unix timestamp
string.date.epoch.parsea morph from an integer string representing a safe Unix timestamp to a Date
string.date.isoan ISO 8601 (YYYY-MM-DDTHH:mm:ss.sssZ) date
string.date.iso.parsea morph from an ISO 8601 (YYYY-MM-DDTHH:mm:ss.sssZ) date to a Date
string.date.parsea morph from a string and a parsable date to a Date
string.digitsonly digits 0-9
string.emailan email address
string.hexhex characters only
string.integera well-formed integer string
string.integer.parsea morph from a well-formed integer string to an integer
string.ipan IP address
string.ip.v4an IPv4 address
string.ip.v6an IPv6 address
string.jsona JSON string
string.json.parsesafe JSON string parser
string.lowera morph from a string to only lowercase letters
string.lower.preformattedonly lowercase letters
string.normalizea morph from a string to NFC-normalized unicode
string.normalize.NFCa morph from a string to NFC-normalized unicode
string.normalize.NFC.preformattedNFC-normalized unicode
string.normalize.NFDa morph from a string to NFD-normalized unicode
string.normalize.NFD.preformattedNFD-normalized unicode
string.normalize.NFKCa morph from a string to NFKC-normalized unicode
string.normalize.NFKC.preformattedNFKC-normalized unicode
string.normalize.NFKDa morph from a string to NFKD-normalized unicode
string.normalize.NFKD.preformattedNFKD-normalized unicode
string.numerica well-formed numeric string
string.numeric.parsea morph from a well-formed numeric string to a number
string.semvera semantic version (see https://semver.org/)
string.trima morph from a string to trimmed
string.trim.preformattedtrimmed
string.uppera morph from a string to only uppercase letters
string.upper.preformattedonly uppercase letters
string.urla string and a URL string
string.url.parsea morph from a string and a URL string to a URL instance
string.uuida UUID
string.uuid.v1a UUIDv1
string.uuid.v2a UUIDv2
string.uuid.v3a UUIDv3
string.uuid.v4a UUIDv4
string.uuid.v5a UUIDv5
string.uuid.v6a UUIDv6
string.uuid.v7a UUIDv7
string.uuid.v8a UUIDv8

literals

const  = ({
	singleQuoted: "'typescript'",
	doubleQuoted: '"arktype"'
})

patterns

Regex literals specify an unanchored regular expression that an input string must match.

They can either be string-embedded or refer directly to a RegExp instance.

const  = ({
	stringEmbedded: "/^a.*z$/",
	regexLiteral: /^a.*z$/
})

lengths

Constrain a string with an inclusive or exclusive min or max length.

const  = ({
	nonEmpty: "string > 0",
	atLeastLength3: "string.alphanumeric >= 3",
	lessThanLength10: "string < 10",
	atMostLength5: "string <= 5"
})

Range expressions allow you to specify both a min and max length and use the same syntax for exclusivity.

const  = ({
	nonEmptyAtMostLength10: "0 < string <= 10",
	integerStringWith2To5Digits: "2 <= string.integer < 6"
})

number

keywords

The following keywords can be referenced in any definition, e.g.:

const  = ({
	createdAt: "number.epoch",
	age: "number.integer >= 0"
})
AliasDescription
numbera number
number.InfinityInfinity
number.NaNNaN
number.NegativeInfinity-Infinity
number.epochan integer representing a safe Unix timestamp
number.integeran integer
number.safeat least -9007199254740991 and at most 9007199254740991

literals

const  = ({
	number: "1337"
})

ranges

Constrain a number with an inclusive or exclusive min or max.

const  = ({
	positive: "number > 0",
	atLeast3: "number.integer >= 3",
	lessThanPi: "number < 3.14159",
	atMost5: "number <= 5"
})

Range expressions allow you to specify both a min and max and use the same syntax for exclusivity.

const  = ({
	positiveAtMostE: "0 < number <= 2.71828",
	evenNumberAbsoluteValueLessThan50: "-50 < (number % 2) < 50"
})

divisors

Constrain a number to a multiple of the specified integer.

const  = ({
	key: "number % 2"
})

bigint

To allow any bigint value, use the "bigint" keyword.

const  = ({
	foo: "bigint"
})

literals

To require an exact bigint value in your type, you can use add the suffix n to a string-embedded number literal to make it a bigint.

const  = ({
	bigint: "1337n"
})

You may also use a unit expression to define bigint literals.

symbol

To allow any symbol value, use the "symbol" keyword.

const  = ({
	key: "symbol"
})

To reference a specific symbol in your definition, use a unit expression.

No special syntax is required to define symbolic properties like { [mySymbol]: "string" }. For more information and examples of how to combine symbolic keys with other syntax like optionality, see properties.

boolean

To allow true or false, use the "boolean" keyword.

const  = ({
	key: "boolean"
})

literals

To require a specific boolean value, use the corresponding literal.

const  = ({
	a: "true",
	b: "false",
	// equivalent to the "boolean" keyword
	c: "true | false"
})

null

The "null" keyword can be used to allow the exact value null, generally as part of a union.

const  = ({
	foo: "number | null"
})

undefined

The "undefined" keyword can be used to allow the exact value undefined, generally as part of a union.

const  = ({
	requiredKey: "number | undefined",
	"optionalKey?": "number | undefined"
})

Allowing undefined as a value does not make the key optional!

In TypeScript, a required property that allows undefined must still be present for the type to be satisfied.

The same is true in ArkType.

See an example
const  = ({
	key: "number | undefined"
})

// valid data
const  = myObj({ key: undefined })

// Error: name must be a number or undefined (was missing)
const  = myObj({})

On this page