Skip to content

validation ​

Validators and format helpers for constrained types. Pair a validator with @validate(...) to check values at runtime, spread the pre-baked @jsonSchema(...) format fragments into your own schemas, or reach for the ready-made type aliases (Email, URLString, UUIDString, …) that combine both. Each validator takes a value and returns a Result.

Single-argument validators plug straight in. Parameterized ones bind their config via partial application:

ts
import { isEmail, min, max } from "std::validation"

@validate(isEmail)
type Email = string

@validate(min.partial(n: 0), max.partial(n: 150))
type Age = number

Validators only run on types annotated with ! (the bang operator). See the type validation guide for full details.

Types ​

Email ​

An email-shaped string with a name, an @ sign, and a dotted domain.

ts
/** An email-shaped string with a name, an @ sign, and a dotted domain. */
@validate(isEmail)
@jsonSchema({
  ...emailFormat
})
export type Email = string

Validators: isEmail

JSON Schema metadata:

agency
{
  ...emailFormat
}

(source)

URLString ​

A string with an http:// or https:// prefix and no whitespace.

ts
/** A string with an http:// or https:// prefix and no whitespace. */
@validate(isUrl)
@jsonSchema({
  ...urlFormat
})
export type URLString = string

Validators: isUrl

JSON Schema metadata:

agency
{
  ...urlFormat
}

(source)

UUIDString ​

A UUID in 8-4-4-4-12 hexadecimal form.

ts
/** A UUID in 8-4-4-4-12 hexadecimal form. */
@validate(isUuid)
@jsonSchema({
  ...uuidFormat
})
export type UUIDString = string

Validators: isUuid

JSON Schema metadata:

agency
{
  ...uuidFormat
}

(source)

NumberInRange ​

A number that must lie within the inclusive range [low, high]. Use as NumberInRange(0, 100) etc. Both bounds substitute into the @validate(...) and @jsonSchema(...) tags at the use site.

ts
/** A number that must lie within the inclusive range `[low, high]`.
    Use as `NumberInRange(0, 100)` etc. Both bounds substitute into the
    `@validate(...)` and `@jsonSchema(...)` tags at the use site. */
@validate(min.partial(n: low), max.partial(n: high))
@jsonSchema({
  minimum: low,
  maximum: high
})
export type NumberInRange(low: number, high: number) = number

Validators: min.partial(n: low), max.partial(n: high)

JSON Schema metadata:

agency
{
  minimum: low,
  maximum: high
}

(source)

StringWithLength ​

A string whose length is in the inclusive range [min, max].

ts
/** A string whose length is in the inclusive range `[min, max]`. */
@validate(minLength.partial(n: min), maxLength.partial(n: max))
@jsonSchema({
  minLength: min,
  maxLength: max
})
export type StringWithLength(min: number, max: number) = string

Validators: minLength.partial(n: min), maxLength.partial(n: max)

JSON Schema metadata:

agency
{
  minLength: min,
  maxLength: max
}

(source)

MatchesPattern ​

A string that matches the given regular expression pat.

ts
/** A string that matches the given regular expression `pat`. */
@validate(matches.partial(pattern: pat))
@jsonSchema({
  pattern: pat
})
export type MatchesPattern(pat: string) = string

Validators: matches.partial(pattern: pat)

JSON Schema metadata:

agency
{
  pattern: pat
}

(source)

BoundedArray ​

An array whose length is in the inclusive range [min, max].

ts
/** An array whose length is in the inclusive range `[min, max]`. */
@jsonSchema({
  minItems: min,
  maxItems: max
})
export type BoundedArray<T>(min: number, max: number) = T[]

JSON Schema metadata:

agency
{
  minItems: min,
  maxItems: max
}

(source)

Json ​

A value that round-trips through JSON unchanged: null, booleans, strings, finite numbers, arrays of Json, and plain objects of Json. Class instances (Date, Map, ...), functions, NaN/Infinity, and cyclic structures are rejected.

ts
/** A value that round-trips through JSON unchanged: null, booleans, strings,
    finite numbers, arrays of Json, and plain objects of Json. Class
    instances (Date, Map, ...), functions, NaN/Infinity, and cyclic
    structures are rejected. */
@validate(isJsonValue)
export type Json = any

Validators: isJsonValue

(source)

JsonObject ​

A plain JSON-serializable object: non-null, non-array, and every value reachable from it is Json.

ts
/** A plain JSON-serializable object: non-null, non-array, and every value
    reachable from it is Json. */
@validate(isJsonObject)
export type JsonObject = object

Validators: isJsonObject

(source)

Constants ​

emailFormat ​

JSON Schema email format annotation for a string.

ts
/** JSON Schema email format annotation for a string. */
export static const emailFormat = {
  format: "email"
}

(source)

urlFormat ​

{ format: "uri" } — RFC 3986 URI.

