Files
wren/example/wdantic_demo.wren
T
retoor 5a4054ec66 feat: add example scripts for argparse, bytes, dataset, html, markdown, uuid, wdantic, and web chat modules
Add comprehensive demo scripts showcasing the usage of multiple Wren modules including argument parsing, byte manipulation, in-memory dataset operations, HTML encoding/slugification, markdown-to-HTML conversion, UUID generation/validation, schema validation with wdantic, and a full web chat application with REST endpoints.
2026-01-24 22:40:22 +00:00

73 lines
2.0 KiB
JavaScript
Vendored

// retoor <retoor@molodetz.nl>
import "wdantic" for Validator, Schema, Field
System.print("=== Wdantic Module Demo ===")
System.print("\n--- Validators ---")
var testEmail = "user@example.com"
System.print("Email '%(testEmail)' valid: %(Validator.email(testEmail))")
var testUrl = "https://example.com/path"
System.print("URL '%(testUrl)' valid: %(Validator.url(testUrl))")
var testUuid = "550e8400-e29b-41d4-a716-446655440000"
System.print("UUID '%(testUuid)' valid: %(Validator.uuid(testUuid))")
System.print("\n--- Schema Validation ---")
var userSchema = Schema.new({
"name": Field.string({"minLength": 1, "maxLength": 100}),
"age": Field.integer({"min": 0, "max": 150}),
"email": Field.email(),
"active": Field.boolean()
})
var validUser = {
"name": "John Doe",
"age": 30,
"email": "john@example.com",
"active": true
}
var result = userSchema.validate(validUser)
System.print("Valid user result: %(result.isValid)")
if (result.isValid) {
System.print("Validated data: %(result.data)")
}
System.print("\n--- Invalid Data ---")
var invalidUser = {
"name": "",
"age": -5,
"email": "not-an-email",
"active": "yes"
}
var result2 = userSchema.validate(invalidUser)
System.print("Invalid user result: %(result2.isValid)")
System.print("Errors:")
for (error in result2.errors) {
System.print(" - %(error)")
}
System.print("\n--- Optional Fields ---")
var profileSchema = Schema.new({
"username": Field.string(),
"bio": Field.optional(Field.string({"maxLength": 500})),
"website": Field.optional(Field.string())
})
var minimalProfile = {"username": "alice"}
var fullProfile = {"username": "bob", "bio": "Developer", "website": "https://bob.dev"}
System.print("Minimal profile valid: %(profileSchema.validate(minimalProfile).isValid)")
System.print("Full profile valid: %(profileSchema.validate(fullProfile).isValid)")
System.print("\n--- List Validation ---")
var tagsSchema = Schema.new({
"tags": Field.list(Field.string())
})
var tagData = {"tags": ["wren", "programming", "cli"]}
System.print("Tags valid: %(tagsSchema.validate(tagData).isValid)")