Async applies to the Validator to make it work asynchronously. It was designed to work with Coercer as well, but has not been tested with it.
¯_(ツ)_/¯
import create from 'single-schema/lib';
import Validator from 'single-schema/lib/flatteners/validate';
import Async from 'single-schema/lib/metaFlatteners/async';
const flatteners = {
validate: Async(Validator()),
};
const { combine, array, map, and, maybe } = create(flatteners);
The leaves work in the same way as the Validator (Coercer), but they can be async. Note that invalid leaves are still resolved - do not reject invalid results.
const stringAsync = {
validate: value => new Promise(resolve =>
setTimeout(() => resolve(typeof value == 'string'
? null
: 'Expected string!'
), 10)
),
};
Operators all work like they would with Validator (Coercer), but the leaves can be async.
/* returns: Promise<{
key2: IS_STRING_ERROR,
}>
*/
validate({
key1: 'hello',
key2: 123,
});