What is ow?
The 'ow' npm package is a powerful and expressive validation library for Node.js. It allows developers to validate arguments and inputs in a concise and readable manner. 'ow' provides a variety of built-in validators and supports custom validation logic, making it a versatile tool for ensuring data integrity in applications.
What are ow's main functionalities?
Basic Type Validation
This feature allows you to validate the type of a variable. In this example, the function `validateString` checks if the input is a string. If the input is not a string, an error is thrown.
const ow = require('ow');
const validateString = (input) => {
ow(input, ow.string);
};
validateString('Hello, World!'); // Passes
validateString(123); // Throws an error
Complex Object Validation
This feature allows you to validate the shape and properties of an object. In this example, the function `validateUser` checks if the user object has a `name` property that is a string with a minimum length of 3 and an `age` property that is a positive integer.
const ow = require('ow');
const validateUser = (user) => {
ow(user, ow.object.exactShape({
name: ow.string.minLength(3),
age: ow.number.integer.positive
}));
};
validateUser({ name: 'Alice', age: 25 }); // Passes
validateUser({ name: 'Al', age: -5 }); // Throws an error
Custom Validation
This feature allows you to create custom validation logic. In this example, the function `validateEvenNumber` checks if the input is an even number. If the input is not an even number, an error is thrown with a custom message.
const ow = require('ow');
const validateEvenNumber = (input) => {
ow(input, ow.number.validate(n => ({
validator: n % 2 === 0,
message: 'Expected an even number'
})));
};
validateEvenNumber(4); // Passes
validateEvenNumber(3); // Throws an error
Other packages similar to ow
joi
Joi is a powerful schema description language and data validator for JavaScript. It allows you to create blueprints or schemas for JavaScript objects to ensure validation of key information. Compared to 'ow', Joi offers a more extensive API and is widely used in the industry for complex validation tasks.
yup
Yup is a JavaScript schema builder for value parsing and validation. It is similar to Joi but is often preferred for its simplicity and ease of use, especially in React applications. Yup provides a fluent API for object schema validation and is highly customizable.
validator
Validator is a library of string validators and sanitizers. It is less comprehensive than 'ow' but is highly efficient for validating and sanitizing strings. Validator is often used for simple validation tasks such as checking email formats, URLs, and other string-based validations.
Argument type validation
View documentation
Install
$ npm install ow
Usage
import ow from 'ow';
const unicorn = input => {
ow(input, ow.string.minLength(5));
…
};
unicorn(3);
unicorn('yo');
API
ow(value, predicate)
Test if value
matches the provided predicate
.
ow.create(predicate)
Create a reusable validator.
const checkPassword = ow.create(ow.string.minLength(6));
checkPassword('foo');
ow.any(...predicate[])
Returns a predicate that verifies if the value matches at least one of the given predicates.
ow('foo', ow.any(ow.string.maxLength(3), ow.number));
ow.{type}
All the below types return a predicate. Every predicate has some extra operators that you can use to test the value even more fine-grained.
Primitives
Built-in types
Typed arrays
Structured data
Miscellaneous
Predicates
The following predicates are available on every type.
not
Inverts the following predicates.
m(1, m.number.not.infinite);
m('', m.string.not.empty);
is(fn)
Use a custom validation function. Return true
if the value matches the validation, return false
if it doesn't.
m(1, m.number.is(x => x < 10));
m(1, m.number.is(x => x > 10));
Instead of returning false
, you can also return a custom error message which results in a failure.
const greaterThan = (max: number, x: number) => {
return x > max || `Expected \`${x}\` to be greater than \`${max}\``;
};
m(5, m.number.is(x => greaterThan(10, x)));
Maintainers
Logo
Logo is based on Comic Book Elements by Carl Eriksson.
License
MIT