|
| 1 | +local M = {} |
| 2 | + |
| 3 | +-- Code taken from @MariaSolOs in a indent-blankline.nvim PR: |
| 4 | +-- https://github.com/lukas-reineke/indent-blankline.nvim/pull/934/files#diff-09ebcaa8c75cd1e92d25640e377ab261cfecaf8351c9689173fd36c2d0c23d94R16 |
| 5 | + |
| 6 | +--- @param spec table<string, {[1]:any, [2]:function|string, [3]:string|true|nil}> |
| 7 | +--- NOTE: We disable some Lua diagnostics here since lua_ls isn't smart enough to |
| 8 | +--- realize that we're using an overloaded function. |
| 9 | +local _validate = function(spec) |
| 10 | + for key, key_spec in pairs(spec) do |
| 11 | + local message = type(key_spec[3]) == "string" and key_spec[3] or nil --[[@as string?]] |
| 12 | + local optional = type(key_spec[3]) == "boolean" and key_spec[3] or nil --[[@as boolean?]] |
| 13 | + ---@diagnostic disable-next-line:param-type-mismatch, redundant-parameter |
| 14 | + vim.validate(key, key_spec[1], key_spec[2], optional, message) |
| 15 | + end |
| 16 | +end |
| 17 | + |
| 18 | +--- @param path string The path to the field being validated |
| 19 | +--- @param tbl table The table to validate |
| 20 | +--- @param source table The original table that we're validating against |
| 21 | +--- @see vim.validate |
| 22 | +function M.validate(path, tbl, source) |
| 23 | + -- Validate |
| 24 | + local _, err = pcall(_validate, tbl) |
| 25 | + if err then |
| 26 | + error(path .. "." .. err) |
| 27 | + end |
| 28 | + |
| 29 | + -- Check for erroneous fields |
| 30 | + for k, _ in pairs(source) do |
| 31 | + if tbl[k] == nil then |
| 32 | + error(path .. "." .. k .. ": unexpected field found in configuration") |
| 33 | + end |
| 34 | + end |
| 35 | +end |
| 36 | + |
| 37 | +return M |
0 commit comments