@foobarhq/validators
The main purpose of this library is (but not limited) to parse and validate data received from HTTP requests.
It achieves this goal by providing a set of composable functions designed to easily build complex input validators.
Usage
Install from NPM: npm install @foobarhq/validators
import { numberValidator } from '@foobarhq/validators';
const validate = numberValidator();
validate('0xff');
validate('123');
validate(12);
validate('foo');
Validators
stringValidator
Validates strings. It only accepts strings as inputs.
import { stringValidator } from '@foobarhq/validators';
const validate = stringValidator({
trim: true,
minLength: 0,
maxLength: Number.POSITIVE_INFINITY,
allowNull: false,
defaultValue: undefined,
});
validate(' 123 ');
numberValidator
Validates numbers. It accepts strings and numbers as inputs.
import { numberValidator } from '@foobarhq/validators';
const validate = numberValidator({
unsigned: false,
integer: false,
allowInfinite: false,
min: -Infinity,
max: +Infinity,
allowNull: false,
defaultValue: undefined,
});
validate('Infinity');
validate('-Infinity');
validate('10.5');
validate(456);
booleanValidator
Parses booleans. It accepts strings and booleans as inputs.
import { booleanValidator } from '@foobarhq/validators';
const validate = booleanValidator({
allowNull: false,
defaultValue: undefined,
});
validate('t');
validate('f');
validate('true');
validate(false);
dateValidator
Parses dates. It accepts strings and native Date objects as inputs.
import { dateValidator } from '@foobarhq/validators';
const validate = dateValidator({
allowNull: false,
defaultValue: undefined,
});
validate('2011-10-05T14:48:00.000Z');
validate(new Date('2011-10-05T14:48:00.000Z'));
validate('true');
enumValidator
Restricts its input to a set of values.
import { enumValidator } from '@foobarhq/validators';
const validate = enumValidator([1, 2, 3], {
allowNull: false,
defaultValue: undefined,
});
validate(1);
validate(3);
validate(4);
const validate = enumValidator({
ITEM_1: 1,
ITEM_2: 'banana',
});
validate(1);
validate('ITEM_1');
validate('ITEM_2');
noValidate
The noop of validators, doesn't parse, doesn't validate, just returns its input.
import { noValidate } from '@foobarhq/validators';
const validate = noValidate();
validate(1);
validate(3);
validate(' 1_2_3 ');
arrayValidator
A composable validator which validates an array and its items.
import { arrayValidator, numberValidator } from '@foobarhq/validators';
const validate = arrayValidator({
allowNull: false,
defaultValue: undefined,
min: 0,
max: Number.POSITIVE_INFINITY,
strict: false,
unique: false,
});
validate(1);
validate([1, 2, 3]);
const validate = arrayValidator(numberValidator(), {
strict: true,
});
validate(1);
validate(['0x00', '0b0', '0', 0]);
structValidator
Validates an object structure. Accepts simple objects as inputs.
import { structValidator, numberValidator } from '@foobarhq/validators';
const validate = structValidator({
item1: numberValidator(),
}, {
allowNull: false,
defaultValue: undefined,
});
validate({});
validate({ item1: '10' })
const validate = structValidator({
item1: numberValidator({ defaultValue: 10 }),
}, {
allowNull: false,
defaultValue: undefined,
});
validate({});
validate({ item1: '5' })