OmYumYum is a small data validation library.
npm i omyumyum
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
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;
}
-
Alternative signature:
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"')
-
Uses a custom validator. Example:
om.custom(validator: ((value: any) => boolean | string) | { validator: TValidator, message?: string, type?: string }): IType;
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"')
- Matches a null.
- Matches an undefined.
-
Same as
om.null.or.undefined
. - Matches a boolean data type.
-
Matches a number data type except
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; }
- Number must be less than the specified value.
-
Alias for
om.number.lt()
. - Number must be less than or equal to the specified value.
-
Alias for
om.number.lte()
. - Number must be greater than the specified value.
-
Alias for
om.number.gt()
. - Number must be greater than or equal to the specified value.
-
Alias for
om.number.gte()
. - Number must be in the specified range.
-
Alias for
om.number.inRange()
. - Number must be positive.
- Number must be negative.
- Number must be an integer.
-
Matches a string data type.
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; }
- Length of string must be equal to the specified value.
- Length of string must be greater than or equal to the specified value.
- Length of string must be less than or equal to the specified value.
- String must match the specified regular expression.
-
Alias for
om.string.pattern()
. - String must begin with the specified substring.
- String must end with the specified substring.
-
Same as
om.string.minLen(1)
. -
Same as
om.string.pattern(/\S/)
.
- Matches a symbol data type.
-
Matches an object data type.
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; }
- Object must match the specified shape.
- Object must exactly match the specified shape.
- Object keys must match the specified regular expression.
- Object values must match the specified validator.
- Object must have at least one property of its own.
-
Matches an array data type.
interface IArrayType extends IType { of(validator: TValidator): IArrayType; len(value: number): IArrayType; minLen(value: number): IArrayType; maxLen(value: number): IArrayType; nonEmpty: IArrayType; }
- Array values must match the specified validator.
- Length of array must be equal to the specified value.
- Length of array must be greater than or equal to the specified value.
- Length of array must be less than or equal to the specified value.
- Array must not be empty.
- Matches a function type.
-
Alias for
om.function
. -
Matches a
Map
type.interface IMapType extends IType { keys(validator: TValidator): IMapType; values(validator: TValidator): IMapType; nonEmpty: IMapType; }
- Map keys must match the specified validator.
- Map values must match the specified validator.
- Map must not be empty.
-
Matches a
Set
type.interface ISetType extends IType { of(validator: TValidator): ISetType; nonEmpty: ISetType; }
- Set values must match the specified validator.
- Set must not be empty.
-
Matches a
WeakMap
type.- WeakMap keys must match the specified validator.
- WeakMap values must match the specified validator.
- WeakMap must not be empty.
-
Alias for
om.weakMap
. -
Matches a
WeakSet
type.- WeakSet values must match the specified validator.
- WeakSet must not be empty.
-
Alias for
om.weakSet
. -
Matches a
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; }
- Date is earlier than the specified date.
- Date is later than the specified date.
-
Alias for
om.date.earlier()
. -
Alias for
om.date.later()
.
-
Matches a
RegExp
type. -
Alias for
om.regExp
. -
Matches a
Promise
type. -
Matches a
Error
type.