@xpresser/abolish
TypeScript icon, indicating that this package has built-in type declarations

2.3.1 • Public • Published

Xpresser Validator (Abolish)

This plugins extends xpresser's RequestEngine to provide different validation methods that abolish supports.

Installation

npm install @xpresser/abolish
# or
yarn add @xpresser/abolish

Add to your project

Add the plugin to your plugins.json file:

{
  "npm://@xpresser/abolish": true
}

Add Types (For TypeScript)

Add to the bottom your xpresser.d.ts file:

import "@xpresser/abolish/xpresser";

Import Plugin Config

Note: Make sure you have xjs-cli installed.

xjs import abolish configs

This should create a configs/abolish.(ts|js) file. where you can extend Abolish and configure it.

Http Methods

These methods are available in the http object.

  • http.validate - Validate objects on the fly.
  • http.validateAsync - Async version of http.validate.
  • http.validateQuery - Validate query object with defined rules.
  • http.validateQueryAsync - Async version of http.validateQuery.
  • http.validateBody - Validate body object with defined rules.
  • http.validateBodyAsync - Async version of http.validateBody.
  • http.validatedBody - Returns validated body set by Abolish Middleware.

If you are conversant with Abolish you should be able to understand how the methods mentioned above works.

For example if we have a controller action.

export = {
    async index(http) {
        // `http.validate`
        const [error, validatedData] = http.validate(someObject, rules);
        // `http.validateAsync`
        const [error2, validatedData2] = await http.validateAsync(someObject, rules);
        
        // `http.validateQuery`
        const [error3, validatedData3] = http.validateQuery(rules);
        // `http.validateQueryAsync`
        const [error4, validatedData4] = await http.validateQueryAsync(rules);
        
        // `http.validateBody`
        const [error5, validatedData5] = http.validateBody(rules);
        // `http.validateBodyAsync`
        const [error6, validatedData6] = await http.validateBodyAsync(rules);
        
        // `http.validatedBody`
        const validatedData7 = http.validatedBody();
        
    }
}

Abolish Middleware

This plugin provides a middleware that will validate the request body and return the validated data via http.validatedBody().

Register Middleware

To register the Middleware, add the following to your use.json file:

{
  "middlewares": {
    "Abolish": "npm://@xpresser/abolish/AbolishMiddleware.js"
  }
}

Or add it globally:

{
  "globalMiddlewares": [
    "npm://@xpresser/abolish/AbolishMiddleware.js"
  ]
}

Add Validation Routes Files

Create a file @ backend/ValidationRules.(js|ts) and add the following to it:

JS
const RoutesGuard = require("@xpresser/abolish/RoutesGuard");

// ===== Initialize RoutesGuard =====
const guard = new RoutesGuard();

// ===== Define validation for Routes =====
// ===== Syntax =====
guard["post" | "patch" | "put"]("Controller@action", rules);
guard["post" | "patch" | "put"]("/exact/path/to/route", rules);

// ===== Example =====
guard.post("AuthController@login", {
    email: "required|email",
    password: "required|min:6"
});

module.exports = guard;
TS
import RoutesGuard from "@xpresser/abolish/RoutesGuard";

// ===== Initialize RoutesGuard =====
const guard = new RoutesGuard();

// ===== Define validation for Routes =====
// ===== Syntax =====
guard["post" | "patch" | "put"]("Controller@action", rules);
guard["post" | "patch" | "put"]("/exact/path/to/route", rules);

// ===== Example =====
guard.post("AuthController@login", {
    email: "required|email",
    password: "required|min:6"
});

export = guard;

Apply Middleware

To apply the middleware, add the following to your controller middlewares;

const controller = {
    middlewares: {
        "Abolish": "*", // for all actions
        // OR
        "Abolish": "action", // for specific action
        // OR
        "Abolish": ["action1", "action2"] // for multiple actions
    }
}

Package Sidebar

Install

npm i @xpresser/abolish

Weekly Downloads

6

Version

2.3.1

License

MIT

Unpacked Size

89.1 kB

Total Files

22

Last publish

Collaborators

  • trapcode