Перейти к основному содержимому

📦 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 install @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:

PropertyDescriptionRequiredDefault value
mappingThe array contains a name for every argument. Used for remapping arguments and validation schemeNo (Yes if validation used)-
schemaJoi validation schemeNo-
configJoi configNo{ convert: true, allowUnknown: true, abortEarly: false }
errorHandlerError handler for validation error. If not defined, thowen error can handled with bot.catchNo-
allowedUpdatesList of allowed to parse updatesNo['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:

PropertyDescriptionExample
rawRaw arguments array['username', '100']
argsResult remapped object, contains validated / remapped arguments{ username: 'username', balance 100 }

Usage example

Command

TRY NOW
Loading...

Inline query

Loading...

Flowchart

Click to expand

Additional Information