Required
#func Required(value: str): boolTrue when the value is not the empty string; spaces count as a value.
import net.http
import net.http.validate
import net.router
func signup(ctx: i64):
email := http.Form(ctx, "email")
validate.Assert(ctx, validate.IsEmail(email), "invalid email")
if validate.IsEmail(email):
http.Text(ctx, "welcome")
end
end
func main throw:
println validate.Required(""), validate.Required(" "), validate.Required("Ali")
println validate.Length("hello", 2, 5), validate.Length("hello world", 2, 5)
println validate.IsEmail("ali@example.com"), validate.IsEmail("ali@")
println try validate.Matches("09121234567", "^09[0-9]{9}$"),
try validate.Matches("12345", "^09[0-9]{9}$")
mut r := router.NewRouter()
router.Post(r, "/signup", signup)
mut s := http.NewServer(19625)
http.Use(s, r)
_ := http.RunBackground(s)
mut headers := http.NewHeaders()
headers.Insert("Content-Type", "application/x-www-form-urlencoded")
bad := http.PostWith("http://127.0.0.1:19625/signup", "email=x", headers)
println http.Status(bad), http.Body(bad)
good := http.PostWith("http://127.0.0.1:19625/signup", "email=a%40b.ir", headers)
println http.Status(good), http.Body(good)
endfalse true true
true false
true false
true false
400 {"error":"invalid email"}
200 welcome