2.3.1 • Published 8 months ago

@xpresser/abolish v2.3.1

Weekly downloads
1
License
MIT
Repository
github
Last release
8 months ago

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
    }
}
2.3.2

8 months ago

2.3.1

9 months ago

1.12.0

2 years ago

2.2.0

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.11.0

2 years ago

1.11.1

2 years ago

2.3.0

2 years ago

2.1.0

2 years ago

1.10.3

2 years ago

1.10.2

2 years ago

1.9.1

2 years ago

1.9.0

2 years ago

1.8.4

3 years ago

1.9.2

2 years ago

1.10.0

2 years ago

1.8.2

3 years ago

1.8.1

3 years ago

1.8.0

3 years ago

1.8.3

3 years ago

1.7.1

3 years ago

1.7.0

3 years ago

1.6.9

3 years ago

1.6.8

3 years ago

1.6.7

3 years ago

1.6.6

3 years ago

1.6.4

3 years ago

1.6.5

3 years ago

1.6.3

3 years ago

1.6.2

3 years ago

1.6.1

3 years ago

1.6.0

3 years ago

1.5.0

3 years ago

1.4.0

3 years ago

1.3.0

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.0

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago