Skip to content

Optional file_upload field with with_params validation #816

@secjoa

Description

@secjoa

Description:

I'm using the with_params validation system extensively, but I'm having trouble declaring a file_upload field as optional.

Lapis provides a types.file_upload type, and I expected to be able to combine it with types.empty to allow optional uploads. However, validation fails whenever no file is submitted in the form.

local lapis = require "lapis"
local capture_errors_json = require("lapis.application").capture_errors_json
local with_params = require("lapis.validate").with_params

local app = lapis.Application()

app:post("/send_file", capture_errors_json(with_params({
  {"file", types.empty + types.file_upload}
}, function(self, params)
  print("ok")
end)))

return app

In this case, validation does not pass if the file field is not present or empty.

Expected behavior:

The validation should pass when:

  • No file is uploaded (empty field), OR
  • A valid file upload is provided

Actual behavior:

Validation fails when the file field is empty or not included in the request.

Questions:

What is the correct way to declare an optional file_upload field using with_params?
Is there a recommended pattern or workaround for this use case?

Additional context:

Reference to file_upload type implementation:

file_upload = types.partial({
filename: types.string * -empty
content: -types.literal("")
})\describe "file upload"

Thanks in advance!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions