checks icon indicating copy to clipboard operation
checks copied to clipboard

Allow optional fields in table

Open aensidhe opened this issue 2 years ago • 0 comments

Introduce special property for table: __checks__skip__omitted__. If it's set to true, 'table' check will allow not fail check on table if it has more properties than we pass to checks.

Reason:

There's lot of legacy code that needs proper parameters checks, but 'table' is too strict because it doesn't allow additional fields. Correcting everything is very time consuming and, sometimes, doesn't make sense. Proposed check will allow gradual changes in code from "no checks at all" to "everything is checked".

Sample usage:

local function f(arg1)
    checks({ a = 'string', b = 'number', __checks__skip__omitted = true})
end

f({ 'a' = 'abc', b = 2 }) -- ok
f({ 'a' = 'abc', b = 2, c = '3' }) -- also ok
f({ 'a' = 123, b = 2, c = '3' }) -- not ok
f({ 'a' = 'abc', b = '2', c = '3' }) -- not ok

aensidhe avatar Dec 21 '23 12:12 aensidhe