Input Validation
Robust schema-based validation with automatic type coercion and detailed error reporting.
Trust, but Verify
SoliLang's validation library is built around the V class. It allows you to define declarative schemas for your data. It handles not just checking values, but also safely converting types (coercion) and sanitizing input.
1 The V Class
Start by choosing the base type using the global V object.
V.string() // Matches text
V.int() // Matches integers (e.g., "123" -> 123)
V.float() // Matches decimals
V.bool() // Matches true/false (and "true"/"false")
V.array(schema) // Matches a list of items
V.hash(schema) // Matches a nested object
2 Validation in Action
Chain methods to build complex rules. Here is a realistic registration schema:
// 1. Define the schema
let user_schema = {
"email": V.string().required().email().trim(),
"password": V.string().required().min_length(8),
"age": V.int().optional().min(18).max(120),
"newsletter": V.bool().default(false)
};
// 2. Validate input data (e.g., from a POST request)
let result = validate(req["body"], user_schema);
// 3. Handle results
if result["valid"] {
// result["data"] contains the clean, coerced data
create_user(result["data"]);
} else {
// result["errors"] contains validation messages
return render("register", { "errors": result["errors"] });
}
Available Constraints
Universal Constraints
.required()
Field must be present and not null.
.optional()
Field is optional (can be undefined).
.nullable()
Allows explicit null values.
.default(value)
Sets a default value if missing.
.one_of([a, b])
Must match one of the provided values.
String Constraints
.email()
Validates standard email format.
.url()
Validates URL format.
.min_length(n)
Minimum number of characters.
.max_length(n)
Maximum number of characters.
.pattern(regex)
Matches against a regex pattern.
.trim()
Removes whitespace from both ends.
Numeric Constraints
.min(n)
Value must be greater or equal to n.
.max(n)
Value must be less or equal to n.
.positive()
Must be greater than 0.
.negative()
Must be less than 0.
Interactive Validation Lab
Test different constraints against live input in our interactive demo.