ts
/** `{ format: "uri" }` — RFC 3986 URI. */
export static const urlFormat = {
  format: "uri"
}

(source)

uuidFormat ​

{ format: "uuid" } — canonical 8-4-4-4-12 UUID.

ts
/** `{ format: "uuid" }` — canonical 8-4-4-4-12 UUID. */
export static const uuidFormat = {
  format: "uuid"
}

(source)

dateTimeFormat ​

{ format: "date-time" } — ISO 8601 date-time.

ts
/** `{ format: "date-time" }` — ISO 8601 date-time. */
export static const dateTimeFormat = {
  format: "date-time"
}

(source)

dateFormat ​

{ format: "date" } — ISO 8601 date (no time).

ts
/** `{ format: "date" }` — ISO 8601 date (no time). */
export static const dateFormat = {
  format: "date"
}

(source)

ipv4Format ​

{ format: "ipv4" } — dotted-quad IPv4 address.

ts
/** `{ format: "ipv4" }` — dotted-quad IPv4 address. */
export static const ipv4Format = {
  format: "ipv4"
}

(source)

ipv6Format ​

{ format: "ipv6" } — IPv6 address.

ts
/** `{ format: "ipv6" }` — IPv6 address. */
export static const ipv6Format = {
  format: "ipv6"
}

(source)

Functions ​

isEmail ​

ts
isEmail(value: string): Result

Check for an email-shaped string with a name, an @ sign, and a dotted domain.

Use for a basic input check. This does not verify that the mailbox exists or implement every email-address syntax rule.

Parameters:

NameTypeDefault
valuestring

Returns: Result

(source)

isUrl ​

ts
isUrl(value: string): Result

Check for an http:// or https:// prefix followed by non-whitespace text.

Use for a basic URL check. This does not validate the hostname or fetch the URL.

Parameters:

NameTypeDefault
valuestring

Returns: Result

(source)

isUuid ​

ts
isUuid(value: string): Result

Returns success if value is a canonical UUID string (8-4-4-4-12 hex), failure otherwise.

Parameters:

NameTypeDefault
valuestring

Returns: Result

(source)

isInt ​

ts
isInt(value: number): Result

Returns success if value is an integer (no fractional component), failure otherwise.

Parameters:

NameTypeDefault
valuenumber

Returns: Result

(source)

isPositive ​

ts
isPositive(value: number): Result

Returns success if value > 0, failure otherwise.

Parameters:

NameTypeDefault
valuenumber

Returns: Result

(source)

isNegative ​

ts
isNegative(value: number): Result

Returns success if value < 0, failure otherwise.

Parameters:

NameTypeDefault
valuenumber

Returns: Result

(source)

min ​

ts
min(n: number, value: number): Result

Returns success if value >= n, failure otherwise.

Bind n with partial application before use in @validate(...), e.g. @validate(min.partial(n: 0)).

Parameters:

NameTypeDefaultDescription
nnumberThe inclusive minimum allowed value.
valuenumber

Returns: Result

(source)

max ​

ts
max(n: number, value: number): Result

Returns success if value <= n, failure otherwise.

Bind n with partial application before use in @validate(...), e.g. @validate(max.partial(n: 150)).

Parameters:

NameTypeDefaultDescription
nnumberThe inclusive maximum allowed value.
valuenumber

Returns: Result

(source)

minLength ​

ts
minLength(n: number, value: string): Result

Returns success if value.length >= n, failure otherwise.

Bind n with partial application before use in @validate(...), e.g. @validate(minLength.partial(n: 3)).

Parameters:

NameTypeDefaultDescription
nnumberThe inclusive minimum allowed length.
valuestring

Returns: Result

(source)

maxLength ​

ts
maxLength(n: number, value: string): Result

Returns success if value.length <= n, failure otherwise.

Bind n with partial application before use in @validate(...), e.g. @validate(maxLength.partial(n: 80)).

Parameters:

NameTypeDefaultDescription
nnumberThe inclusive maximum allowed length.
valuestring

Returns: Result

(source)

matches ​

ts
matches(pattern: string, value: string): Result

Returns success if value matches the regular expression pattern, failure otherwise.

Bind pattern with partial application before use in @validate(...), e.g. @validate(matches.partial(pattern: "^[A-Z]")).

Parameters:

NameTypeDefaultDescription
patternstringThe regular expression source to match against.
valuestring

Returns: Result

(source)

isJsonValue ​

ts
isJsonValue(value: any): Result

Returns success if value is a plain JSON tree that round-trips through JSON serialization unchanged, failure (naming the offending path) otherwise.

Parameters:

NameTypeDefault
valueany

Returns: Result

(source)

isJsonObject ​

ts
isJsonObject(value: any): Result

Returns success if value is a plain JSON-serializable object (non-null, non-array), failure otherwise.

Parameters:

NameTypeDefault
valueany

Returns: Result

(source)