KScript docs
EN

Types and validation

Text with a meaning of its own

Type What it holds Tidied Methods
email An address such as joan@example.com. Spaces are trimmed and the domain is lowercased. .local, .domain
handle @ followed by letters, digits, _, . or -, up to 30 of them. Lowercased. .name (without the @)
uuid A UUID, versions 1 to 8, plus the nil and max UUIDs. Lowercased to 8-4-4-4-12. urn:uuid:, braces and 32 hex digits are read too. .version, .timestamp
regex A regular expression with flags i, m, s and x. ->test(), ->match(), ->match_all(), ->replace(), ->split(), .pattern, .flags
password A secret. It never prints. ->hash(), ->verify(), ->needs_rehash(), ->strength(), ->is_strong(), ->reveal()

A native value is still text, and it compares equal to the same text once tidied. An email compares with its domain lowercased. It keeps the string methods that make sense for it, and the editor only suggests those:

Type String methods it keeps
email contains, starts_with, ends_with, index_of, count, lower, upper, slice, split, replace, matches, truncate, pad, escape, url_encode, markdown_escape
handle contains, starts_with, ends_with, count, upper, slice, matches, truncate, pad, escape, url_encode, markdown_escape
uuid contains, starts_with, ends_with, upper, slice, split, pad, escape, url_encode, markdown_escape
regex contains, starts_with, ends_with, count, escape, markdown_escape
url contains, starts_with, ends_with, index_of, count, split, replace, slice, matches, truncate, pad, escape, url_encode, markdown_escape
domain contains, starts_with, ends_with, index_of, count, split, upper, slice, matches, truncate, pad, escape, url_encode, markdown_escape
ip contains, starts_with, ends_with, index_of, count, split, slice, matches, truncate, pad, escape, url_encode, markdown_escape
cidr contains, starts_with, ends_with, index_of, count, split, slice, matches, truncate, pad, escape, url_encode, markdown_escape
mac contains, starts_with, ends_with, index_of, count, split, upper, slice, matches, truncate, pad, escape, url_encode, markdown_escape
slug contains, starts_with, ends_with, index_of, count, split, upper, replace, slice, matches, truncate, pad, escape, url_encode, markdown_escape
file_path contains, starts_with, ends_with, index_of, count, split, lower, upper, replace, slice, matches, truncate, pad, escape, url_encode, markdown_escape
slash_path contains, starts_with, ends_with, index_of, count, split, lower, upper, replace, slice, matches, truncate, pad, escape, url_encode, markdown_escape
semver contains, starts_with, ends_with, index_of, count, split, slice, matches, truncate, pad, escape, url_encode, markdown_escape
mime contains, starts_with, ends_with, index_of, count, split, slice, matches, truncate, pad, escape, url_encode, markdown_escape
hex contains, starts_with, ends_with, index_of, count, upper, slice, matches, truncate, pad, escape, url_encode, markdown_escape
base64 contains, starts_with, ends_with, index_of, count, slice, matches, truncate, pad, escape, url_encode, markdown_escape
jwt contains, starts_with, ends_with, index_of, count, slice, matches, truncate, pad, escape, url_encode, markdown_escape
json contains, starts_with, ends_with, index_of, count, slice, matches, truncate, pad, escape, url_encode, markdown_escape
csv contains, starts_with, ends_with, index_of, count, slice, matches, truncate, pad, escape, url_encode, markdown_escape
phone contains, starts_with, ends_with, index_of, count, slice, matches, truncate, pad, escape, url_encode, markdown_escape
country contains, starts_with, ends_with, index_of, count, slice, matches, truncate, pad, escape, url_encode, markdown_escape
currency contains, starts_with, ends_with, index_of, count, slice, matches, truncate, pad, escape, url_encode, markdown_escape
locale contains, starts_with, ends_with, index_of, count, split, slice, matches, truncate, pad, escape, url_encode, markdown_escape
timezone contains, starts_with, ends_with, index_of, count, split, slice, matches, truncate, pad, escape, url_encode, markdown_escape
duration contains, starts_with, ends_with, index_of, count, slice, matches, truncate, pad, escape, url_encode, markdown_escape
date_format contains, starts_with, ends_with, index_of, count, slice, matches, truncate, pad, escape, url_encode, markdown_escape
cron contains, starts_with, ends_with, index_of, count, slice, matches, truncate, pad, escape, url_encode, markdown_escape
color contains, starts_with, ends_with, index_of, count, upper, slice, matches, truncate, pad, escape, url_encode, markdown_escape
iban contains, starts_with, ends_with, index_of, count, slice, matches, truncate, pad, escape, url_encode, markdown_escape

A port, card or password keeps none. The rest, such as ->to_date() on a uuid, are reported in the editor and fail when the script runs. To use one on the text anyway, call ->to_string() first: $id->to_string()->to_date().

<let name="email" type="email" value="' Joan.Smith@Example.COM '" />
<echo>{ $email } { $email.local } at { $email.domain }</echo>
<echo>{ $email == 'Joan.Smith@EXAMPLE.com' }</echo>

<let name="who" type="handle" value="@Joan_S" />
<echo>{ $who } { $who.name }</echo>

<let name="id" value="@uuid()" />
<let name="key" value="@uuid(v: 8, name: 'joan@example.com')" />
<echo>{ $id.version } { $key.version } { $key == @uuid(v: 8, name: 'joan@example.com') }</echo>
$email: email = ' Joan.Smith@Example.COM '
echo "{ $email } { $email.local } at { $email.domain }"
echo "{ $email == 'Joan.Smith@EXAMPLE.com' }"

$who: handle = @Joan_S
echo "{ $who } { $who.name }"

$id = @uuid()
$key = @uuid(v: 8, name: 'joan@example.com')
echo "{ $id.version } { $key.version } { $key == @uuid(v: 8, name: 'joan@example.com') }"

To check whether text is a UUID, use $text->is_a(uuid). Use $text as uuid to convert it and report invalid text as an error.

@uuid() makes a random version 4 UUID. @uuid(v: 7) makes a time-ordered version 7 UUID, the usual choice for database keys because new ones sort after old ones; its .timestamp is the time it was made. @uuid(v: 8) makes a version 8 UUID that is time-ordered too. With name:, it is the same UUID every time for that name, and namespace: keeps names from different places apart.

A value of the wrong form fails where it is made, with a Shape error that says why:

Text
$email doesn't fit email: 'joan@' isn't an email: there's nothing after the @.

The editor reports the same problem at the value as you type (KS5012).