src/prologue/validater/validater

Source   Edit  

This module contains basic validation operations.

The single text validation

Example:

import src/prologue/validater/validater
import strtabs

let
  msg = "Int required"
  decide = isInt(msg)
  decideDefaultMsg = isInt()

doAssert decide("12") == (true, "")
doAssert decide("-753") == (true, "")
doAssert decide("0") == (true, "")
doAssert decide("912.6") == (false, msg)
doAssert decide("a912") == (false, msg)
doAssert decide("") == (false, msg)
doAssert decideDefaultMsg("a912") == (false, "a912 is not an integer!")
doAssert decideDefaultMsg("") == (false, " is not an integer!")
Multiple texts validation

Example:

import src/prologue/validater/validater
import strtabs

var form = newFormValidation({
      "accepted": @[required(), accepted()],
      "required": @[required()],
      "requiredInt": @[required(), isInt()],
      "minValue": @[required(), isInt(), minValue(12), maxValue(19)]
    })
let
  chk1 = form.validate({"required": "on", "accepted": "true",
      "requiredInt": "12", "minValue": "15"}.newStringTable)
  chk2 = form.validate({"requird": "on", "time": "555",
      "minValue": "10"}.newStringTable)
  chk3 = form.validate({"requird": "on", "time": "555",
      "minValue": "10"}.newStringTable, allMsgs = false)
  chk4 = form.validate({"required": "on", "accepted": "true",
  "requiredInt": "12.5", "minValue": "13"}.newStringTable, allMsgs = false)

doAssert chk1 == (true, "")
doAssert not chk2.hasValue
doAssert chk2.msg == "Can\'t find key: accepted\nCan\'t find key: " &
        "required\nCan\'t find key: requiredInt\n10 is not greater than or equal to 12.0!\n"
doAssert not chk3.hasValue
doAssert chk3.msg == "Can\'t find key: accepted\n"
doAssert not chk4.hasValue
doAssert chk4.msg == "12.5 is not an integer!\n"

Types

FormValidation = object
  
Source   Edit  
Info = tuple[hasValue: bool, msg: string]
Source   Edit  
ValidateHandler = proc (text: string): Info {.closure.}
Source   Edit  

Procs

func accepted(msg = ""): ValidateHandler {.inline, ...raises: [], tags: [],
    forbids: [].}
If lowerAscii input in {"yes", "on", "1", or "true"}, return true If the length of msg is more than 0, returns this msg when failed. Source   Edit  
func equals(value: string; msg = ""): ValidateHandler {.inline, ...raises: [],
    tags: [], forbids: [].}
The content of text is equal to value. If the length of msg is more than 0, returns this msg when failed. Source   Edit  
func isBool(msg = ""): ValidateHandler {.inline, ...raises: [], tags: [],
    forbids: [].}
The value of text is a bool. If the length of msg is more than 0, returns this msg when failed. Source   Edit  
func isInt(msg = ""): ValidateHandler {.inline, ...raises: [], tags: [],
                                        forbids: [].}
The value of text is a int. If the length of msg is more than 0, returns this msg when failed. Source   Edit  
func isNumeric(msg = ""): ValidateHandler {.inline, ...raises: [], tags: [],
    forbids: [].}
The value of text is a number. If the length of msg is more than 0, returns this msg when failed. Source   Edit  
func matchRegex(value: Regex; msg = ""): ValidateHandler {.inline, ...raises: [],
    tags: [], forbids: [].}
Succeeds if the content of text matches the regex expression. If the length of msg is more than 0, returns the msg when failed. Source   Edit  
func matchURL(msg = ""): ValidateHandler {.inline, ...raises: [], tags: [],
    forbids: [].}
Succeeds if the content of text matches the url expression. If the length of msg is more than 0, returns the msg when failed. Source   Edit  
func maxLength(max: Natural; msg = ""): ValidateHandler {.inline, ...raises: [],
    tags: [], forbids: [].}
The length of text is less than or equal to max. If the length of msg is more than 0, returns this msg when failed. Source   Edit  
func maxValue(max: float; msg = ""): ValidateHandler {.inline, ...raises: [],
    tags: [], forbids: [].}
The value of text is less than or equal to max. If the length of msg is more than 0, returns this msg when failed. Source   Edit  
func minLength(min: Natural; msg = ""): ValidateHandler {.inline, ...raises: [],
    tags: [], forbids: [].}
The length of text is more than or equal to min. If the length of msg is more than 0, returns this msg when failed. Source   Edit  
func minValue(min: float; msg = ""): ValidateHandler {.inline, ...raises: [],
    tags: [], forbids: [].}
The value of text is more than or equal to min. If the length of msg is more than 0, returns this msg when failed. Source   Edit  
func newFormValidation(validator: openArray[(string, seq[ValidateHandler])]): FormValidation {.
    inline, ...raises: [], tags: [], forbids: [].}
Creates a new Formvalidation. Source   Edit  
func rangeLength(min, max: Natural; msg = ""): ValidateHandler {.inline,
    ...raises: [], tags: [], forbids: [].}
The length of text is between min and max. If the length of msg is more than 0, returns this msg when failed. Source   Edit  
func rangeValue(min, max: float; msg = ""): ValidateHandler {.inline,
    ...raises: [], tags: [], forbids: [].}
The value of text is between min and max. If the length of msg is more than 0, returns this msg when failed. Source   Edit  
func required(msg = ""): ValidateHandler {.inline, ...raises: [], tags: [],
    forbids: [].}
Succeeds if The content of text is not empty. If the length of msg is more than 0, returns the msg when failed. Source   Edit  
proc validate(formValidation: FormValidation; textTable: StringTableRef;
              allMsgs = true): Info {....raises: [ValueError, Exception, KeyError],
                                      tags: [RootEffect], forbids: [].}
Validates all (key, value) pairs in textTable. Source   Edit