8.0.8 • Published 4 years ago

@lbzg/validate v8.0.8

Weekly downloads
1
License
ISC
Repository
bitbucket
Last release
4 years ago

Intent

Simple and composable data validation.

Installation & Usage

npm i @lbzg/validate

Data validation

const { is } = require('@lbzg/validate')

is.number('number') # F
is.string(1) # F
is.numeric('123') # T
is.array(['q', 'q', 't']) # T
is.nil(undefined) # T
is.plain({x: 1}) # T
is.empty([]) # T 
is.in([1, 2, 3, 4, 5], 5) # T 
is.subset([1, 2, 3, 4, 5], [1, 1, 1, 4]) # T 

is.eq(['q'], ['q']) # T
is.lt(10, 10) # F
is.gt(10, 9) # F

const gt10 = is.gt(10)
gt10(9) # idk

is.prop('id', {id: undefined}) # T
is.propEq('id', 5, {id: 5}) # T
is.propWhen('id', is.gt(10), {id: 11}) # T

const idGt10 = is.propWhen('id', gt10)
idGt10({id: 1}) # F

is.props('id', {id: 1}, {x: 1}) # F
is.propsEq('id', {id: 222}, {id: 222}) # T
is.propsWhen('id', is.gt, {id: 1}, {q: 2}) # T

Validator composition

# all
is.all(
  is.number,
  is.gte(25),
  is.lt(50)
)(27) # T

# any
const isAny = is.any(is.url, is.uuid, is.numeric)
isAny('123') # T
isAny('http://www.google.com') # T

# none
const isNone = is.none(is.url, is.uuid, is.numeric)
isNone('123') # F
isNone('http://www.google.com') # F

# not (complement)
const notAnyIsNone = is.not(isAny)
notAnyIsNone('123') # F
notAnyIsNone('http://www.google.com') # F

More examples below
Tests as examples (github)

Prototype

Is exported under is. Functions with multiple arguments are curried.

Validators

f :: a -> boolean
array(x)
boolean(x)
date(x)
empty(x)
error(x)
function(x)
nil(x) ~ null or undefined
null(x)
number(x)
numeric(x) ~ numeric string or number
object(x)
plain(x) ~ plain object
regex(x)
string(x)
symbol(x)
undefined(x)
value(x) ~ not nil

ipv4(ip)
json(json)
port(port)
url(url)
uuid(uuid)

instance(instance, obj)
type(typeof, x) ~ test typeof

Comparators

f :: a -> b -> boolean
eq(a, b) ~ equals
gt(num, x)
gte(num, x)
lt(num, x)
lte(num, x)

f :: key/value/fn -> a -> boolean
prop(key, obj)
propEq(key, value, obj)
propWhen(key, fn, obj)
fn :: a[key] -> boolean

f :: key/fn -> a -> b -> boolean
props(key, obj, obj2)
propsEq(key, obj, obj2)
propsWhen(key, fn, obj, obj2)
fn :: a[key] -> b[key] -> boolean

Composition

not :: fn -> (...values) -> boolean ~ complement
all :: (...fns) -> (...values) -> boolean ~ all pass
any :: (...fns) -> (...values) -> boolean ~ any pass
none :: (...fns) -> (...values) -> boolean ~ none pass / not(any)

Provide an interface to compose validators with AND (all) / OR (any) logic.

# equivalent expressions
is.any(is.array, is.number, is.string)(x)
is.array(x) || is.number(x) || is.string(x)

Iterators

in(array, x)
subset(superset: any[], subset: any[])

inAny(array, fn, x) ~ array.some
inAll(array, fn, x) ~ array.every
fn :: el -> x -> boolean

# equivalent expressions ~ x is greather than every element
is.inAll(array, is.gt)(x) 
array.every(el => is.gt(el, x))

Example

Iterator validation example ~ validate item before insertion.
Rules: Item id not in blacklist, item not in data, instance of SomeClass.

const input = [...] # item objects
const blacklist = [...] # blacklisted id's
const data = [...] # item objects

const valid = is.all(
  is.instance(SomeClass),
  is.none(
    is.inAny(blacklist, is.propEq('id')), # propEq(key, value, object)
    is.inAny(data, is.propsEq('id')), # propsEq(key, object, object2)
  ),
)

const addIfValid = x => { if (valid(x)) data.push(x) }
const add = input => input.forEach(addIfValid)

add(input)
8.0.8

4 years ago

8.0.7

5 years ago

8.0.6

5 years ago

8.0.5

5 years ago

8.0.4

5 years ago

8.0.3

5 years ago

8.0.2

5 years ago

8.0.1

5 years ago

8.0.0

5 years ago

7.0.4

5 years ago

7.0.3

5 years ago

7.0.2

5 years ago

7.0.1

5 years ago

7.0.0

5 years ago

6.5.1

5 years ago

6.5.0

5 years ago

6.4.0

5 years ago

6.3.0

5 years ago

6.2.1

5 years ago

6.2.0

5 years ago

6.1.0

5 years ago

6.0.1

5 years ago

6.0.0

5 years ago

5.0.1

5 years ago

5.0.0

5 years ago

4.0.3

5 years ago

4.0.2

5 years ago

4.0.1

5 years ago

4.0.0

5 years ago

3.1.4

5 years ago

3.1.3

5 years ago

3.1.2

5 years ago

3.1.1

5 years ago

3.1.0

5 years ago

2.6.1

5 years ago

2.6.0

5 years ago

2.5.2

5 years ago

2.5.1

5 years ago

2.4.1

5 years ago

2.4.0

5 years ago

2.3.0

5 years ago

2.2.4

5 years ago

2.2.3

5 years ago

2.2.2

5 years ago

2.2.1

5 years ago

2.2.0

5 years ago

2.1.0

5 years ago

2.0.4

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago