@se-oss/is is an elegant and type-safe value validation library for TypeScript and JavaScript.
npm install @se-oss/isInstall using your favorite package manager
pnpm
pnpm install @se-oss/isyarn
yarn add @se-oss/isimport is from '@se-oss/is';
is.string('β');
//=> true
is.number(42);
//=> true
is.array([1, 2, 3]);
//=> true
is.urlInstance(new URL('https://example.com'));
//=> trueimport is from '@se-oss/is';
is('hello');
//=> 'string'
is(new Map());
//=> 'Map'
is(new Uint8Array());
//=> 'Uint8Array'import is from '@se-oss/is';
const value: unknown = 'β';
if (is.string(value)) {
console.log(value.length);
//=> 2
}import is from '@se-oss/is';
// Check if any of the predicates match
is.any([is.string, is.number], 'hello');
//=> true
// Check if all of the predicates match
is.all([is.array, is.nonEmptyArray], [1, 2, 3]);
//=> true
// Check if a value is optional
is.optional(undefined, is.string);
//=> true
// Use as predicate factories
const isStringOrNumber = is.any([is.string, is.number]);
isStringOrNumber('hello');
//=> trueimport is from '@se-oss/is';
// Validate array elements
is.array([1, 2, 3], is.number);
//=> true
// Check if a number is in range
is.inRange(3, [0, 5]);
//=> true
// Check for plain objects
is.plainObject({ a: 1 });
//=> trueimport is from '@se-oss/is';
is.json('{"a":1}');
//=> true
is.base64('SGVsbG8gd29ybGQ=');
//=> true
is.ipv4('127.0.0.1');
//=> true
is.hexColor('#ffffff');
//=> trueFor all configuration options, please see the API docs.
Want to contribute? Awesome! To show your support is to star the project, or to raise issues on GitHub
Thanks again for your support, it is much appreciated! π
MIT Β© Shahrad Elahi and contributors.