bungle / lua-resty-validation

Validation Library (Input Validation and Filtering) for Lua and OpenResty.
BSD 2-Clause "Simplified" License
155 stars 25 forks source link

multiple cascade validators #6

Open jBernavaPrah opened 6 years ago

jBernavaPrah commented 6 years ago

Hi!

How can I have the fields in original format when multiple new validator are created?

Ex:

--- zone_config.lua
local validation = require "resty.validation"

local http2 = validation.new {
    value = validation:optional('on'):oneof('on', 'off'):string(),
    modified_at = validation:optional(0):number()
}

-- First we create single validators for each form field
return validation.new {
    http2 = http2,
}
--- test.lua
local config_validator = require('zone_config')
local cjson = require('cjson')

local valid, fields, errors = config_validator {
     http2 = { value = nil , modified_at = nil }
}

print(cjson.encode(fields()))

--- ASPECTED:
--- {http2={value='on',modified_at=0}}

--- RESULT:
--- {"http2":{"value":{"value":"on","invalid":false,"validated":true,"unvalidated":false,"valid":true,"name":"value"},"modified_at":{"value":0,"invalid":false,"validated":true,"unvalidated":false,"valid":true,"name":"modified_at"}}}
jBernavaPrah commented 6 years ago

My proposal:


--- line: 559-569 of resty/validation.lua

       if valid and field.valid then
            data[index] = istype('callable')(field.value) and field.value() or field.value
        elseif invalid and field.invalid then
            data[index] = istype('callable')(field.value) and field.value() or field.value
        elseif validated and field.validated then
            data[index] = istype('callable')(field.value) and field.value() or field.value
        elseif unvalidated and field.unvalidated then
            data[index] = istype('callable')(field.value) and field.value() or field.value
        end