Home Explore Blog CI



neovim

32th chunk of `runtime/doc/lua.txt`
8bbf19db2e9a3584e71ac26749d9b4dc1115aaf62ad7109a0000000100000fd2
 {name} with value {value} satisfies
       {validator}. If {optional} is given and is `true`, then {value} may be
       `nil`. If {message} is given, then it is used as the expected type in
       the error message.
       Example: >lua
         function vim.startswith(s, prefix)
          vim.validate('s', s, 'string')
          vim.validate('prefix', prefix, 'string')
          -- ...
        end
<
    2. `vim.validate(spec)` (deprecated) where `spec` is of type
       `table<string,[value:any, validator: vim.validate.Validator, optional_or_msg? : boolean|string]>)`
       Validates a argument specification. Specs are evaluated in alphanumeric
       order, until the first failure.
       Example: >lua
         function user.new(name, age, hobbies)
          vim.validate{
            name={name, 'string'},
            age={age, 'number'},
            hobbies={hobbies, 'table'},
          }
          -- ...
        end
<

    Examples with explicit argument values (can be run directly): >lua
        vim.validate('arg1', {'foo'}, 'table')
           --> NOP (success)
        vim.validate('arg2', 'foo', 'string')
           --> NOP (success)

        vim.validate('arg1', 1, 'table')
           --> error('arg1: expected table, got number')

        vim.validate('arg1', 3, function(a) return (a % 2) == 0 end, 'even number')
           --> error('arg1: expected even number, got 3')
<

    If multiple types are valid they can be given as a list. >lua
        vim.validate('arg1', {'foo'}, {'table', 'string'})
        vim.validate('arg2', 'foo', {'table', 'string'})
        -- NOP (success)

        vim.validate('arg1', 1, {'string', 'table'})
        -- error('arg1: expected string|table, got number')
<

    Note: ~
      • `validator` set to a value returned by |lua-type()| provides the best
        performance.

    Parameters: ~
      • {name}       (`string`) Argument name
      • {value}      (`any`) Argument value
      • {validator}  (`vim.validate.Validator`)
                     • (`string|string[]`): Any value that can be returned
                       from |lua-type()| in addition to `'callable'`:
                       `'boolean'`, `'callable'`, `'function'`, `'nil'`,
                       `'number'`, `'string'`, `'table'`, `'thread'`,
                       `'userdata'`.
                     • (`fun(val:any): boolean, string?`) A function that
                       returns a boolean and an optional string message.
      • {optional}   (`boolean?`) Argument is optional (may be omitted)
      • {message}    (`string?`) message when validation fails


==============================================================================
Lua module: vim.loader                                            *vim.loader*

vim.loader.enable({enable})                              *vim.loader.enable()*
    WARNING: This feature is experimental/unstable.

    Enables or disables the experimental Lua module loader:

    Enable (`enable=true`):
    • overrides |loadfile()|
    • adds the Lua loader using the byte-compilation cache
    • adds the libs loader
    • removes the default Nvim loader

    Disable (`enable=false`):
    • removes the loaders
    • adds the default Nvim loader

    Parameters: ~
      • {enable}  (`boolean?`) true/nil to enable, false to disable

vim.loader.find({modname}, {opts})                         *vim.loader.find()*
    WARNING: This feature is experimental/unstable.

    Finds Lua modules for the given module name.

    Parameters: ~
      • {modname}  (`string`) Module name, or `"*"` to find the top-level
                   modules instead
      • {opts}     (`table?`) Options for finding a module:
                   • {rtp}? (`boolean`, default: `true`) Search for modname in
                     the runtime path.
                   • {paths}? (`string[]`, default: `{}`) Extra paths to
                     search for modname
                   • {patterns}? (`string[]`, default:
                     `{"/init.lua",

Title: Lua API: Argument Validation Details and Experimental Module Loader
Summary
This section provides a detailed explanation of `vim.validate()`, including its two forms: validating individual arguments and validating argument specifications. It includes examples of both successful and failing validations. Additionally, it covers the experimental `vim.loader` module, which allows enabling/disabling and finding Lua modules using custom paths and patterns.