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
|
||||
Vendored
+36
@@ -0,0 +1,36 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "wdantic" for Validator
|
||||
|
||||
System.print(Validator.email("test@example.com")) // expect: true
|
||||
System.print(Validator.email("invalid")) // expect: false
|
||||
System.print(Validator.email("test@domain")) // expect: false
|
||||
|
||||
System.print(Validator.domain("example.com")) // expect: true
|
||||
System.print(Validator.domain("sub.example.com")) // expect: true
|
||||
System.print(Validator.domain("-invalid.com")) // expect: false
|
||||
|
||||
System.print(Validator.safeStr("Hello World")) // expect: true
|
||||
System.print(Validator.safeStr("Line1\nLine2")) // expect: false
|
||||
|
||||
System.print(Validator.url("https://example.com")) // expect: true
|
||||
System.print(Validator.url("http://localhost:8080/path")) // expect: true
|
||||
System.print(Validator.url("ftp://invalid")) // expect: false
|
||||
|
||||
System.print(Validator.uuid("550e8400-e29b-41d4-a716-446655440000")) // expect: true
|
||||
System.print(Validator.uuid("invalid")) // expect: false
|
||||
|
||||
System.print(Validator.minLength("hello", 3)) // expect: true
|
||||
System.print(Validator.minLength("hi", 3)) // expect: false
|
||||
|
||||
System.print(Validator.maxLength("hi", 5)) // expect: true
|
||||
System.print(Validator.maxLength("hello world", 5)) // expect: false
|
||||
|
||||
System.print(Validator.range(5, 1, 10)) // expect: true
|
||||
System.print(Validator.range(15, 1, 10)) // expect: false
|
||||
|
||||
System.print(Validator.positive(5)) // expect: true
|
||||
System.print(Validator.positive(-5)) // expect: false
|
||||
|
||||
System.print(Validator.integer(5)) // expect: true
|
||||
System.print(Validator.integer(5.5)) // expect: false
|
||||
Reference in New Issue
Block a user