1.0.5 • Published 6 years ago
obcheck v1.0.5
obcheck
Overview
This is a tiny utility for checking if an object contains a profile of values. It is pretty basic and not too smart so your usecase may warrant a look into validate.js instead.
Usage
The validator method is imported and passed a configuration object like so:
import { Obcheck } from 'obcheck' // or require()
const config = {
whitelists: { a: ['b'] }
}
const validator = new Obcheck(config)
const target = { a: 'c' }
validator.validate(target)
This will return
{
whitelists: {
a: {
isValid: false,
value: 'c'
}
}
}
One may also run custom validator logic or apply sanitizer functions to an object's values. In this case, the above code's config would look like this:
import { Obcheck, mark } from 'obcheck' // or require()
const config = {
whitelists: { a: ['b'] },
sanitizers: {
a: (val) => return val.toUpperCase()
}
validators: {
isMoreThanOneChar: (obj) => isMoreThanOneChar(obj.a)
}
}
const isMoreThanOneChar = (str) => {
if (str.length > 1) {
return mark(true, str.length)
}
return mark(false, str.length)
}
As you can see, a sanitizer's key in the config refers to it's location in the target object. One may target deep keys with strings like so:
const config = {
whitelists: { 'a.b.c.d.e.f.g': ['h', 'i'] },
}