bag

Types

InputBag = ref object
InputBagType = enum
  inputTypeUrlEncoded, inputTypeMultipart
MinMax = ref object
  length*: int
  error*: string
Rule = object
  id*: string
  required*: bool
  case ftype*: TField
  of tSelect:
    selectOptions*: seq[string]
  of tFile:
    allowFileTypes*: (seq[string], string) ## A tuple containing a seq of mimetypes `@["image/png"]`
                                           ## and the error message
    allowMultiple*: bool     ## Whether to allow multiple files
    maxFiles*: (uint, string) ## The maximum number of files that can be uploaded.
    minFileSize*, maxFileSize*: uint ## The min/max file size allowed in megabytes.
                                     ## Where `0` means unmetered size
  of tDate, tMonth, tTime, tWeek:
    formatDate*: string
    minDate*, maxDate*: tuple[isset: bool, error: string, date: DateTime]
  of tNone:
    callbackHandler*: proc (input: string): bool
  else:
    nil
  error*: string
  min*, max*: MinMax
Rules = OrderedTable[string, Rule]
TField = enum
  tNone, tCheckbox, tColor, tDate, tDatalist, tEmail, tFile, tHidden, tMonth,
  tNumber, tPassword, tTextarea, tRadio, tRange, tSelect, tSearch, tTel, tText,
  tTime, tUrl, tWeek, tBase32, tBase58, tBase64, tCard, tCountry, tCountryState,
  tCountryCapital, tCurrency, tEAN, tIP, tJSON, tMD5, tPort, tPasswordStrength,
  tAlpha, tAlphanumeric, tUppercase, tLowercase, tBool, tFloat, tHex, tRegex,
  tUUID, tCSRF, tDomain

Procs

proc addRule(bag: InputBag; rule: Rule) {....raises: [], tags: [], forbids: [].}
proc getErrors(bag: InputBag): seq[(string, string)] {....raises: [], tags: [],
    forbids: [].}
proc isInvalid(bag: InputBag): bool {....raises: [], tags: [], forbids: [].}
proc isValid(bag: InputBag): bool {....raises: [], tags: [], forbids: [].}
proc newInputBag(bagType: InputBagType): InputBag {....raises: [], tags: [],
    forbids: [].}
proc validate(bag: InputBag; data: openArray[(string, string)]) {....raises: [
    KeyError, Exception, OpenParserRegexError, ValueError, IOError, OSError],
    tags: [TimeEffect, RootEffect, ReadIOEffect, WriteIOEffect], forbids: [].}
Validate data
proc validate(bag: InputBag; jsonData: JsonNode) {....raises: [KeyError, Exception,
    OpenParserRegexError, ValueError, IOError, OSError],
    tags: [TimeEffect, RootEffect, ReadIOEffect, WriteIOEffect], forbids: [].}
Validate input data using JSON format
proc validateMultipart(bag: InputBag; contentType, multipartBody: sink string) {....raises: [
    MultipartSizeLimitError, Exception, MultipartConfigError, OSError, IOError,
    ValueError, KeyError, OpenParserRegexError], tags: [ReadEnvEffect,
    ReadIOEffect, RootEffect, WriteDirEffect, ReadDirEffect, WriteIOEffect,
    TimeEffect], forbids: [].}
proc validateMultipart(bag: InputBag; contentType: string;
                       data: ptr UncheckedArray[byte]; dataLen: int) {....raises: [
    MultipartSizeLimitError, Exception, MultipartConfigError, OSError, IOError,
    ValueError, KeyError, OpenParserRegexError], tags: [ReadEnvEffect,
    ReadIOEffect, RootEffect, WriteDirEffect, ReadDirEffect, WriteIOEffect,
    TimeEffect], forbids: [].}
proc validateMultipart(bag: InputBag; contentType: string;
                       multipartBody: sink seq[byte]) {....raises: [
    MultipartSizeLimitError, Exception, MultipartConfigError, OSError, IOError,
    ValueError, KeyError, OpenParserRegexError], tags: [ReadEnvEffect,
    ReadIOEffect, RootEffect, WriteDirEffect, ReadDirEffect, WriteIOEffect,
    TimeEffect], forbids: [].}
proc validateMultipartStreamed(bag: InputBag; contentType: string; feeder: proc (
    ms: var MultipartStreamer): bool {.closure.}) {....raises: [
    MultipartConfigError, OSError, IOError, Exception, KeyError,
    OpenParserRegexError, ValueError], tags: [ReadEnvEffect, ReadIOEffect,
    WriteDirEffect, ReadDirEffect, RootEffect, TimeEffect, WriteIOEffect],
    forbids: [].}

Macros

macro multipartBag(data, contentType: typed; rules: untyped;
                   bodySuccess, bodyFail: untyped = nil)

Create a bag from multipart body

data must be a multipart body from a request contentType the string version of Content-Type header

rules is used to define your rules at compile-time.

macro multipartStreamedBag(feeder, contentType: typed; rules: untyped;
                           bodyFail: untyped = nil)
Create a bag from a streaming multipart body using MultipartStreamer. feeder must be a proc(ms: var MultipartStreamer): bool {.closure.} that feeds multipart chunks and returns true to continue or false when done. File validation happens on-the-fly via signature callback.
macro withBag(data: typed; rules: untyped; bodyFail: untyped = nil)

Create a new input bag validation at compile time.

data expects a seq[tuple[k, v: string]] that represent submitted data from the current request.

rules is used to define your rules at compile-time.

Templates

template withValidator(x: typed; r: untyped; b: untyped = nil)
Create a new input bag validation at compile time. This is an alias for withBag macro.