Scrutiny
An async validator, inspired by ReactJS PropTypes.
Installation
$ npm install scrutiny
Requirements
Scrutiny has a Promise
based API, and needs a global Promise
object to function. Promises are natively available from Node.js v0.11.13
onwards.
If you don't have a global Promise
object, or don't want to rely on the native implementation (which is known to be slow), you can alternatively specify bluebird
, Q
or any Promises/A+ compliant promise library to use,
Scrutiny;
Usage
To use scrutiny in your projects, you need to require the node module first.
var Scrutiny = ;
Scrutiny uses checks
for validation, which are just simple functions. They can be asynchronous functions returning a promise, or plain synchronous functions.
Validating values,
var scrutiny = ; scrutiny;
When an error occurs, all the inbuilt checks return an instance of Scrutiny.Error
, so you can verify that the Error
was in fact a validation error, and not some other error.
Tip: You can check if an error is Scrutiny.Error
either with instanceof
or the name
property of the error object.
Inbuilt checks
scrutinychecksany // matches anythingscrutinychecksundef // matches undefined using typeofscrutinychecksstring // matches strings using typeofscrutinychecksbool // matches booleans using typeofscrutinychecksnumber // matches numbers, doesn't match NaNscrutinychecksfunc // matches functions using typeofscrutinychecksarray // matches arrays using Array.isArrayscrutinychecksobject // matches objects, doesn't match null
Inbuilt helpers
// Value is limited to specific valuesscrutinychecks // An object with property values of a certain typescrutinychecks // Value could be one of many typesscrutinychecks // An object taking on a particular shapescrutinychecksshape name: scrutinychecksstring salary: scrutinychecksnumber
Custom checks
Scrutiny is not of much use without custom checks. Adding your own custom checks are easy.
Checks can be of 2 types, synchronous or asynchronous. Synchronous checks can throw errors if check failed, and asynchronous checks can return a promise which resolves if check passed, and rejects with error if check failed.
var scrutiny = ; // Synchronous validatorscrutiny; // Async validatorscrutiny; scrutiny;
While you can just throw/reject with plain Error
objects in checks
, it's highly recommended that you throw Scrutiny.Error
instead, so that errors which aren't validation errors don't go unnoticed.
Custom helpers
Helpers are functions which take some parameters and return a check
. Useful when you want to pass some parameters to your checks.
scrutiny; scrutiny;
async
functions
Using with ES2016 Scrutiny works great with ES2016 async
/await
syntax, and the code becomes a lot more simpler.
try await scrutiny; // do something with the input value catch error if error instanceof ScrutinyError // handle error in validation else // handle other errors
Source code
You can get the latest source code from the github page.
git clone https://github.com/satya164/scrutiny.git
Bugs and feature requests
Please submit bugs and feature requests here. Pull requests are always welcome.
Pull requests must follow the .editorconfig
settings and pass eslint
validation.