OmYumYum
OmYumYum is a small data validation library.
Install
npm i omyumyum
Examples
import om from 'omyumyum';
const isOptionalNumber = om.number.or.undefined;
isOptionalNumber('1');
// => false
isOptionalNumber(null);
// => false
isOptionalNumber(undefined);
// => true
isOptionalNumber(1);
// => true
om(isOptionalNumber, '1');
// throws TypeError('Expected type "number"')
om(isOptionalNumber, 1);
// Ok
const isNumericArray = om.array.of(om.number);
isNumericArray(['1']);
// => false
isNumericArray([1]);
// => true
om(isNumericArray, [1, 2, '3']);
// Throws TypeError('Expected type "number" at "[2]"').
// Keypath to incorrect data in error message.
const isUserData = om.object.shape({
name: om.string,
age: om.number.or.vacuum // `.or.vacuum` == `.or.null.or.undefined`
});
isUserData({});
// => false
isUserData({ age: 20 })
// => false
isUserData({ name: 'Иванушка' });
// => true
isUserData({ name: 'Иванушка', age: null });
// => true
isUserData({ name: 'Иванушка', age: 20 });
// => true
om(isUserData, { name: 'Иванушка', age: '1' });
// throws TypeError('Expected type "number" at "age"')
const isEmailOrPhone = om.custom(require('is-email')).or.custom(require('is-phone'));
isEmailOrPhone('test@test.test');
// => true
Use and
to combine and improvement types:
const isNonZeroString = om.string.and.custom(minLenght(1));
isNonZeroString('');
// => false
isNonZeroString('1');
// => true
Use previously created validators:
const isImprovedUserData = isUserData.and.object.shape({
friends: om.array.of(isUserData).or.undefined
});
isImprovedUserData({
name: 'Иванушка',
age: 20,
friends: [{ name: 'Алёнушка', age: 18 }]
});
// => true
Use not
for type negation:
const isNotVacuum = om.not.null.and.not.undefined; // == `om.not.vacuum`
isNotVacuum(1);
// => true
isNotVacuum(null);
// => false
isNotVacuum(undefined);
// => false
API
type TValidator = (value: any) => boolean;
interface IType {
(value: any): boolean;
and: ITypes;
or: ITypes;
allow(value: any): IType;
notAllow(value: any): IType;
oneOf(values: Array<any>): IType;
notOneOf(values: Array<any>): IType;
}
-
om(validator: TValidator): (value: any) => true;
Returns true if the value is valid and throws a TypeError otherwise. Example:const isNumber = om.number; om(isNumber, '1'); // throws TypeError('Expected type "number"') let isNumberOrThrow = om(isNumber); isNumberOrThrow('1'); // throws TypeError('Expected type "number"')
-
Return the string as an error message:
const isUserOrNull = om.custom(value => value instanceof User).or.null; isUserOrNull(new User(data)); // => true
Use the object with theconst isLegalAge = om.number.and.custom(age => age >= 18 || 'You are still too small'); om(isLegalAge, 17); // throws TypeError('You are still too small')
message
property as an error message:Use the object with theconst isLegalAge = om.number.and.custom({ validator: gte(18), message: 'You are still too small' }); om(isLegalAge, 17); // throws TypeError('You are still too small')
type
property as the type in an error message:const isType1OrType2 = om.custom({ validator: type1Validator, type: 'type1' }).or.custom({ validator: type2Validator, type: 'type2' }); om(isType1OrType2, type3Value); // throws TypeError('Expected type "type1" or "type2"')
-
-
-
om.null.or.undefined
. -
-
NaN
,Infinity
and-Infinity
.interface INumberType extends IType { lt(value: number): INumberType; less(value: number): INumberType; lte(value: number): INumberType; max(value: number): INumberType; gt(value: number): INumberType; greater(value: number): INumberType; gte(value: number): INumberType; min(value: number): INumberType; inRange(minValue: number, maxValue: number): INumberType; between(minValue: number, maxValue: number): INumberType; positive: INumberType; negative: INumberType; integer: INumberType; }
-
-
om.number.lt()
. -
-
om.number.lte()
. -
-
om.number.gt()
. -
-
om.number.gte()
. -
-
om.number.inRange()
. -
-
-
-
-
interface IStringType extends IType { len(value: number): IStringType; minLen(value: number): IStringType; maxLen(value: number): IStringType; pattern(re: RegExp): IStringType; matches(re: RegExp): IStringType; startsWith(searchString: string, position?: number): IStringType; endsWith(searchString: string, position?: number): IStringType; nonZero: IStringType; nonEmpty: IStringType; }
-
-
-
-
-
om.string.pattern()
. -
-
-
om.string.minLen(1)
. -
om.string.pattern(/\S/)
.
-
-
-
interface IObjectType extends IType { shape(shape: Record<string, TValidator>, exact = false): IObjectType; exactShape(shape: Record<string, TValidator>): IObjectType; keys(re: RegExp): IObjectType; values(validator: TValidator): IObjectType; nonEmpty: IObjectType; }
-
-
interface IArrayType extends IType { of(validator: TValidator): IArrayType; len(value: number): IArrayType; minLen(value: number): IArrayType; maxLen(value: number): IArrayType; nonEmpty: IArrayType; }
-
-
-
om.function
. -
Map
type.interface IMapType extends IType { keys(validator: TValidator): IMapType; values(validator: TValidator): IMapType; nonEmpty: IMapType; }
-
-
Set
type.interface ISetType extends IType { of(validator: TValidator): ISetType; nonEmpty: ISetType; }
-
-
WeakMap
type. -
-
om.weakMap
. -
WeakSet
type. -
-
om.weakSet
. -
Date
type.interface IDateType extends IType { earlier(earlierThanDate: Date | string | number): IDateType; later(laterThanDate: Date | string | number): IDateType; before(beforeDate: Date | string | number): IDateType; after(afterDate: Date | string | number): IDateType; }
-
-
-
om.date.earlier()
. -
om.date.later()
.
-
-
RegExp
type. -
om.regExp
. -
Promise
type. -
Error
type.