Skip to content

Commit 6b34f10

Browse files
committed
make spec.optional check explicitly for nil
1 parent d6355d3 commit 6b34f10

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

spec.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,11 @@ end
122122
---@return fun(value: any): boolean
123123
function M.optional(spec)
124124
return function(value)
125-
if value then
126-
return spec(value)
125+
-- if no value is specified return true!
126+
if value == nil then
127+
return true
127128
end
128-
return true
129+
return spec(value)
129130
end
130131
end
131132

spec_test.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ describe("spec.lua", function()
4747
assert.True(spec.optional(spec.string) "Hello, World")
4848
assert.True(spec.optional(spec.string)(nil))
4949
assert.False(spec.optional(spec.string)(true))
50+
assert.False(spec.optional(spec.string)(false))
5051
end)
5152

5253
it("spec.all_of", function()

0 commit comments

Comments
 (0)