Getting started

How does Validatem work?

Validatem is a little different from most other validation libraries. In those, you often need to either chain a lot of method calls, or specify a bunch of strings to define the validation rules. But those approaches have a big limitation: you're pretty much expected to use the validators that come with the library, and adding your own more precise validation rules is a hassle. So in practice, you usually won't bother, and you end up with very inconsistent validation.

This also happens in other ways; the library itself is usually designed very monolithically. It expects you to use it in a particular way, for a particular purpose, and anything else is painful to do. A form validation library only validates form input. A configuration file validator only validates configuration files. A CLI argument validator only validates CLI arguments. And so on. And each of these have a completely different syntax, and a different set of validation rules they can apply.

Validatem, instead, uses regular functions as its validators. There is nothing special about these - an 'official' validator works the exact same way as one from a third-party package, or one in your own code. They're all equally easy to use. I think this is important, because it encourages people to share their work and collaborate on a much bigger commons of off-the-shelf validators, instead of being limited to what the maintainers of a library thought to put into it. Of course you need to actually find these validators, and so you can find a categorized index here.

Validatem makes very few assumptions about how you use it, too; it provides a basic set of validation functions, that all take the same kind of validation schema and produce structured validation errors. You can integrate it into whatever you need it for, whether that's validating form data, configuration files, or something completely different. And the core is small enough that you can totally use it in a library to validate function arguments!

Okay, so how do I use it?

For most cases, you'll end up using one of these functions, all of them exported from @validatem/core:

The "schema" here is simply a validator, and that validator can either implement a single rule or combine multiple other rules, making it a "combinator". For example, the following are some examples of valid schemas:

1
2
3
isString
arrayOf(isBoolean)
{ oneProperty: [ required, isString ], anotherProperty: isNumber }

When you use an object literal in a schema directly, it is automatically converted to a hasShape validator internally; this is what you would typically use to validate a nested object structure. When you use an array literal in a schema directly, it simply means that multiple validators are applied in sequence - [ isString ] and isString mean exactly the same thing.

Here is an example of what a real-world validation schema might look like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const { validateArguments } = require("@validatem/core");
const required = require("@validatem/required");
const defaultTo = require("@validatem/default-to");
const isString = require("@validatem/is-string");
const isBoolean = require("@validatem/is-boolean");
const isInteger = require("@validatem/is-integer");

let validatedArguments = validateArguments(arguments, {
    /* Yes, these arguments are positional, even though we're using object syntax; this works because objects in JS are ordered! */
    name: [ required, isString ],
    properties: [ required, {
        activated: [ required, isBoolean ],
        suspended: [ defaultTo(false), isBoolean ],
        infractions: isInteger
    }]
});

In this example, we're validating arguments to a function call. The first argument is a required name argument, which must be a string; the second argument is an object containing a number of different properties, one of which is also required to be specified, and one of which has a default value specified.

Look closely at how for the properties argument, the schema contains an object literal and a required validator. By default, validators in Validatem - including combinators like hasShape! - allow undefined and null values, and those will skip all the validators entirely, since it doesn't make sense to validate a value that isn't there.

To disallow that for any given value, whether it's a function argument or an object property or anything else, you specify the required validator, which will fail if it encounters a null or undefined. You can also choose to use defaultTo to specify a default value for that case.


As validation libraries go, Validatem is a little different. For example, unlike Joi (where every validator is a chained method) or checkit (where every validator is a string name), a validator in Validatem is just a function that receives the to-be-validated value as an argument. That function can do a few different things:

Importantly, you can write these functions yourself, and custom validators - whether written by you or by someone else - are on equal footing with the 'official' validators. They are just as easy to use, and just as easy to combine. In fact, all of the official validators are packaged as if they were third-party packages!

Validating a value or a function's arguments, then, is just a matter of calling one of the validation functions with the desired validation schema. Either it will pass (and return the validated value, optionally transformed), or it will fail and throw an AggregrateValidationError with a clear description of what's wrong:

TODO: Insert error example here

There are a number of different validation functions included in @validatem/core:

Additionally, some more validation utility functions are available as separate packages:

Transforming values

TODO: explain why defaultTo etc. are useful, self-documenting

Combining (composing) validators

TODO: show array syntax TODO: show why wrapError can be useful

TODO: explain how linters can interfere? TODO: explain how either can be used like a match statement, like is done in raqb with wrapping