Skip to content

Commit

Permalink
support setting label to false for params_shape field to hide the err…
Browse files Browse the repository at this point in the history
…or message label entirely
  • Loading branch information
leafo committed Feb 29, 2024
1 parent ca15f3f commit 1bbd811
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/input_validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ $options_table{
},
{
name = "label",
description = "A prefix to be used in place of the field name when generating an error message"
description = "A prefix to be used in place of the field name when generating an error message. If `false` is provided, then the error will not be prefixed by the field name."
},
{
name = "as",
Expand Down
9 changes: 7 additions & 2 deletions lapis/validate/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,12 @@ do
if validation.error then
table.insert(errors, validation.error)
else
local error_prefix = tostring(validation.label or validation.field) .. ": "
local error_prefix
if validation.label == false then
error_prefix = ""
else
error_prefix = tostring(validation.label or validation.field) .. ": "
end
if self.error_prefix then
error_prefix = tostring(self.error_prefix) .. ": " .. tostring(error_prefix)
end
Expand Down Expand Up @@ -202,7 +207,7 @@ do
(types.string + types.number):tag("field"),
is_base_type:describe("tableshape type"):tag("type"),
error = types["nil"] + types.string:tag("error"),
label = types["nil"] + types.string:tag("label"),
label = types["nil"] + (types.string + types.literal(false)):tag("label"),
as = types["nil"] + types.string:tag("as")
}), {
format_error = function(self, val, err)
Expand Down
8 changes: 6 additions & 2 deletions lapis/validate/types.moon
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class ParamsShapeType extends BaseType
is_base_type\describe("tableshape type")\tag "type"

error: types.nil + types.string\tag "error"
label: types.nil + types.string\tag "label"
label: types.nil + (types.string + types.literal(false))\tag "label"
as: types.nil + types.string\tag "as"
}), format_error: (val, err) => "params_shape: Invalid validation specification object: #{err}"

Expand Down Expand Up @@ -82,7 +82,11 @@ class ParamsShapeType extends BaseType
if validation.error -- override error message
table.insert errors, validation.error
else
error_prefix = "#{validation.label or validation.field}: "
error_prefix = if validation.label == false
""
else
"#{validation.label or validation.field}: "

if @error_prefix
error_prefix = "#{@error_prefix}: #{error_prefix}"

Expand Down
31 changes: 31 additions & 0 deletions spec/validate_spec.moon
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,37 @@ describe "lapis.validate.types", ->
it "tests object with state", ->
-- TODO:

it "test labels", ->
t = types.params_shape {
{"name", label: false, types.string}
{"inner", label: false, types.params_shape {
{"one", label: false, types.string}
{"two", types.string}
}}
}

_, errors = t\transform { }

assert.same {
[[expected type "string", got "nil"]]
[[params: expected type "table", got "nil"]]
}, errors

_, errors = t\transform {
name: false
inner: {
one: 23892
two: 23892
}
}

assert.same {
[[expected type "string", got "boolean"]]
[[expected type "string", got "number"]]
[[two: expected type "string", got "number"]]
}, errors


it "test nested validate", ->
test_object = types.params_shape {
{"alpha", types.one_of {"one", "two"} }
Expand Down

0 comments on commit 1bbd811

Please sign in to comment.