Skip to content
This repository has been archived by the owner on Oct 19, 2023. It is now read-only.

Not-throwing validations (ensure)

Latest
Compare
Choose a tag to compare
@ealush ealush released this 17 Aug 12:24
f94c542

If you do not use a validations testing library but still want to use enforce rules, you can use the ensure interface.

Ensure is similar to enforce, only that it returns a boolean value instead of throwing an error.

Since ensure has no way of knowing when you are done adding rules, you have to do that by putting the .test() function last with your value as its argument.

Usage example

For example, if I want to test that the number 4 is both numeric, and is less than 5, I would:

import { ensure } from 'n4s';

ensure()
    .isNumeric()
    .lessThan(5)
    .test(4);   // The value I am testing.
                // `.test()` is always the last function passed

// returns true