From 62cae595459f3c430acf354a4814386a19299490 Mon Sep 17 00:00:00 2001 From: Will Hopkins Date: Thu, 28 Mar 2024 16:51:39 -0700 Subject: [PATCH 1/2] feat: add validator combinators for forms --- lua/nui-components/validators.lua | 38 +++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/lua/nui-components/validators.lua b/lua/nui-components/validators.lua index d2f73a9..86a3098 100644 --- a/lua/nui-components/validators.lua +++ b/lua/nui-components/validators.lua @@ -40,4 +40,42 @@ function M.compose(...) end end +function M.none(...) + local validators = { ... } + return function(value) + for _, fn in ipairs(validators) do + if fn(value) then + return false + end + end + return true + end +end + +function M.any(...) + local validators = { ... } + return function(value) + for _, fn in ipairs(validators) do + if fn(value) then + return true + end + end + return false + end +end + +function M.all(...) + local validators = { ... } + + return function(value) + for _, fn in ipairs(validators) do + if not fn(value) then + return false + end + end + + return true + end +end + return M From c627111a658b458118ebc7f6cb32f69505751dab Mon Sep 17 00:00:00 2001 From: Will Hopkins Date: Thu, 28 Mar 2024 16:54:54 -0700 Subject: [PATCH 2/2] docs: document validator combinators refactor!: soft-deprecate `compose` combinator --- docs/pages/docs/components/form.mdx | 11 +++++++++-- lua/nui-components/validators.lua | 29 +++++++++++++---------------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/docs/pages/docs/components/form.mdx b/docs/pages/docs/components/form.mdx index 0562162..fbecb66 100644 --- a/docs/pages/docs/components/form.mdx +++ b/docs/pages/docs/components/form.mdx @@ -38,14 +38,14 @@ n.form( #### on_submit - #### submit_key - @@ -63,4 +63,11 @@ Available validation functions: - `equals` - `contains` +Available combinators: + +- `all`: ensures all validators pass +- `any`: ensures at least one validator passes +- `none`: ensures no validators pass +- `compose` (deprecated, use `all`) + diff --git a/lua/nui-components/validators.lua b/lua/nui-components/validators.lua index 86a3098..0c159c3 100644 --- a/lua/nui-components/validators.lua +++ b/lua/nui-components/validators.lua @@ -1,11 +1,13 @@ local M = {} +---@type fun(min: integer): fun(arg: string): boolean function M.min_length(min) return function(arg) return #arg >= min end end +---@type fun(arg: string): boolean M.is_not_empty = M.min_length(1) function M.max_length(max) @@ -14,32 +16,21 @@ function M.max_length(max) end end +---@type fun(pattern: string): fun(arg: string): boolean function M.contains(pattern) return function(arg) - return string.find(arg, pattern) + return string.find(arg, pattern) ~= nil end end +---@type fun(value: any): fun(arg: any): boolean function M.equals(value) return function(arg) return vim.deep_equal(value, arg) end end -function M.compose(...) - local tbl = { ... } - - return function(value) - for _, fn in ipairs(tbl) do - if not fn(value) then - return false - end - end - - return true - end -end - +---@type fun(...: fun(arg: any): boolean): fun(arg: any): boolean function M.none(...) local validators = { ... } return function(value) @@ -52,6 +43,7 @@ function M.none(...) end end +---@type fun(...: fun(arg: any): boolean): fun(arg: any): boolean function M.any(...) local validators = { ... } return function(value) @@ -64,7 +56,8 @@ function M.any(...) end end -function M.all(...) +---@type fun(...: fun(arg: any): boolean): fun(arg: any): boolean +local function all(...) local validators = { ... } return function(value) @@ -78,4 +71,8 @@ function M.all(...) end end +M.all = all + +M.compose = all + return M