All files / platform/packages/validator/providers ValidatorProvider.js

100% Statements 30/30
100% Branches 0/0
100% Functions 11/11
100% Lines 27/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129                      1x 1x                           418x 1x                           1x 406x 406x                           1x                     1x 1x 1x                               1x 1x                   1x 1x 67x 67x             1x 4x   4x 8x 8x     4x 4x           1x 1x       1x  
'use strict';
 
/*
 * adonis-validator
 *
 * (c) Harminder Virk <virk@adonisjs.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
 
const { ServiceProvider } = require('@adonisjs/fold');
const _ = require('lodash');
 
class ValidationProvider extends ServiceProvider {
  /**
   * Register the validator to the IoC container
   * with `Adonis/Addons/Validator` namespace.
   *
   * @method _registerValidator
   *
   * @return {void}
   *
   * @private
   */
  _registerValidator() {
    this.app.bind('Adonis/Addons/Validator', () => require('../src/Validator'));
    this.app.alias('Adonis/Addons/Validator', 'Validator');
  }
 
  /**
   * Register the middleware to the IoC container
   * with `Adonis/Middleware/Validator` namespace
   *
   * @method _registerMiddleware
   *
   * @return {void}
   *
   * @private
   */
  _registerMiddleware() {
    this.app.bind('Adonis/Middleware/Validator', (app) => {
      const MiddlewareValidator = require('../src/Middleware/Validator');
      return new MiddlewareValidator(app.use('Adonis/Addons/Validator'));
    });
  }
 
  /**
   * Register the `make:validator` command to the IoC container
   *
   * @method _registerCommands
   *
   * @return {void}
   *
   * @private
   */
  _registerCommands() {
    this.app.bind('Adonis/Commands/Make:Validator', () => require('../commands/MakeValidator'));
  }
 
  /**
   * Register bindings
   *
   * @method register
   *
   * @return {void}
   */
  register() {
    this._registerValidator();
    this._registerMiddleware();
    this._registerCommands();
  }
 
  /**
   * On boot
   *
   * @method boot
   *
   * @return {void}
   */
  boot() {
    /**
     * Define a named middleware with server
     *
     * @type {String}
     */
    const Server = this.app.use('Adonis/Src/Server');
    Server.registerNamed({
      av: 'Adonis/Middleware/Validator',
    });
 
    /**
     * Extend route class by adding a macro, which pushes a
     * middleware to the route middleware stack and
     * validates the request via validator
     * class
     */
    const Route = this.app.use('Adonis/Src/Route');
    Route.Route.macro('validator', function (validatorClass) {
      this.middleware([`av:${validatorClass}`]);
      return this;
    });
 
    /**
     * Adding resource macro to apply validator on
     * route resource
     */
    Route.RouteResource.macro('validator', function (validatorsMap) {
      const middlewareMap = new Map();
 
      for (const [routeNames, validators] of validatorsMap) {
        const middleware = _.castArray(validators).map((validator) => `av:${validator}`);
        middlewareMap.set(routeNames, middleware);
      }
 
      this.middleware(middlewareMap);
      return this;
    });
 
    /**
     * Register command with ace.
     */
    const ace = require('@adonisjs/ace');
    ace.addCommand('Adonis/Commands/Make:Validator');
  }
}
 
module.exports = ValidationProvider;