{# retoor #} {% extends 'page.html' %} {% set page_title = "wdantic" %} {% set breadcrumb = [{"url": "api/index.html", "title": "API Reference"}, {"title": "wdantic"}] %} {% set prev_page = {"url": "api/argparse.html", "title": "argparse"} %} {% set next_page = {"url": "api/dataset.html", "title": "dataset"} %} {% block article %}

wdantic

The wdantic module provides data validation similar to Python's Pydantic. It includes standalone validators and a schema-based validation system for structured data.

import "wdantic" for Validator, Field, Schema, ValidationResult

Validator Class

Validator

Standalone validation functions

Static Methods

Validator.email(value) → Bool

Validates email address format.

Validator.email("user@example.com")  // true
Validator.email("invalid")           // false
Validator.domain(value) → Bool

Validates domain name format.

Validator.domain("example.com")  // true
Validator.domain("localhost")    // false
Validator.url(value) → Bool

Validates HTTP/HTTPS URL format.

Validator.url("https://example.com/path")  // true
Validator.url("ftp://example.com")         // false
Validator.uuid(value) → Bool

Validates UUID format.

Validator.uuid("550e8400-e29b-41d4-a716-446655440000")  // true
Validator.safeStr(value) → Bool

Checks if string contains only printable ASCII characters (32-126).

Validator.safeStr("Hello World")  // true
Validator.safeStr("Hello\x00")    // false
Validator.base64(value) → Bool

Validates Base64 encoding format.

Validator.base64("SGVsbG8=")  // true
Validator.base64("invalid!")  // false
Validator.json(value) → Bool

Checks if string is valid JSON.

Validator.json("{\"key\": \"value\"}")  // true
Validator.json("{invalid}")             // false
Validator.ipv4(value) → Bool

Validates IPv4 address format.

Validator.ipv4("192.168.1.1")  // true
Validator.ipv4("256.0.0.1")    // false
Validator.minLength(value, min) → Bool

Checks minimum length of string or list.

Validator.maxLength(value, max) → Bool

Checks maximum length of string or list.

Validator.range(value, min, max) → Bool

Checks if number is within range (inclusive).

Validator.range(5, 1, 10)  // true
Validator.range(15, 1, 10) // false
Validator.positive(value) → Bool

Checks if number is positive (greater than 0).

Validator.negative(value) → Bool

Checks if number is negative (less than 0).

Validator.integer(value) → Bool

Checks if number is an integer (no decimal part).

Validator.regex(value, pattern) → Bool

Tests if value matches a regular expression pattern.

Validator.regex("abc123", "^[a-z]+[0-9]+$")  // true

Field Class

Field

Factory for creating field definitions

Static Methods

Field.string() → StringField
Field.string(options) → StringField

Creates a string field. Options: minLength, maxLength, pattern, required, default.

Field.integer() → IntegerField
Field.integer(options) → IntegerField

Creates an integer field. Options: min, max, required, default.

Field.number() → NumberField
Field.number(options) → NumberField

Creates a number field (integer or float). Options: min, max, required, default.

Field.email() → EmailField

Creates a field that validates email format.

Field.boolean() → BooleanField

Creates a boolean field.

Field.list(itemType) → ListField

Creates a list field with typed items.

Field.map() → MapField

Creates a map/object field.

Field.optional(fieldDef) → OptionalField

Makes any field optional (not required).

Schema Class

Schema

Validates data against a field definition map

Constructor

Schema.new(definition) → Schema

Creates a schema from a map of field names to field definitions.

Instance Methods

validate(data) → ValidationResult

Validates data against the schema.

validateOrAbort(data) → Map

Validates data and aborts on failure. Returns validated data on success.

ValidationResult Class

ValidationResult

Result of schema validation

Properties

isValid → Bool

Whether validation passed.

errors → List

List of ValidationError objects.

data → Map

Validated and coerced data (null if invalid).

Examples

Basic Schema Validation

import "wdantic" for Field, Schema

var userSchema = Schema.new({
    "name": Field.string({"minLength": 1, "maxLength": 100}),
    "email": Field.email(),
    "age": Field.integer({"min": 0, "max": 150})
})

var result = userSchema.validate({
    "name": "Alice",
    "email": "alice@example.com",
    "age": 30
})

if (result.isValid) {
    System.print("Valid: %(result.data)")
} else {
    for (error in result.errors) {
        System.print("Error: %(error)")
    }
}

Optional Fields

import "wdantic" for Field, Schema

var schema = Schema.new({
    "title": Field.string(),
    "description": Field.optional(Field.string()),
    "tags": Field.optional(Field.list(Field.string()))
})

var result = schema.validate({
    "title": "My Post"
})

System.print(result.isValid)  // true

Nested Lists

import "wdantic" for Field, Schema

var schema = Schema.new({
    "numbers": Field.list(Field.integer({"min": 0}))
})

var result = schema.validate({
    "numbers": [1, 2, 3, -1]  // -1 will fail
})

System.print(result.isValid)  // false

Using validateOrAbort

import "wdantic" for Field, Schema

var schema = Schema.new({
    "username": Field.string({"minLength": 3}),
    "password": Field.string({"minLength": 8})
})

var data = schema.validateOrAbort({
    "username": "admin",
    "password": "secret123"
})

Standalone Validators

import "wdantic" for Validator

var email = "user@example.com"
if (Validator.email(email)) {
    System.print("Valid email")
}

var ip = "192.168.1.1"
if (Validator.ipv4(ip)) {
    System.print("Valid IP address")
}

var jsonStr = "{\"key\": 123}"
if (Validator.json(jsonStr)) {
    System.print("Valid JSON")
}
Note

Field types automatically coerce values when possible. For example, IntegerField will convert the string "42" to the number 42.

{% endblock %}