Skip to content

Validate Module

The std/validate module provides common input validation functions for strings, numbers, and data types.

import { validate } from "std/validate"

Returns true if the value is a valid email address.

validate.isEmail("user@example.com") // true
validate.isEmail("not-an-email") // false

Returns true if the value is a valid URL (has scheme and host).

validate.isUrl("https://example.com") // true
validate.isUrl("not a url") // false

validate.minLength(value: string, min: int): bool

Section titled “validate.minLength(value: string, min: int): bool”

Returns true if the string length is at least min.

validate.minLength("hello", 3) // true
validate.minLength("hi", 3) // false

validate.maxLength(value: string, max: int): bool

Section titled “validate.maxLength(value: string, max: int): bool”

Returns true if the string length is at most max.

validate.maxLength("hi", 10) // true
validate.maxLength("hello", 3) // false

validate.matches(value: string, pattern: string): bool

Section titled “validate.matches(value: string, pattern: string): bool”

Returns true if the value matches the given pattern string.

PatternMeaning
^prefixStarts with
suffix$Ends with
^exact$Exact match
substringContains
validate.matches("hello world", "^hello") // true
validate.matches("hello world", "world$") // true
validate.matches("hello world", "lo wo") // true

Returns true if the value contains only alphabetic characters.

validate.isAlphanumeric(value: string): bool

Section titled “validate.isAlphanumeric(value: string): bool”

Returns true if the value contains only alphanumeric characters.

Returns true if the value is not null and not an empty string.

validate.isRequired("hello") // true
validate.isRequired("") // false
validate.isRequired(null) // false

Returns true if the value is an integer or a string parseable as an integer.

Returns true if the value is a float or a string parseable as a float.

Returns true if the value is a string.

Returns true if the value is a boolean.

validate.min(value: any, minimum: any): bool

Section titled “validate.min(value: any, minimum: any): bool”

Returns true if the value is greater than or equal to minimum.

validate.min(18, 13) // true
validate.min(10, 13) // false

validate.max(value: any, maximum: any): bool

Section titled “validate.max(value: any, maximum: any): bool”

Returns true if the value is less than or equal to maximum.

validate.max(5, 10) // true
validate.max(15, 10) // false

validate.check(results: []bool, messages: []string): map

Section titled “validate.check(results: []bool, messages: []string): map”

Validate multiple conditions at once. Returns a map with valid (bool) and errors (array of strings for failed checks).

const result = validate.check(
[
validate.isRequired(username),
validate.minLength(username, 3),
validate.isEmail(email),
validate.minLength(password, 8)
],
[
"Username is required",
"Username must be at least 3 characters",
"Invalid email address",
"Password must be at least 8 characters"
]
)
if (!result["valid"]) {
for (var err of result["errors"]) {
println(err)
}
}
FunctionDescription
validate.isEmail(value)Check valid email
validate.isUrl(value)Check valid URL
validate.isRequired(value)Check not null/empty
validate.minLength(value, min)Check minimum string length
validate.maxLength(value, max)Check maximum string length
validate.isInt(value)Check integer type
validate.isFloat(value)Check float type
validate.isString(value)Check string type
validate.isBool(value)Check boolean type
validate.min(value, minimum)Check minimum value
validate.max(value, maximum)Check maximum value
validate.matches(value, pattern)Check pattern match
validate.isAlpha(value)Check alphabetic only
validate.isAlphanumeric(value)Check alphanumeric only
validate.check(results, messages)Batch validate with error messages
import { validate } from "std/validate"
import { createServer, Request, Response } from "std/http"
import { json } from "std/json"
const app = createServer()
app.post("/register", function(req: Request, res: Response): Response {
const body = json.parse(req.body)
const username = body["username"]
const email = body["email"]
const password = body["password"]
const result = validate.check(
[
validate.isRequired(username),
validate.minLength(username, 3),
validate.maxLength(username, 20),
validate.isAlphanumeric(username),
validate.isEmail(email),
validate.minLength(password, 8)
],
[
"Username is required",
"Username must be at least 3 characters",
"Username must be at most 20 characters",
"Username must be alphanumeric",
"Invalid email address",
"Password must be at least 8 characters"
]
)
if (!result["valid"]) {
return res.status(400).json(json.stringify(result))
}
return res.status(201).json('{"message": "User created"}')
})
app.listen(3000)