Update.
This commit is contained in:
Vendored
+36
@@ -0,0 +1,36 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "wdantic" for Schema, Field, ValidationResult
|
||||
|
||||
var userSchema = Schema.new({
|
||||
"name": Field.string({"minLength": 1}),
|
||||
"age": Field.integer({"min": 0, "max": 150}),
|
||||
"email": Field.email()
|
||||
})
|
||||
|
||||
var validData = {"name": "John", "age": 30, "email": "john@example.com"}
|
||||
var result = userSchema.validate(validData)
|
||||
System.print(result.isValid) // expect: true
|
||||
System.print(result.data["name"]) // expect: John
|
||||
System.print(result.data["age"]) // expect: 30
|
||||
|
||||
var invalidData = {"name": "", "age": -5, "email": "invalid"}
|
||||
var result2 = userSchema.validate(invalidData)
|
||||
System.print(result2.isValid) // expect: false
|
||||
System.print(result2.errors.count > 0) // expect: true
|
||||
|
||||
var optionalSchema = Schema.new({
|
||||
"name": Field.string(),
|
||||
"nickname": Field.optional(Field.string())
|
||||
})
|
||||
|
||||
var data3 = {"name": "Alice"}
|
||||
var result3 = optionalSchema.validate(data3)
|
||||
System.print(result3.isValid) // expect: true
|
||||
|
||||
var listSchema = Schema.new({
|
||||
"tags": Field.list(Field.string())
|
||||
})
|
||||
var data4 = {"tags": ["a", "b", "c"]}
|
||||
var result4 = listSchema.validate(data4)
|
||||
System.print(result4.isValid) // expect: true
|
||||
Reference in New Issue
Block a user