1.0.0 • Published 8 years ago

@justinc/all-included-validator v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
8 years ago

all-included-validator Build status NPM version License Semantic Release Js Standard Style

Install

npm i @justinc/all-included-validator

Demo

(look at tests for more examples)

// NOTE: run  `node repl.js` to see this in action:
const allIncluded = require('@justinc/all-included-validator')

const ids = [1, 2, 3, 4, 5]
const refs = [1, 2]
const nums = [2, 4, 2, 3, 1]
const over9000s = [9001, 9002]

const allIncludedInIds = allIncluded(ids)
const allIncludedInOver9000s = allIncluded(over9000s)

console.log(allIncludedInIds(refs)('The following refs are not in ids: '))
// [ true, [] ]

console.log(allIncludedInIds(nums)())
// [true, [] ]

const allOver9000sIncludedInIds = allIncludedInIds(over9000s)
console.log(allOver9000sIncludedInIds('The following are not in ids: '))
// [ false, [ new Error('The following are not in ids: 9001, 9002') ]]

console.log(allOver9000sIncludedInIds('Ids knows not of these: '))
// [ false, [ new Error('Ids knows not of these: 9001, 9002') ]]

console.log(allIncludedInOver9000s(ids)(`These ids aren't even over 9000: `))
// [ false, [ new Error(`These ids aren't even over 9000: : 1, 2, 3, 4, 5`) ]]

combine-validations

If you're OK with using Folktale Validation, you might be interested in combine-validations.

To use this package with combine-validations you will need to convert its output Tuple<Boolean, Array<Error>> to a Folktale Validation with something like this:

const Validation = require('data.validation')
const allIncluded = require('@justinc/all-included-validator')
const combineValidations = require('@justinc/combine-validations')

const { Success, Failure } = Validation

const asValidation = ([isValid, errors]) => isValid ? Success(true) : Failure(errors)

const ids = ['node1', 'node2', 'node3']
const dependencies = [ 'node1', 'node3' ]
const validation = combineValidations([
  asValidation(allIncluded(ids)(dependencies)())
])

Tutorials

TODO: A JSDoc 3 tutorial (re currying) needs to be injected in this README.md. For now there's only a link:

Modules

@justinc/all-included-validator

@justinc/all-included-validator~allIncluded(iterableA, iterableB, errMsgPrefix) ⇒ Tuple.<Boolean, Array.<Error>>

This function is curried. Checks that all elements in the given 2nd iterable are members of the given 1st iterable.

Kind: inner method of @justinc/all-included-validator
See: Tuple

ParamTypeDefaultDescription
iterableAIterable[]The 1st iterable
iterableBIterable[]The 2nd iterable, whose elements should all be in the 1st iterable
errMsgPrefixString''A string to prefix the error message (the err msg is any found 2nd iterable elems which are not in the 1st iterable)

@justinc/jsdocs

This module houses JSDoc 3 type definitions which can be re-used in different packages.

@justinc/jsdocs.Tuple : Array

The type Tuple is an Array of fixed length whose elements at specific indices are of the specified types.

Kind: static typedef of @justinc/jsdocs
Example

// A Tuple of arity (length) 2, whose first el is a Boolean and second el is
// an Array of Error:
Tuple<Boolean, Array<Error>>
// e.g. [ true, [] ]
// e.g. [ false, [ new Error('computer says no') ] ]

Example

// A Tuple of arity (length) 3, whose first el is a String, and both the
// second and third els are an Array of String:
Tuple<String, Array<String>, Array<String>>
// e.g. [ 'hello', [ 'world' ], [ 'goodbye', 'world' ] ]