The input validator

One of the key features of a thorin app is the way you validate your input data by using sanitizers. Since sanitizers are pure synchronous functions, given an input value it will check if it's valid or not, and if it is, return the validated version.

So, the dispatcher.validate() function will return a a ThorinIntentValidator instance that you can use to specify additional information about your input set. The validator's functionality is available below.

validatorObj.default(val)
If the value of an input is not valid, it will set the specified val value in the intent's input value for the failed key (see example below)
  • valany the default value to use when an incoming validation fails
validatorObj.error(err)
If the value of an input is not valid, use a custom error to return to the client and override the default INPUT.NOT_VALID error.
  • errthorin.error instance the error to send back to the client.
If you call the function as validatorObj.error('errorCode, 'errorMessage', statusCode), the validator will automatically create the error instance for you.
validatorObj.promise(fn)
Used for asynchronous validations, the callback function will be called with the input value and expect a promise to be returned. Once the promise is fulfilled, the call stack resumes.
  • fnfunction the function to call when validating the given input field. Should return a promise
A short example can be viewed below.
'use strict'
const dispatcher = thorin.dispatcher;
dispatcher
   .addAction('some.action')
   .input({
      name: dispatcher
               .validate('STRING', {min: 2})
               .default('John')
               .promise((inputName) => {
                  log.info('Todo some async operation');
                  return Promise.resolve('Doe');
               });
   })
   .use((intentObj) => {
      log.info(intentObj.input('name'));  // should output Doe
      intentObj.send():
   });
validatorObj.callback(done)
Similar to the above promise(), used for asynchronous validations, the callback function will be called with the input value and a done function that should be called with the standard (err, result)
  • donefunction the callback function to use as the async validator.

The dispatcher will automatically handle input data when used in an action or a middleware context. However, you can also use thorin to validate data outside the scope of an action or middleware, by using a special sanitization function found under thorin.sanitize.

thorin.sanitize(name, input, opt, defaultValue)
Apply the given validator on the input data with the given options. Based on the sanitizer used, it will either return the sanitized version of the input, the default value given or null
  • namestring the sanitizer name that will be used
  • inputany the input value we want to verify
  • optobject additional options that we will pass to the sanitizer
  • defaultValueany the default value to use should the validation fails.
Inline sanitization example
'use strict'
thorin.sanitize('STRING', 'myString'); // => 'myString'
thorin.sanitize('NUMBER', '30'); // => 30
thorin.sanitize('IP', 'a.b.c.d', '127.0.0.1'); // => '127.0.0.1'
Middleware or action input sanitization example
'use strict';
const dispatcher = thorin.dispatcher;
dispatcher
   .addAction('my.action')
   .input({
      name: dispatcher.validate('STRING', {min: 3, max: 20}).default('John'),
      age: dispatcher.validate('NUMBER', {min: 13}).error('INVALID.AGE', 'Please provide a valid age', 400),
      gender: dispatcher.validate('ENUM', ['male', 'female']).error('INVALID.GENDER', 'Please select a gender')
   });
Do you have a question or is something missing?

You can always create a new issue on GitHub or contact one of the core founders by chat.