Getting started
How does Validatem work?
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:
- Return with nothing: Consider the value valid, and don't transform it.
- Return with a new value: Consider the value valid, but transform it to something else (eg. a parsed version).
- Throw or return a ValidationError: Consider the value invalid; the message of the error is what shows up in the validation results.
- Throw with a different kind of error: Indicate that something is wrong (eg. there is a bug in the code); this error will be passed through as-is, and validation will be aborted.
- Return a validationResult object: This can be done to return multiple errors, or a combination of errors and a new value. Unless you're writing a combinator, you probably don't need this.
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 validateValue
, validateArguments
, or validateOptions
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
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