Skip to content

Commit

Permalink
support numeric indicies for params_shape
Browse files Browse the repository at this point in the history
  • Loading branch information
leafo committed Oct 2, 2023
1 parent b4e53cd commit d11adee
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lapis/validate/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ do
test_input_type = types.table
is_base_type = instance_of(BaseType)
param_validator_spec = types.annotate(types.shape({
types.string:tag("field"),
(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"),
Expand Down
2 changes: 1 addition & 1 deletion lapis/validate/types.moon
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ParamsShapeType extends BaseType
is_base_type = instance_of BaseType

param_validator_spec = types.annotate types.shape({
types.string\tag "field"
(types.string + types.number)\tag "field"

-- TODO: AssertErrorType should be unwrapped so we don't yield error when processing nested object
is_base_type\describe("tableshape type")\tag "type"
Expand Down
27 changes: 27 additions & 0 deletions spec/validate_spec.moon
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,33 @@ params type {
}
}

it "numeric indicies", ->
t = types.params_shape {
{1, types.string}
{2, types.number}
}

assert.same {"tuple", 200}, (t\transform { "tuple", 200 })
assert.same {"tuple", 200}, (t\transform { "tuple", 200, "extra", things: "true" })

assert.same {
nil, {
[[2: expected type "number", got "string"]]
}
}, {
t\transform { "one", "two" }
}

assert.same {
nil, {
[[1: expected type "string", got "boolean"]]
[[2: expected type "number", got "boolean"]]
}
}, {
t\transform { false, true, "give" }
}


describe "params_array", ->
types = require "lapis.validate.types"

Expand Down

0 comments on commit d11adee

Please sign in to comment.