// 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)")