@xpresser/abolish
Version:
Using Abolish validator in Xpresserjs
111 lines (110 loc) • 2.95 kB
JavaScript
;
const xpresser_1 = require("xpresser");
const index_1 = require("../index");
const $ = (0, xpresser_1.getInstance)();
// Get provided Abolish Class
const ProvidedAbolish = $.engineData.call("getProvidedAbolish");
class AbolishRequestEngine extends $.extendedRequestEngine() {
constructor() {
super(...arguments);
/**
* Initialize abolish instance
*/
this.abolish = new ProvidedAbolish();
}
validate(object, rules) {
return (this.abolish || ProvidedAbolish).validate(object, rules);
}
validateAsync(object, rules) {
return (this.abolish || ProvidedAbolish).validateAsync(object, rules);
}
/**
* Use a specific abolish validator class
* @param abolish
*/
useAbolish(abolish) {
this.abolish = abolish;
return this;
}
/**
* Get new validator instance
*/
newAbolish() {
return new ProvidedAbolish();
}
/**
* Validate query data
* @param rules
*/
validateQuery(rules) {
return this.validate(this.req.query, rules);
}
/**
* Validate body data
* @param rules
*/
validateBody(rules) {
return this.validate(this.req.body, rules);
}
/**
* Validate query data Async
* @param rules
*/
validateQueryAsync(rules) {
return this.validateAsync(this.req.query, rules);
}
/**
* Validate body data Async
* @param rules
*/
validateBodyAsync(rules) {
return this.validateAsync(this.req.body, rules);
}
/**
* Get validated body data.
*/
validatedBody() {
return this.state.data["validatedBody"] || {};
}
/**
* Validate body or throw error
* @param rules - Validation rules
*/
validateBodyOrThrow(rules) {
const [err, body] = this.validateBody(rules);
if (err)
throw new index_1.IsAbolishXpresserError(err);
return body;
}
/**
* Validate query or throw error
* @param rules - Validation rules
*/
validateQueryOrThrow(rules) {
const [err, query] = this.validateQuery(rules);
if (err)
throw new index_1.IsAbolishXpresserError(err);
return query;
}
/**
* Validate body or throw error async
* @param rules - Validation rules
*/
async validateBodyOrThrowAsync(rules) {
const [err, body] = await this.validateBodyAsync(rules);
if (err)
throw new index_1.IsAbolishXpresserError(err);
return body;
}
/**
* Validate query or throw error async
* @param rules - Validation rules
*/
async validateQueryOrThrowAsync(rules) {
const [err, query] = await this.validateQueryAsync(rules);
if (err)
throw new index_1.IsAbolishXpresserError(err);
return query;
}
}
module.exports = AbolishRequestEngine;