Skip to content

Commit c627111

Browse files
committed
docs: document validator combinators
refactor!: soft-deprecate `compose` combinator
1 parent 62cae59 commit c627111

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

docs/pages/docs/components/form.mdx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ n.form(
3838

3939
#### on_submit
4040

41-
<Property
41+
<Property
4242
defaultValue="fn.ignore"
4343
types={['fun(is_valid: boolean): nil']}
4444
/>
4545

4646
#### submit_key
4747

48-
<Property
48+
<Property
4949
defaultValue="<C-CR>"
5050
types={['string[]', 'string']}
5151
/>
@@ -63,4 +63,11 @@ Available validation functions:
6363
- `equals`
6464
- `contains`
6565

66+
Available combinators:
67+
68+
- `all`: ensures all validators pass
69+
- `any`: ensures at least one validator passes
70+
- `none`: ensures no validators pass
71+
- `compose` (deprecated, use `all`)
72+
6673

lua/nui-components/validators.lua

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
local M = {}
22

3+
---@type fun(min: integer): fun(arg: string): boolean
34
function M.min_length(min)
45
return function(arg)
56
return #arg >= min
67
end
78
end
89

10+
---@type fun(arg: string): boolean
911
M.is_not_empty = M.min_length(1)
1012

1113
function M.max_length(max)
@@ -14,32 +16,21 @@ function M.max_length(max)
1416
end
1517
end
1618

19+
---@type fun(pattern: string): fun(arg: string): boolean
1720
function M.contains(pattern)
1821
return function(arg)
19-
return string.find(arg, pattern)
22+
return string.find(arg, pattern) ~= nil
2023
end
2124
end
2225

26+
---@type fun(value: any): fun(arg: any): boolean
2327
function M.equals(value)
2428
return function(arg)
2529
return vim.deep_equal(value, arg)
2630
end
2731
end
2832

29-
function M.compose(...)
30-
local tbl = { ... }
31-
32-
return function(value)
33-
for _, fn in ipairs(tbl) do
34-
if not fn(value) then
35-
return false
36-
end
37-
end
38-
39-
return true
40-
end
41-
end
42-
33+
---@type fun(...: fun(arg: any): boolean): fun(arg: any): boolean
4334
function M.none(...)
4435
local validators = { ... }
4536
return function(value)
@@ -52,6 +43,7 @@ function M.none(...)
5243
end
5344
end
5445

46+
---@type fun(...: fun(arg: any): boolean): fun(arg: any): boolean
5547
function M.any(...)
5648
local validators = { ... }
5749
return function(value)
@@ -64,7 +56,8 @@ function M.any(...)
6456
end
6557
end
6658

67-
function M.all(...)
59+
---@type fun(...: fun(arg: any): boolean): fun(arg: any): boolean
60+
local function all(...)
6861
local validators = { ... }
6962

7063
return function(value)
@@ -78,4 +71,8 @@ function M.all(...)
7871
end
7972
end
8073

74+
M.all = all
75+
76+
M.compose = all
77+
8178
return M

0 commit comments

Comments
 (0)