Provides a way to easily combine Folktale Validations
npm install @justinc/combine-validationscombine-validations [![Build status][travis-image]][travis-url] [![NPM version][version-image]][version-url] [![Semantic Release][semantic-release-image]][semantic-release-url] [![Js Standard Style][standard-image]][standard-url]Provides a way to easily combine Folktale Validations.
As of v2, the Validation ADT is passed in as the first argument. This gives back a function which works the same as the main function exported by this package in v1.
This removes this package's dependency on data.validation and allows you to work with the Validation in Folktale 2 and beyond.
npm i @justinc/combine-validations
(See tests for more example usage)
``js
const Validation = require('folktale/data/validation')
const combineV = require('@justinc/combine-validations')
const { Success, Failure } = Validation
const combineValidations = combineV(Validation)
function noStartsWith (prefix) {
return arg => arg.startsWith(prefix)
// use new Error instead of String in Failure if you want stack tracePrefix Error: '${arg}' starts with '${prefix}'
? Failure([ ])
: Success('prefix ok')
}
function noEndsWith (suffix) {
return arg => arg.endsWith(suffix)
// use new Error instead of String in Failure if you want stack traceSuffix Error: '${arg}' ends with '${suffix}'
? Failure([ ])
: Success('suffix ok')
}
const input = 'hello world'
const v1 = combineValidations([
noStartsWith(' ')(input),
noEndsWith('?')(input)
])
const v2 = combineValidations([
noStartsWith('h')(input),
noEndsWith('?')(input)
])
const v3 = combineValidations([
noStartsWith('h')(input),
noEndsWith('world')(input)
])
const v4 = combineValidations()
console.log({v1, v2, v3, v4})
/*
{ v1: Validation { value: [ 'prefix ok', 'suffix ok' ] },
v2:
Validation {
value: [ 'Prefix Error: \'hello world\' starts with \'h\'' ] },
v3:
Validation {
value:
[ 'Prefix Error: \'hello world\' starts with \'h\'',
'Suffix Error: \'hello world\' ends with \'world\'' ] },
v4: Validation { value: [] } }
*/
``
* @justinc/combine-validations
* [~combineValidations([Validation], [iterableOfValidations])](#module_@justinc/combine-validations..combineValidations) ⇒ Validation
* ~Validation : Object
Kind: inner method of @justinc/combine-validations
Returns: Validation - The combined Validations as a single Validation
See: Validation
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| [Validation] | Validation | | The Validation ADT to use |
| [iterableOfValidations] | Iterable.<Validation> | [] | The validations to combine |
Kind: inner typedef of @justinc/combine-validations
[travis-image]: https://img.shields.io/travis/justin-calleja/combine-validations.svg?style=flat-square
[travis-url]: https://travis-ci.org/justin-calleja/combine-validations
[version-image]: https://img.shields.io/npm/v/@justinc/combine-validations.svg?style=flat-square
[version-url]: https://npmjs.org/package/@justinc/combine-validations
[semantic-release-image]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=flat-square
[semantic-release-url]: https://github.com/semantic-release/semantic-release
[standard-image]: https://img.shields.io/badge/code-standard-yellow.svg?style=flat-square
[standard-url]: https://github.com/feross/standard