joi-async
DO NOT USE YET!
Async validation support for Joi. Adds mixins to Joi object to allow custom async validation callbacks.
Usage
;// or you can use// import Joi from 'joi-async';// ... or via require()// require('joi-async');// const Joi = require('joi'); const checkUsernameIsUnique = { return { ; };}; async { const schema = Joiobject; try const filteredValues = await schema; catch e console; /* [ { "message": "The username \"taken\" has already been taken", "path": [ "username" ], "type": "customAsync.error", "context": { "value": "taken", "key": "username", "label": "username" } } ] */ };
Methods
any.async(callback)
callback
- callback function can be synchronous or Promise-based
Joi.asyncValidate(value, schema, [options], [callback])
Same rules as Joi.validate() but with extra options
any.validate(value, [options], [callback])
Same rules as any.validate() but with extra options
Options
afterSyncSuccess
: defaults totrue
. All callbacks will be called only after regular synchronous flow is successful.
Callbacks
Callbacks can return Promise or be synchronous. A callback receives the following arguments:
value
- the value being processed by Joi.state
- an object containing the current context of validation.key
- the key of the current value.path
- the full path of the current value.parent
- the potential parent of the current value.
options
- options object provided through any().options() or Joi.validate().
If callback returns a value it will transform original value in the output in case if
{ convert: true }
is used.
Errors
You can use Promise.reject('error template')
or throw 'error template'
.
However, if you need full control over resulting error details you can use JoiAsyncError
to override errorCode
as well
JoiAsyncError(message[, errorCode])
message
- error message template, same rules as regular Joi error messages. (e.g.!!Example error message label: {{label}}, value: {{!value}}
)errorCode
- defaults tocustomAsync.error
;; const checkUsernameIsUnique = { throw '!!taken' 'error.taken';}; async { const schema = Joiobject; try await schema; catch e console; /* [ { "message": "taken", "path": [ "username" ], "type": "error.taken", // new code "context": { "value": "taken", "key": "username", "label": "username" } } ] */ };
Examples
Async with overriding
; const removeBadWords = async { const withoutBadWords = await ; // Will replace original value if {convert: true} option return withoutBadWords;}; async { const schema = Joiobject; try const filteredValues = await schema; catch e // standard Joi error object };
Full example
; const checkUsername = async { const available = await ; if !available throw '!!This username has already been taken'; // No need to return anything if you don't want to override it}; async { const schema = Joiobject; try const filteredValues = await schema; catch e // standard Joi error object };
Synchronous example
;; const kebabify = { // works only with { convert: true } return _;}; async { const schema = Joiobject; try const filteredValues = await schema; console; // example-message catch e // standard Joi error object };