Common curried predicates for validating subjects.
$ npm install prettycats --save
Run the specs
$ npm test
const prr = require('prettycats'),
R = require('ramda'),
V = require('nested-validate');
const userType = {
username : prr.isStringOfLengthAtMost(15),
email : prr.isEmail,
age : prr.isNumberBetween(13, 100)
};
const user = {
username : 'dayman',
email : 'charlie@kittenmittons.com',
age : 31
};
// Use for simple type checking
if (prr.objectSatisfies(userType, user)) {
// cool
}
// Or use with a validator
const validateUserOrThrow = V.isObjectOf(userType);
validateUserOrThrow(userType, user);
String → Boolean
expect(prr.isString('foo')).toBe(true);
expect(prr.isString(123)).toBe(false);
Number → String → Boolean
expect(prr.isStringOfLength(5, 'foo')).toBe(false);
expect(prr.isStringOfLength(3, 'foo')).toBe(true);
expect(prr.isStringOfLength(3, 123)).toBe(false);
Number → String → Boolean
expect(prr.isStringOfLengthAtLeast(5, 'foo')).toBe(false);
expect(prr.isStringOfLengthAtLeast(3, 'foo')).toBe(true);
expect(prr.isStringOfLengthAtLeast(3, 'fooo')).toBe(true);
expect(prr.isStringOfLengthAtLeast(3, 123)).toBe(false);
Number → String → Boolean
expect(prr.isStringOfLengthAtMost(5, 'fooooo')).toBe(false);
expect(prr.isStringOfLengthAtMost(3, 'foo')).toBe(true);
expect(prr.isStringOfLengthAtMost(5, 'fooo')).toBe(true);
expect(prr.isStringOfLengthAtMost(3, 123)).toBe(false);
Number → String → Boolean
expect(prr.isStringLongerThan(5, 'foo')).toBe(false);
expect(prr.isStringLongerThan(3, 'foo')).toBe(false);
expect(prr.isStringLongerThan(3, 'fooo')).toBe(true);
expect(prr.isStringLongerThan(3, 123)).toBe(false);
Number → String → Boolean
expect(prr.isStringShorterThan(3, 'fooo')).toBe(false);
expect(prr.isStringShorterThan(3, 'foo')).toBe(false);
expect(prr.isStringShorterThan(5, 'fooo')).toBe(true);
expect(prr.isStringShorterThan(3, 123)).toBe(false);
Number → Number → String → Boolean
expect(prr.isStringOfLengthBetween(5, 9, 'foobar')).toBe(true);
expect(prr.isStringOfLengthBetween(5, 9, 'foo')).toBe(false);
expect(prr.isStringOfLengthBetween(5, 9, 'foobarbazbat')).toBe(false);
expect(prr.isStringOfLengthBetween(3, 5, 123)).toBe(false);
Number → Number → String → Boolean
expect(prr.isStringOfLengthBetweenInclusive(5, 9, 'foobar')).toBe(true);
expect(prr.isStringOfLengthBetweenInclusive(5, 9, 'fooba')).toBe(true);
expect(prr.isStringOfLengthBetweenInclusive(5, 9, 'foobarbaz')).toBe(true);
expect(prr.isStringOfLengthBetweenInclusive(5, 9, 'foo')).toBe(false);
expect(prr.isStringOfLengthBetweenInclusive(5, 9, 'foobarbazbat')).toBe(false);
expect(prr.isStringOfLengthBetweenInclusive(3, 5, 123)).toBe(false);
String → String → Boolean
expect(prr.isStringContaining('bar', 'foobarfoo')).toBe(true);
expect(prr.isStringContaining('bar', 'foobazfoo')).toBe(false);
RegExp → String → Boolean
expect(prr.isStringMatching(/bar/, 'foobarfoo')).toBe(true);
expect(prr.isStringMatching(/bar/, 'foobazfoo')).toBe(false);
Array → String → Boolean
expect(prr.stringIsOneOf(['foo', 'bar', 'baz'], 'foo')).toBe(true);
expect(prr.stringIsOneOf(['foo', 'bar', 'baz'], 'buz')).toBe(false);
expect(prr.stringIsOneOf(['foo', 'bar', 'baz'], 123)).toBe(false);
String → Boolean
expect(prr.isEmail('foo@bar.com')).toBe(true);
expect(prr.isEmail('123@localhost:1337')).toBe(true);
expect(prr.isEmail('foobar.com')).toBe(false);
String → Boolean
expect(prr.isTimestamp('2017-08-24T19:20:30.457Z')).toBe(true);
expect(prr.isTimestamp('2017-08-24T19:20:30.45+01:00')).toBe(true);
expect(prr.isTimestamp('2017-08-24T19:20:30-05:00')).toBe(true);
expect(prr.isTimestamp('2017-08-24T19:20:30Z')).toBe(true);
expect(prr.isTimestamp('Thu Aug 24 2017 19:20:30 GMT-0700 (PDT)')).toBe(false);
expect(prr.isTimestamp('Thu Aug 24 2017')).toBe(false);
expect(prr.isTimestamp(2017)).toBe(false);
String → Boolean
expect(prr.isNumericString('123')).toBe(true);
expect(prr.isNumericString(123)).toBe(false);
expect(prr.isNumericString({ foo : 'bar' })).toBe(false);
expect(prr.isNumericString('asd123asd')).toBe(false);
String → Boolean
expect(prr.isJSON('foo')).toBe(false);
expect(prr.isJSON('{foo:"bar"}')).toBe(false);
expect(prr.isJSON('{}')).toBe(true);
expect(prr.isJSON('{"foo":"bar"}')).toBe(true);
expect(prr.isJson('foo')).toBe(false);
expect(prr.isJson('{foo:"bar"}')).toBe(false);
expect(prr.isJson('{}')).toBe(true);
expect(prr.isJson('{"foo":"bar"}')).toBe(true);
String → Boolean
expect(prr.isUUID('foo')).toBe(false);
expect(prr.isUUID(123)).toBe(false);
expect(prr.isUUID('cd17c371-9468-963b-f3e0bf05e70e')).toBe(false);
expect(prr.isUUID('cd17c371-9468-4baa-963b-f3e0bf05e70e')).toBe(true);
expect(prr.isUuid('foo')).toBe(false);
expect(prr.isUuid(123)).toBe(false);
expect(prr.isUuid('cd17c371-9468-963b-f3e0bf05e70e')).toBe(false);
expect(prr.isUuid('cd17c371-9468-4baa-963b-f3e0bf05e70e')).toBe(true);
Number → Boolean
expect(prr.isNumber(123)).toBe(true);
expect(prr.isNumber('123')).toBe(false);
Number → Boolean
expect(prr.isPositiveNumber(123)).toBe(true);
expect(prr.isPositiveNumber(-123)).toBe(false);
expect(prr.isPositiveNumber(0)).toBe(false);
expect(prr.isPositiveNumber('123')).toBe(false);
Number → Boolean
expect(prr.isNegativeNumber(123)).toBe(false);
expect(prr.isNegativeNumber(-123)).toBe(true);
expect(prr.isNegativeNumber(0)).toBe(false);
expect(prr.isNegativeNumber('123')).toBe(false);
Number → Boolean
expect(prr.isAtLeastZero(123)).toBe(true);
expect(prr.isAtLeastZero(-123)).toBe(false);
expect(prr.isAtLeastZero(0)).toBe(true);
expect(prr.isAtLeastZero('123')).toBe(false);
Number → Boolean
expect(prr.isAtMostZero(123)).toBe(false);
expect(prr.isAtMostZero(-123)).toBe(true);
expect(prr.isAtMostZero(0)).toBe(true);
expect(prr.isAtMostZero('123')).toBe(false);
Number → Boolean
expect(prr.isCalendarMonth(1)).toBe(true);
expect(prr.isCalendarMonth(12)).toBe(true);
expect(prr.isCalendarMonth(0)).toBe(false);
expect(prr.isCalendarMonth(13)).toBe(false);
expect(prr.isCalendarMonth('6')).toBe(false);
Number → Boolean
expect(prr.isCalendarMonthZeroBased(0)).toBe(true);
expect(prr.isCalendarMonthZeroBased(11)).toBe(true);
expect(prr.isCalendarMonthZeroBased(-1)).toBe(false);
expect(prr.isCalendarMonthZeroBased(12)).toBe(false);
expect(prr.isCalendarMonthZeroBased('123')).toBe(false);
Number → Number → Number → Boolean
expect(prr.isNumberBetween(5, 10, 7)).toBe(true);
expect(prr.isNumberBetween(5, 10, 3)).toBe(false);
expect(prr.isNumberBetween(5, 10, 12)).toBe(false);
expect(prr.isNumberBetween(5, 10, '7')).toBe(false);
Number → Number → Number → Boolean
expect(prr.isNumberBetweenInclusive(5, 10, 7)).toBe(true);
expect(prr.isNumberBetweenInclusive(5, 10, 3)).toBe(false);
expect(prr.isNumberBetweenInclusive(5, 10, 12)).toBe(false);
expect(prr.isNumberBetweenInclusive(5, 10, 5)).toBe(true);
expect(prr.isNumberBetweenInclusive(5, 10, 10)).toBe(true);
expect(prr.isNumberBetweenInclusive(5, 10, '7')).toBe(false);
Number → Boolean
expect(prr.isEvenNumber(8)).toBe(true);
expect(prr.isEvenNumber(-8)).toBe(true);
expect(prr.isEvenNumber(5)).toBe(false);
Number → Boolean
expect(prr.isOddNumber(7)).toBe(true);
expect(prr.isOddNumber(-7)).toBe(true);
expect(prr.isOddNumber(8)).toBe(false);
- → Boolean
expect(prr.isNumeric('123')).toBe(true);
expect(prr.isNumeric(123)).toBe(true);
expect(prr.isNumeric({ foo : 'bar' })).toBe(false);
expect(prr.isNumeric('asd123asd')).toBe(false);
Number → Boolean
expect(prr.isNumericBoolean(0)).toBe(true);
expect(prr.isNumericBoolean(1)).toBe(true);
expect(prr.isNumericBoolean(3)).toBe(false);
expect(prr.isNumericBoolean('1')).toBe(false);
Number → Array → Boolean
expect(prr.numberIsOneOf([123, 456, 789], 123)).toBe(true);
expect(prr.numberIsOneOf([12, 34, 56], 123)).toBe(false);
expect(prr.numberIsOneOf([123, 456, 789], '123')).toBe(false);
Array → Boolean
expect(prr.isArray(['foo'])).toBe(true);
expect(prr.isArray(123)).toBe(false);
Array → Boolean
expect(prr.isEmptyArray([])).toBe(true);
expect(prr.isEmptyArray(['foo'])).toBe(false);
expect(prr.isEmptyArray('foo')).toBe(false);
Number → Array → Boolean
expect(prr.isArrayOfLength(5, [1,2,3])).toBe(false);
expect(prr.isArrayOfLength(3, [1,2,3])).toBe(true);
expect(prr.isArrayOfLength(3, 123)).toBe(false);
Number → Array → Boolean
expect(prr.isArrayOfLengthAtLeast(5, [1,2,3])).toBe(false);
expect(prr.isArrayOfLengthAtLeast(3, [1,2,3])).toBe(true);
expect(prr.isArrayOfLengthAtLeast(3, [1,2,3,4])).toBe(true);
expect(prr.isArrayOfLengthAtLeast(3, 123)).toBe(false);
Number → Array → Boolean
expect(prr.isArrayOfLengthAtMost(3, [1,2,3,4])).toBe(false);
expect(prr.isArrayOfLengthAtMost(3, [1,2,3])).toBe(true);
expect(prr.isArrayOfLengthAtMost(5, [1,2,3,4])).toBe(true);
expect(prr.isArrayOfLengthAtMost(3, 123)).toBe(false);
Number → Array → Boolean
expect(prr.isArrayLongerThan(5, [1,2,3])).toBe(false);
expect(prr.isArrayLongerThan(3, [1,2,3])).toBe(false);
expect(prr.isArrayLongerThan(3, [1,2,3,4])).toBe(true);
expect(prr.isArrayLongerThan(3, 123)).toBe(false);
Number → Array → Boolean
expect(prr.isArrayShorterThan(3, [1,2,3,4])).toBe(false);
expect(prr.isArrayShorterThan(3, [1,2,3])).toBe(false);
expect(prr.isArrayShorterThan(5, [1,2,3,4])).toBe(true);
expect(prr.isArrayShorterThan(3, 123)).toBe(false);
String → Array → Boolean
expect(prr.isArrayContaining('bar', ['foo','bar','baz'])).toBe(true);
expect(prr.isArrayContaining('bar', ['foo','baz','buz'])).toBe(false);
Object → Boolean
expect(prr.isObject({ foo : 'bar' })).toBe(true);
expect(prr.isObject(123)).toBe(false);
String → Object → Boolean
expect(prr.isObjectContaining('foo', { foo : 'bar', baz : 'bat' })).toBe(true);
expect(prr.isObjectContaining('foo', { baz : 'bat', buz : 'biz' })).toBe(false);
expect(prr.isObjectContaining('foo', 123)).toBe(false);
String → Object → Boolean
expect(prr.isObjectAbsent('foo', { baz : 'bat', buz : 'biz' })).toBe(true);
expect(prr.isObjectAbsent('foo', { foo : 'bar', buz : 'biz' })).toBe(false);
expect(prr.isObjectAbsent('foo', 123)).toBe(false);
Object → Object → Boolean
expect(prr.isObjectMatching({ foo : 'bar' }, { foo : 'bar' })).toBe(true);
expect(prr.isObjectMatching({ foo : 'bar' }, { baz : 'bat' })).toBe(false);
expect(prr.isObjectMatching({ foo : 'bar' }, 'foo')).toBe(false);
expect(prr.isObjectMatching('foo', { foo : 'bar' })).toBe(false);
Object → Object → Boolean
expect(prr.isObjectExtending({ foo : 'bar' }, { foo : 'bar', baz : 'bat' })).toBe(true);
expect(prr.isObjectExtending({ foo : 'bar' }, { foo : 'bar' })).toBe(true);
expect(prr.isObjectExtending({ foo : 'bar' }, 'foo')).toBe(false);
expect(prr.isObjectExtending('foo', { foo : 'bar' })).toBe(false);
Object → Object → Boolean
const schema = {
foo : v => typeof v === 'string',
bar : v => typeof v === 'number'
};
expect(prr.isObjectSatisfying(schema, { foo : 'hello', bar : 123 })).toBe(true);
expect(prr.isObjectSatisfying(schema, { foo : 'hello' })).toBe(false);
expect(prr.isObjectSatisfying({ foo : 'bar' }, { baz : 'bat' })).toBe(false);
expect(prr.isObjectSatisfying({ foo : 'bar' }, 'foo')).toBe(false);
expect(prr.isObjectSatisfying('foo', { foo : 'bar' })).toBe(false);
Boolean → Boolean
expect(prr.isBoolean(['foo'])).toBe(false);
expect(prr.isBoolean(false)).toBe(true);