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:
import { isEmail, min, max } from "std::validation"
@validate(isEmail)
type Email = string
@validate(min.partial(n: 0), max.partial(n: 150))
type Age = numberValidators only run on types annotated with ! (the bang operator). See the type validation guide for full details.
Types
Email
A syntactically valid email address.
/** A syntactically valid email address. */
@validate(isEmail)
@jsonSchema({
...emailFormat
})
export type Email = stringValidators: isEmail
JSON Schema metadata:
{
...emailFormat
}(source)
URLString
An http:// or https:// URL. (Named URLString so it does not shadow JavaScript's global URL constructor.)
/** An http:// or https:// URL. (Named `URLString` so it does not shadow
JavaScript's global `URL` constructor.) */
@validate(isUrl)
@jsonSchema({
...urlFormat
})
export type URLString = stringValidators: isUrl
JSON Schema metadata:
{
...urlFormat
}(source)
UUIDString
A canonical 8-4-4-4-12 hex UUID string. (Named UUIDString for symmetry with URLString.)
/** A canonical 8-4-4-4-12 hex UUID string. (Named `UUIDString` for symmetry
with `URLString`.) */
@validate(isUuid)
@jsonSchema({
...uuidFormat
})
export type UUIDString = stringValidators: isUuid
JSON Schema metadata:
{
...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.
/** 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) = numberValidators: min.partial(n: low), max.partial(n: high)
JSON Schema metadata:
{
minimum: low,
maximum: high
}(source)
StringWithLength
A string whose length is in the inclusive range [min, max].
/** 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) = stringValidators: minLength.partial(n: min), maxLength.partial(n: max)
JSON Schema metadata:
{
minLength: min,
maxLength: max
}(source)
MatchesPattern
A string that matches the given regular expression pat.
/** A string that matches the given regular expression `pat`. */
@validate(matches.partial(pattern: pat))
@jsonSchema({
pattern: pat
})
export type MatchesPattern(pat: string) = stringValidators: matches.partial(pattern: pat)
JSON Schema metadata:
{
pattern: pat
}(source)
BoundedArray
An array whose length is in the inclusive range [min, max].
/** 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:
{
minItems: min,
maxItems: max
}(source)
Constants
emailFormat
export static const emailFormat = {
format: "email"
}(source)
urlFormat
export static const urlFormat = {
format: "uri"
}(source)
uuidFormat
export static const uuidFormat = {
format: "uuid"
}(source)
dateTimeFormat
export static const dateTimeFormat = {
format: "date-time"
}(source)
dateFormat
export static const dateFormat = {
format: "date"
}(source)
ipv4Format
export static const ipv4Format = {
format: "ipv4"
}(source)
ipv6Format
export static const ipv6Format = {
format: "ipv6"
}(source)
Functions
isEmail
isEmail(value: string): ResultReturns success if value is a syntactically valid email address, failure otherwise.
@param value - The string to check.
Parameters:
| Name | Type | Default |
|---|---|---|
| value | string |
Returns: Result
(source)
isUrl
isUrl(value: string): ResultReturns success if value is an http:// or https:// URL, failure otherwise.
@param value - The string to check.
Parameters:
| Name | Type | Default |
|---|---|---|
| value | string |
Returns: Result
(source)
isUuid
isUuid(value: string): ResultReturns success if value is a canonical UUID string (8-4-4-4-12 hex), failure otherwise.
@param value - The string to check.
Parameters:
| Name | Type | Default |
|---|---|---|
| value | string |
Returns: Result
(source)
isInt
isInt(value: number): ResultReturns success if value is an integer (no fractional component), failure otherwise.
@param value - The number to check.
Parameters:
| Name | Type | Default |
|---|---|---|
| value | number |
Returns: Result
(source)
isPositive
isPositive(value: number): ResultReturns success if value > 0, failure otherwise.
@param value - The number to check.
Parameters:
| Name | Type | Default |
|---|---|---|
| value | number |
Returns: Result
(source)
isNegative
isNegative(value: number): ResultReturns success if value < 0, failure otherwise.
@param value - The number to check.
Parameters:
| Name | Type | Default |
|---|---|---|
| value | number |
Returns: Result
(source)
min
min(n: number, value: number): ResultReturns 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:
| Name | Type | Default |
|---|---|---|
| n | number | |
| value | number |
Returns: Result
(source)
max
max(n: number, value: number): ResultReturns 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:
| Name | Type | Default |
|---|---|---|
| n | number | |
| value | number |
Returns: Result
(source)
minLength
minLength(n: number, value: string): ResultReturns 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:
| Name | Type | Default |
|---|---|---|
| n | number | |
| value | string |
Returns: Result
(source)
maxLength
maxLength(n: number, value: string): ResultReturns 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:
| Name | Type | Default |
|---|---|---|
| n | number | |
| value | string |
Returns: Result
(source)
matches
matches(pattern: string, value: string): ResultReturns 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:
| Name | Type | Default |
|---|---|---|
| pattern | string | |
| value | string |
Returns: Result
(source)