aladov3.13.0

Docs/Validation

Available Rules

Type

Method Description
string() Must be a string
number() Must be a finite number (not NaN, not Infinity: a JSON body can produce it with 1e999)
bigint() Must be a bigint
boolean() Must be a boolean
array() Must be an array
object() Must be a plain object (not array, not null)

String

Method Description
min(n) Min length (string) or min value (number)
max(n) Max length (string) or max value (number)
length(n) Exact length
regex(pattern) Must match RegExp
email() Valid email format, at most 254 characters
uuid() Valid UUID v4
objectId() Valid MongoDB ObjectId (24-char hex)
url(protocols?) Valid URL (parsed by new URL()) whose protocol is in protocols (default ['http', 'https', 'ws', 'wss'])
trim() No leading/trailing whitespace

Number

Method Description
integer() Must be an integer
positive() Must be > 0
negative() Must be < 0

min, max, integer, positive and negative accept bigints as well.

Numbers in query and path parameters

Query and path values always arrive as strings, and so do the fields of a form body (urlencoded or multipart). When such a field is validated by a validator built from validate.number() or validate.bigint(), the string is converted before validation, and the handler gets the converted value:

TypeScript
class ListQueryDto {
  @validateProperty({
    handler: validate.number().integer().min(1).max(100).build(),
    error: { statusCode: 400, message: 'limit must be 1 to 100' },
  })
  limit: number = 20; // ?limit=50 -> req.query.limit === 50
}

Accepted: decimal numbers with sign, fraction and exponent (-1.5e3), 0x / 0o / 0b integers, digit separators (1_000), and for bigint() an optional n suffix (123n). Anything else stays a string and fails validation: empty or padded text, Infinity, NaN, out-of-range values, and for number() integers past Number.MAX_SAFE_INTEGER (use bigint() for those). Other validators, such as string(), get the text unchanged, and a repeated key (?limit=1&limit=2) stays an array.

A JSON body is typed already, so nothing is converted there: "30" is a string and fails number().

JSON.stringify cannot serialise a bigint: convert it (for example with String()) before putting it in a response body.

url() protocols

new URL() also accepts javascript:, data: and file: URLs, so url() checks the protocol as well. Pass the list to allow others, e.g. url(['https', 'mailto']). The rule checks the format only, not where the URL points: http://169.254.169.254/ and private network addresses pass, so it is not protection against server-side request forgery.

Generic

Method Description
enum(...values) Must be one of the provided values
custom(fn) Any custom (value) => boolean | Promise<boolean> function

Object

Method Description
shape(schema) Validate each key against a nested validator
minKeys(n) Object must have at least n keys
maxKeys(n) Object must have at most n keys
or(...keys) At least one of the listed keys must be present and non-empty
xor(...keys) Exactly one of the listed keys must be present (mutually exclusive)

Array

Method Description
items(fn) Every element must pass the validator
minItems(n) Minimum array length
maxItems(n) Maximum array length
unique() No duplicate values