Table of Contents
- Validation
- Simple Validation
- Request Validation * API
- Error messages
- Rules
- accepted
- after
- afterOrEqual
- alpha
- alphaDash
- alphaNum
- array
- before
- beforeOrEqual
- betweenNum
- betweenStr
- betweenArr
- betweenFile
- boolean
- confirmed
- date
- dateEquals
- different
- digits
- digitsBetween
- distinctArr
- domain
- endsWith
- file
- filled
- gtNum
- gtFile
- gtStr
- gtArr
- gteNum
- gteFile
- gteStr
- gteArr
- image
- in
- inArray
- integer
- json
- ltNum
- ltFile
- ltStr
- ltArr
- lteNum
- lteFile
- lteStr
- ltArr
- maxNum
- maxFile
- maxStr
- maxArr
- mimes
- minNum
- minFile
- minStr
- minArr
- notIn
- notRegex
- numeric
- present
- regex
- required
- requiredIf
- requiredUnless
- requiredWith
- requiredWithAll
- requiredWithout
- same
- sizeNum
- sizeFile
- sizeStr
- sizeArr
- startsWith
- timestamp
- url
- uuid
Basolato has it's own validation function. It recieves request and check request params.
There are two validation type. One is used in controller that recieve request and return errors array.
Another is more simple. Recieve value and return bool
.
import basolato/core/validation
echo newValidation().email("[email protected]")
>> true
echo newValidation().email("sample@example")
>> false
import basolato/request_validation
func newRequestValidation*(params: Params):RequestValidation =
func hasErrors*(self:RequestValidation):bool =
func hasError*(self:RequestValidation, key:string):bool =
func errors*(self:RequestValidation):ValidationErrors =
func add*(self:ValidationErrors, key, value:string) =
proc storeValidationResult*(client:Client, validation:RequestValidation) {.async.} =
storeValidationResult
stores params and validation errors to session as flash message.
form request
<input type="base" name="email" value="[email protected]">
or json request
{
"email": "[email protected]"
}
proc signUp*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
v.required("email")
v.email("email")
let client = await newClient(request)
if v.hasErrors:
await client.storeValidationResult(v)
return redirect("/signup")
let client = await newClient(request)
echo await client.getFlash()
>> {
"errors": {
"email": ["The name field is required."]
},
"params": {
"email": "[email protected]"
}
}
Definition of error messages is in resources/lang/{locale}/validation.json
.
Default local is en
. If you want to replace it, please define environment valiable LOCALE
.
Error message has request params key name by default. You can replace it.
default
let v = newRequestValidation(params)
v.required("name")
v.errors["name"][0] == "The name field is required."
replace
let v = newRequestValidation(params)
v.required("name", attribute="User Name")
v.errors["name"][0] == "The User Name field is required."
See test code of simple validation and request validation
The field under validation must be "yes", "on", 1, or true. This is useful for validating "Terms of Service" acceptance or similar fields.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "on"
v.accepted("base")
assert v.hasErrors == false
The field under validation must be a value after a given date.
Instead of passing a date string to be evaluated by format
, you may specify another field to compare against the Datetime
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "2020-01-01"
assert params.getStr("target") == "2020-01-02"
v.after("base", "target", "yyyy-MM-dd")
v.after("base", "2020-01-02".parse("yyyy-MM-dd"), "yyyy-MM-dd")
assert v.hasErrors == false
The field under validation must be a value after or equal to the given date.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "2020-01-01"
assert params.getStr("same") == "2020-01-01"
assert params.getStr("target") == "2020-01-02"
v.afterOrEqual("base", "target", "yyyy-MM-dd")
v.afterOrEqual("base", "same", "yyyy-MM-dd")
v.afterOrEqual("base", "2020-01-01".parse("yyyy-MM-dd"), "yyyy-MM-dd")
v.afterOrEqual("base", "2020-01-02".parse("yyyy-MM-dd"), "yyyy-MM-dd")
assert v.hasErrors == false
The field under validation must be entirely alphabetic characters.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("small") == "abcdefghijklmnopqrstuvwxyz"
assert params.getStr("large") == "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
v.alpha("small")
v.alpha("large")
assert v.hasErrors == false
The field under validation may have alpha-numeric characters, as well as dashes and underscores.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "abcABC012-_"
v.alphaDash("base")
assert v.hasErrors == false
The field under validation must be entirely alpha-numeric characters.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "abcABC012"
v.alphaNum("base")
assert v.hasErrors == false
The field under validation must be a array
.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "a, b, c"
v.array("base")
assert v.hasErrors == false
The field under validation must be a value preceding the given date.
In addition, like the after
rule, the name of another field under validation may be supplied as the value of Datetime
.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "2020-01-02"
assert params.getStr("target") == "2020-01-01"
v.before("base", "target", "yyyy-MM-dd")
v.before("base", "2020-01-01".parse("yyyy-MM-dd")", "yyyy-MM-dd")
assert v.hasErrors == false
The field under validation must be a value preceding or equal the given date.
In addition, like the after
rule, the name of another field under validation may be supplied as the value of Datetime
.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "2020-01-02"
assert params.getStr("same") == "2020-01-02"
assert params.getStr("target") == "2020-01-01"
v.beforeOrEqual("base", "target", "yyyy-MM-dd")
v.beforeOrEqual("base", "same", "yyyy-MM-dd")
v.beforeOrEqual("base", "2020-01-01".parse("yyyy-MM-dd")", "yyyy-MM-dd")
v.beforeOrEqual("base", "2020-01-02".parse("yyyy-MM-dd")", "yyyy-MM-dd")
assert v.hasErrors == false
The field under validation must be between the given min and max.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getInt("int") == 2
assert params.getFloat("float") == 2.0
v.betweenNum("int", 1, 3)
v.betweenNum("float", 1.9, 2.1)
assert v.hasErrors == false
The field under validation must have a length between the given min and max.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("str") == "ab"
v.betweenStr("str", 1, 3)
assert v.hasErrors == false
The field under validation must have a length between the given min and max.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("arr") == "a, b"
v.betweenStr("arr", 1, 3)
assert v.hasErrors == false
The field under validation must have a size between the given min and max.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("file").len == 2048
v.betweenFile("file", 1, 3)
assert v.hasErrors == false
The field under validation must be able to be cast as a boolean.
Accepted input are y, yes, true, 1, on, n, no, false, 0, off
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("bool") == "true"
v.boolean("bool")
assert v.hasErrors == false
The field under validation must have a matching field of {field}_confirmation
. For example, if the field under validation is password
, a matching password_confirmation
field must be present in the input.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("password") == "aaa"
assert params.getStr("password_confirmation") == "aaa"
assert params.getStr("password_check") == "aaa"
v.confirmed("password")
v.confirmed("password", saffix="_check")
assert v.hasErrors == false
The field under validation must be a valid, non-relative Datetime
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "2020-01-01"
v.date("base", "yyyy-MM-dd")
assert v.hasErrors == false
The field under validation must be equal to the given Datetime
.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("date") == "2020-01-01"
assert params.getStr("timestamp") == "1577880000"
let target = "2020-01-01".format("yyyy-MM-dd")
v.dateEquals("base", "yyyy-MM-dd", target)
v.dateEquals("timestamp", target)
assert v.hasErrors == false
The field under validation must have a different value than arge2
.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "a"
assert params.getStr("target") == "b"
v.different("base", "target")
assert v.hasErrors == false
The field under validation must be numeric and must have an exact length of arge2
.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "10"
v.digits("base", 2)
assert v.hasErrors == false
The field under validation must be numeric and must have a length between the given min and max.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "10"
v.digitsBetween("base", 1, 3)
assert v.hasErrors == false
When validating arrays, the field under validation must not have any duplicate values.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "a, b, c"
v.distinctArr("base")
assert v.hasErrors == false
The field under validation must have a valid A or AAAA record.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("v4") == "domain.com"
assert params.getStr("v6") == "[2001:0db8:bd05:01d2:288a:1fc0:0001:10ee]"
v.domain("v4")
v.domain("v6")
assert v.hasErrors == false
The field under validation must be formatted as an email address.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "[email protected]"
v.email("base")
assert v.hasErrors == false
The field under validation must end with one of the given values.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "abcdefg"
v.email("base", ["ef", "fg"])
assert v.hasErrors == false
The field under validation must be a successfully uploaded file.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params["base"].ext == "jpg"
v.file("base")
assert v.hasErrors == false
The field under validation must not be empty when it is present.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "a"
v.filled("base")
assert v.hasErrors == false
The field under validation must be greater than the given field.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getInt("base") == 2
assert params.getInt("target") == 1
v.gtFile("base", "target")
assert v.hasErrors == false
The field under validation must have a greater size than the given field.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base").len == 2048
assert params["base"].ext == "jpg"
assert params.getStr("target").len == 1024
assert params["target"].ext == "jpg"
v.gtFile("base", "target")
assert v.hasErrors == false
The field under validation must be longer than the given field.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "ab"
assert params.getStr("target") == "a"
v.gtStr("base", "target")
assert v.hasErrors == false
The field under validation must have more items than the given field.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "a, b"
assert params.getStr("target") == "a"
v.gtArr("base", "target")
assert v.hasErrors == false
The field under validation must be same or greater than the given field.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getInt("base") == 2
assert params.getInt("same") == 2
assert params.getInt("target") == 1
v.gtFile("base", "target")
v.gtFile("base", "same")
assert v.hasErrors == false
The field under validation must be have a greater or same size than the given field.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base").len == 2048
assert params.getStr("same").len == 2048
assert params.getStr("target").len == 1024
v.gteFile("base", "target")
v.gteFile("base", "same")
assert v.hasErrors == false
The field under validation must be have longer or same than the given field.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "ab"
assert params.getStr("same") == "bc"
assert params.getStr("target") == "a"
v.gteStr("base", "target")
v.gteStr("base", "same")
assert v.hasErrors == false
The field under validation must have more or same items than the given field.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "a, b"
assert params.getStr("same") == "b, c"
assert params.getStr("target") == "a"
v.gteArr("base", "target")
v.gteArr("base", "same")
assert v.hasErrors == false
The file under validation must be an image (jpg, jpeg, png, bmp, gif, svg, or webp).
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params["base"].ext == "jpg"
v.image("base")
assert v.hasErrors == false
The field under validation must be included in the given list of values.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params["base"].ext == "a"
v.in("base", ["a", "b"])
assert v.hasErrors == false
The field under validation must be in anotherfield's values.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "a"
assert params.getStr("target") == "a, b, c"
v.inArray("base", "target")
assert v.hasErrors == false
The field under validation must be an integer.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getInt("base") == 1
v.integer("base")
assert v.hasErrors == false
The field under validation must be a valid JSON string.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == """{"key": "value"}"""
v.json("base")
assert v.hasErrors == false
The field under validation must be less than the given field.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getInt("base") == 1
assert params.getInt("target") == 2
v.ltNum("base", "target")
assert v.hasErrors == false
The field under validation must have less size than the given field.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base").len == 1024
assert params.getStr("target").len == 2048
v.ltFile("base", "target")
assert v.hasErrors == false
The field under validation must have less length than the given field.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "a"
assert params.getStr("target") == "ab"
v.ltStr("base", "target")
assert v.hasErrors == false
The field under validation must have less items than the given field.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "a"
assert params.getStr("target") == "a, b"
v.ltStr("base", "target")
assert v.hasErrors == false
The field under validation must be less than or same the given field.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getInt("base") == 1
assert params.getInt("same") == 1
assert params.getInt("target") == 2
v.ltNum("base", "target")
v.ltNum("base", "same")
assert v.hasErrors == false
The field under validation must have less size than or same size the given field.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base").len == 1024
assert params.getStr("same").len == 1024
assert params.getStr("target").len == 2048
v.lteFile("base", "target")
v.lteFile("base", "same")
assert v.hasErrors == false
The field under validation must have less length than or same length the given field.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "a"
assert params.getStr("same") == "b"
assert params.getStr("target") == "ab"
v.lteStr("base", "target")
v.lteStr("base", "same")
assert v.hasErrors == false
The field under validation must have less or same items than the given field.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base").len == "a"
assert params.getStr("same").len == "a"
assert params.getStr("target").len == "a, b"
v.ltStr("base", "target")
v.ltStr("base", "same")
assert v.hasErrors == false
The field under validation must be less than or equal to a maximum value.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getInt("base") == 2
assert params.getInt("small") == 1
v.maxNum("base", 2)
v.maxNum("small", 2)
assert v.hasErrors == false
The field under validation must be less size than or equal to a maximum value.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base").len == 2048
assert params.getStr("small").len == 1024
v.maxFile("base", 2)
v.maxFile("small", 2)
assert v.hasErrors == false
The field under validation must be less length than or equal to a maximum value.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "ab"
assert params.getStr("small") == "a"
v.maxStr("base", 2)
v.maxStr("small", 2)
assert v.hasErrors == false
The field under validation must be less length than or equal to a maximum value.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "a, b"
assert params.getStr("small") == "a"
v.maxArr("base", 2)
v.maxArr("small", 2)
assert v.hasErrors == false
The file under validation must have a MIME type corresponding to one of the listed extensions.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params["base"].ext == "jpg"
v.mimes("base", ["jpg", "gif"])
assert v.hasErrors == false
The field under validation must have a minimum value.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getInt("base") == 2
v.minNum("base", 1)
v.minNum("base", 2)
assert v.hasErrors == false
The field under validation must have a minimum value of size.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base").len == 2048
v.minFile("base", 1)
v.minFile("base", 2)
assert v.hasErrors == false
The field under validation must have a minimum value of length.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "ab"
v.minStr("base", 1)
v.minStr("base", 2)
assert v.hasErrors == false
The field under validation must have a minimum value of length.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "a, b"
v.minArr("base", 1)
v.minArr("base", 2)
assert v.hasErrors == false
The field under validation must not be included in the given list of values.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "a"
v.notIn("base", ["b", "c"])
assert v.hasErrors == false
The field under validation must not match the given regular expression.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "abc"
v.notRegex("base", re"\d")
assert v.hasErrors == false
The field under validation must be numeric
.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getInt("base") == 1
assert params.getFloat("float") == -1.23
v.numeric("base")
v.numeric("float")
assert v.hasErrors == false
The field under validation must be present in the input data but can be empty.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == ""
v.present("base")
assert v.hasErrors == false
The field under validation must match the given regular expression.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "abc"
v.regex("base", re"\w")
assert v.hasErrors == false
The field under validation must be present in the input data and not empty. A field is considered "empty" if one of the following conditions are true:
- The value is
null
. - The value is an empty string.
- The value is an empty array or empty
Countable
object. - The value is an uploaded file with no path.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "abc"
v.required("base")
assert v.hasErrors == false
The field under validation must be present and not empty if the anotherfield field is equal to any value.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "abc"
assert params.getStr("empty") == ""
assert params.getStr("other") == "123"
v.requiredIf("base", "other", ["123"])
v.requiredIf("empty", "other", ["xyz"])
assert v.hasErrors == false
The field under validation must be present and not empty unless the anotherfield field is equal to any value.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "abc"
assert params.getStr("empty") == ""
assert params.getStr("other") == "123"
v.requiredUnless("base", "other", ["123"])
v.requiredUnless("empty", "other", ["123"])
assert v.hasErrors == false
The field under validation must be present and not empty only if any of the other specified fields are present and not empty.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "abc"
assert params.getStr("other") == "123"
v.requiredWith("base", ["a"])
v.requiredWith("base", ["other"])
assert v.hasErrors == false
The field under validation must be present and not empty only if all of the other specified fields are present and not empty.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "abc"
assert params.getStr("empty") == ""
assert params.getStr("other1") == "123"
assert params.getStr("other2") == "123"
v.requiredWithAll("valid", ["other1", "other2"])
v.requiredWithAll("empty", ["notExists"])
assert v.hasErrors == false
The field under validation must be present and not empty only when any of the other specified fields are empty or not present.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "abc"
assert params.getStr("empty") == ""
assert params.getStr("other") == "123"
v.requiredWithoutAll("base", ["aaa", "bbb"])
v.requiredWithoutAll("empty", ["other"])
assert v.hasErrors == false
The given field must match the field under validation.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "abc"
assert params.getStr("target") == "abc"
v.same("base", "target")
assert v.hasErrors == false
The field under validation must have a size matching the given value. For numeric data, value corresponds to a given integer value (the attribute must also have the numeric or integer rule).
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getInt("base") == 2
v.sizeNum("base", 2)
assert v.hasErrors == false
The field under validation must have a size matching the given value. For files, size corresponds to the file size in kilobytes.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base").len == 2048
v.sizeFile("base", 2)
assert v.hasErrors == false
The field under validation must have a size matching the given value.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "ab"
v.sizeStr("base", 2)
assert v.hasErrors == false
The field under validation must have a size matching the given value. For an array, size corresponds to the length of the array.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "a, b"
v.sizeArr("base", 2)
assert v.hasErrors == false
The field under validation must start with one of the given values.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "abcde"
v.startsWith("base", ["abc", "bcd"])
assert v.hasErrors == false
The field under validation must be a valid timestamp.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "1577804400"
v.timestamp("base")
assert v.hasErrors == false
The field under validation must be a valid URL.
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "https://google.com:8000/xxx/yyy/zzz?key=value"
v.url("base")
assert v.hasErrors == false
The field under validation must be a valid RFC 4122 (version 1, 3, 4, or 5) universally unique identifier (UUID).
proc index*(request:Request, params:Params):Future[Response] {.async.} =
let v = newRequestValidation(params)
assert params.getStr("base") == "a0a2a2d2-0b87-4a18-83f2-2529882be2de"
v.url("base")
assert v.hasErrors == false