daruk-validate
    TypeScript icon, indicating that this package has built-in type declarations

    1.0.5 • Public • Published

    daruk-validate

    parameter validate middleware for daruk, powered by koa-parameter.

    Installation

    $ npm install daruk-validate --save

    Usage

    import { DarukServer, controller, get } from "daruk";
    import { validate } from "daruk-validate";
    
    (async () => {
      const myapp = DarukServer({
        middlewareOrder: ["daruk-validate"],
      });
    
      @controller()
      class Index {
        @get("/")
        @validate({
          name: {
            type: 'string',
            required: true,
          },
        })
        public async index(ctx: any) {
          ctx.body = "hello world";
        }
      }
    
      await myapp.loadFile("../middleware");
      await myapp.binding();
      myapp.listen(3000);
    })();

    Checkout parameter to get all the rules.

    Daruk Middleware

    Daruk Middleware click

    import { Daruk, defineMiddleware, MiddlewareClass } from "daruk";
    import { parameter } from "daruk-validate";
    
    @defineMiddleware("daruk-validate")
    class DarukValidate implements MiddlewareClass {
      public initMiddleware(daruk: Daruk) {
        // If there is no global error intercept
        // You can use this middleware to catch errors
        return parameter(daruk.app);
      }
    }

    Translate

    You can override the translate method of parameter to implement I18n, by passing a function like this :

    import { Daruk, defineMiddleware, MiddlewareClass } from "daruk";
    import { parameter } from "daruk-validate";
    
    @defineMiddleware("daruk-validate")
    class DarukValidate implements MiddlewareClass {
      public initMiddleware(daruk: Daruk) {
        parameter(daruk.app, function() {
          // Same example with node-parameter
          var args = Array.prototype.slice.call(arguments);
          // Assume there have I18n.t method for convert language.
          return I18n.t.apply(I18n, args);
        }});
      }
    }

    Error

    import { Daruk, defineMiddleware, MiddlewareClass } from "daruk";
    import { errorVerify } from "daruk-validate";
    
    @defineMiddleware("error")
    class DarukValidate implements MiddlewareClass {
      public initMiddleware(daruk: Daruk) {
        // Create a global capture middleware
        return errorVerify;
      }
    }

    Example

    License

    MIT

    Keywords

    none

    Install

    npm i daruk-validate

    DownloadsWeekly Downloads

    14

    Version

    1.0.5

    License

    MIT

    Unpacked Size

    6.75 kB

    Total Files

    6

    Last publish

    Collaborators

    • zuigzm