Skip to content

Latest commit

 

History

History
47 lines (37 loc) · 1.03 KB

README.md

File metadata and controls

47 lines (37 loc) · 1.03 KB

Async

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.

¯_(ツ)_/¯

Setup

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);

Leaves

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

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,
});