π¦ Arguments
This plugin allows you to easily parse and validate command / inline query arguments with Joi
Plugin works with inline query, channel posts and messages
Installing a pluginβ
- NPM
- Yarn
- pnpm
npm install @opengram/arguments
yarn add @opengram/arguments
pnpm add @opengram/arguments
How to useβ
You need to create new Joi validation schema and pass it to arguments
plugin with
errorHandler
and mapping
array.
Configurationβ
Accepted fields:
Property | Description | Required | Default value |
---|---|---|---|
mapping | The array contains a name for every argument. Used for remapping arguments and validation scheme | No (Yes if validation used) | - |
schema | Joi validation scheme | No | - |
config | Joi config | No | { convert: true, allowUnknown: true, abortEarly: false } |
errorHandler | Error handler for validation error. If not defined, thowen error can handled with bot.catch | No | - |
allowedUpdates | List of allowed to parse updates | No | ['message', 'channel_post', 'inline_query'] |
Mappingβ
This array shows for plugin how to remap argument index to argument name and used for validation with Joi
.
For example, if you declare an object like this:
['firstarg', 'secondarg']
It means first argument be mapped to firstarg
and second mapped to secondarg
in result
ctx.state.args
object
Error handlingβ
When arguments validation fails, and errorHandler
not declared in options
, plugin throws original joi
error,
which you can handle with bot.catch
handler. But this is not very convenient,
So you can use the errorHandler
to register a handler to be called on an error with an error object and
the current context, you can use the context object to e.g. to tell user about an error
Result objectβ
You can get plugin result in ctx.state.args
, that object contains properties, showed in table:
Property | Description | Example |
---|---|---|
raw | Raw arguments array | ['username', '100'] |
args | Result remapped object, contains validated / remapped arguments | { username: 'username', balance 100 } |
Usage exampleβ
Commandβ
- Validation & mapping
- Parsing & mapping
- Only parsing
Inline queryβ
- Validation & mapping
- Parsing & mapping
- Only parsing
Loading...
Loading...
Loading...