Repository: neovim/neovim
File: runtime/lua/vim/shared.lua (Lua)

Code before:
  --- @nodoc
  --- @class vim.validate.Spec
  --- @field [1] any Argument value
  --- @field [2] vim.validate.Validator Argument validator
  --- @field [3]? boolean|string Optional flag or error message

  local function is_type(val, t)
    return type(val) == t or (t == 'callable' and vim.is_callable(val))
  end

  --- @param param_name string
  --- @param val any
  --- @param validator vim.validate.Validator
  --- @param message? string
  --- @param allow_alias? boolean Allow short type names: 'n', 's', 't', 'b', 'f', 'c'
  --- @return string?
  local function is_valid(param_name, val, validator, message, allow_alias)
    if type(validator) == 'string' then
      local expected = allow_alias and type_aliases[validator] or validator

      if not expected then
        return string.format('invalid type name: %s', validator)
      end

      if not is_type(val, expected) then
        message = message and message or expected
        return string.format('%s: expected %s, got %s', param_name, message, type(val))
      end
    elseif vim.is_callable(validator) then
      -- Check user-provided validation function
      local valid, opt_msg = validator(val)
      if not valid then
        local err_msg =
          string.format('%s: expected %s, got %s', param_name, message or '?', tostring(val))

        if opt_msg then
          err_msg = string.format('%s. Info: %s', err_msg, opt_msg)
        end

        return err_msg
      end
    elseif type(validator) == 'table' then
      for _, t in ipairs(validator) do
        local expected = allow_alias and type_aliases[t] or t
        if not expected then
          return string.format('invalid type name: %s', t)
        end

        if is_type(val, expected) then
          return -- success
        end

Review comment (suggestion):
```suggestion
        message = message or expected
```

Code after:
  --- | fun(v:any):boolean, string?

  local type_aliases = {
    b = 'boolean',
    c = 'callable',
    f = 'function',
    n = 'number',
    s = 'string',
    t = 'table',
  }

  --- @nodoc
  --- @class vim.validate.Spec
  --- @field [1] any Argument value
  --- @field [2] vim.validate.Validator Argument validator
  --- @field [3]? boolean|string Optional flag or error message

  local function is_type(val, t)
    return type(val) == t or (t == 'callable' and vim.is_callable(val))
  end

  --- @param param_name string
  --- @param val any
  --- @param validator vim.validate.Validator
  --- @param message? string "Expected" message
  --- @param allow_alias? boolean Allow short type names: 'n', 's', 't', 'b', 'f', 'c'
  --- @return string?
  local function is_valid(param_name, val, validator, message, allow_alias)
    if type(validator) == 'string' then
      local expected = allow_alias and type_aliases[validator] or validator

      if not expected then
        return string.format('invalid type name: %s', validator)
      end

      if not is_type(val, expected) then
        return ('%s: expected %s, got %s'):format(param_name, message or expected, type(val))
      end
    elseif vim.is_callable(validator) then
      -- Check user-provided validation function
      local valid, opt_msg = validator(val)
      if not valid then
        local err_msg = ('%s: expected %s, got %s'):format(
          param_name,
          message or '?',
          tostring(val)
        )
        err_msg = opt_msg and ('%s. Info: %s'):format(err_msg, opt_msg) or err_msg

        return err_msg
      end