Sleitnick / Knit

Lightweight game framework for Roblox
https://sleitnick.github.io/Knit/
MIT License
566 stars 93 forks source link

Add a way to get function name for Middleware #200

Closed recanman closed 2 years ago

recanman commented 2 years ago

Currently, it is hard to validate inputs for my services without making classes for functions that use the same set of arguments.

If the function name being executed is added to middleware, that would certainly improve my workflow since I would be able to easily work with validation of input and separate it from the actual function itself (so the top of each function isn't just a chain of if not x then return end, or similar).

An example that uses the function name:

local function validate(plr, args, name)
    local validation = service.Validation[name]

    -- Do validation here
end

local knit = require(path.to.knit)
local service = knit.CreateService({Name = "Test", Middleware = validate})

service.Validation = {}

service.Validation["TakeInput"] = {"number", "string"}
function service.Client:TakeInput(plr, num, str)

end
OverHash commented 2 years ago

I think using something like t inside your TakeInput function makes the most sense. If you want to use a middleware layer, there's no reason why you couldn't build this already.

local validators = {
    ["TakeInput"] = t.tuple(t.number, t.string)
}

local service = Knit.CreateService({
    Name = "Test",
    Middleware = { Inbound = { createValidatorMiddleware(validators) } }
})
recanman commented 2 years ago

Didn't know about this. Thanks for linking the resource!