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

A syntactically valid email address.

ts
/** A syntactically valid email address. */
@validate(isEmail)
@jsonSchema({
  ...emailFormat
})
export type Email = string

Validators: isEmail

JSON Schema metadata:

agency
{
  ...emailFormat
}

(source)

URLString

An http:// or https:// URL. (Named URLString so it does not shadow JavaScript's global URL constructor.)

ts
/** An http:// or https:// URL. (Named `URLString` so it does not shadow
    JavaScript's global `URL` constructor.) */
@validate(isUrl)
@jsonSchema({
  ...urlFormat
})
export type URLString = string

Validators: isUrl

JSON Schema metadata:

agency
{
  ...urlFormat
}

(source)

UUIDString

A canonical 8-4-4-4-12 hex UUID string. (Named UUIDString for symmetry with URLString.)

ts
/** A canonical 8-4-4-4-12 hex UUID string. (Named `UUIDString` for symmetry
    with `URLString`.) */
@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)

Constants

emailFormat

ts
export static const emailFormat = {
  format: "email"
}

(source)

urlFormat

ts
export static const urlFormat = {
  format: "uri"
}

(source)

uuidFormat

ts
export static const uuidFormat = {
  format: "uuid"
}

(source)

dateTimeFormat

ts
export static const dateTimeFormat = {
  format: "date-time"
}

(source)

dateFormat

ts
export static const dateFormat = {
  format: "date"
}

(source)

ipv4Format

ts
export static const ipv4Format = {
  format: "ipv4"
}

(source)

ipv6Format

ts
export static const ipv6Format = {
  format: "ipv6"
}

(source)

Functions

isEmail

ts
isEmail(value: string): Result

Returns success if value is a syntactically valid email address, failure otherwise.

@param value - The string to check.

Parameters:

NameTypeDefault
valuestring

Returns: Result

(source)

isUrl

ts
isUrl(value: string): Result

Returns success if value is an http:// or https:// URL, failure otherwise.

@param value - The string to check.

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.

@param value - The string to check.

Parameters:

NameTypeDefault
valuestring

Returns: Result

(source)

isInt

ts
isInt(value: number): Result

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

@param value - The number to check.

Parameters:

NameTypeDefault
valuenumber

Returns: Result

(source)

isPositive

ts
isPositive(value: number): Result

Returns success if value > 0, failure otherwise.

@param value - The number to check.

Parameters:

NameTypeDefault
valuenumber

Returns: Result

(source)

isNegative

ts
isNegative(value: number): Result

Returns success if value < 0, failure otherwise.

@param value - The number to check.

Parameters:

NameTypeDefault
valuenumber

Returns: Result

(source)

min

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

Returns success if value >= n, failure otherwise.

@param n - The inclusive minimum allowed value. @param value - The number to check.

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

Parameters:

NameTypeDefault
nnumber
valuenumber

Returns: Result

(source)

max

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

Returns success if value <= n, failure otherwise.

@param n - The inclusive maximum allowed value. @param value - The number to check.

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

Parameters:

NameTypeDefault
nnumber
valuenumber

Returns: Result

(source)

minLength

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

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

@param n - The inclusive minimum allowed length. @param value - The string to check.

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

Parameters:

NameTypeDefault
nnumber
valuestring

Returns: Result

(source)

maxLength

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

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

@param n - The inclusive maximum allowed length. @param value - The string to check.

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

Parameters:

NameTypeDefault
nnumber
valuestring

Returns: Result

(source)

matches

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

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

@param pattern - The regular expression source to match against. @param value - The string to check.

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

Parameters:

NameTypeDefault
patternstring
valuestring

Returns: Result

(source)