Validate Module
The std/validate module provides common input validation functions for strings, numbers, and data types.
Import
Section titled “Import”import { validate } from "std/validate"String Validators
Section titled “String Validators”validate.isEmail(value: string): bool
Section titled “validate.isEmail(value: string): bool”Returns true if the value is a valid email address.
validate.isEmail("user@example.com") // truevalidate.isEmail("not-an-email") // falsevalidate.isUrl(value: string): bool
Section titled “validate.isUrl(value: string): bool”Returns true if the value is a valid URL (has scheme and host).
validate.isUrl("https://example.com") // truevalidate.isUrl("not a url") // falsevalidate.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) // truevalidate.minLength("hi", 3) // falsevalidate.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) // truevalidate.maxLength("hello", 3) // falsevalidate.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.
| Pattern | Meaning |
|---|---|
^prefix | Starts with |
suffix$ | Ends with |
^exact$ | Exact match |
substring | Contains |
validate.matches("hello world", "^hello") // truevalidate.matches("hello world", "world$") // truevalidate.matches("hello world", "lo wo") // truevalidate.isAlpha(value: string): bool
Section titled “validate.isAlpha(value: string): bool”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.
Type Validators
Section titled “Type Validators”validate.isRequired(value: any): bool
Section titled “validate.isRequired(value: any): bool”Returns true if the value is not null and not an empty string.
validate.isRequired("hello") // truevalidate.isRequired("") // falsevalidate.isRequired(null) // falsevalidate.isInt(value: any): bool
Section titled “validate.isInt(value: any): bool”Returns true if the value is an integer or a string parseable as an integer.
validate.isFloat(value: any): bool
Section titled “validate.isFloat(value: any): bool”Returns true if the value is a float or a string parseable as a float.
validate.isString(value: any): bool
Section titled “validate.isString(value: any): bool”Returns true if the value is a string.
validate.isBool(value: any): bool
Section titled “validate.isBool(value: any): bool”Returns true if the value is a boolean.
Range Validators
Section titled “Range Validators”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) // truevalidate.min(10, 13) // falsevalidate.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) // truevalidate.max(15, 10) // falseBatch Validation
Section titled “Batch Validation”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) }}Function Reference
Section titled “Function Reference”| Function | Description |
|---|---|
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 |
Example: Registration Validation
Section titled “Example: Registration Validation”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)