Skip to content

Filters

Josh Newman edited this page Jul 22, 2017 · 1 revision

Filters are methods that manipulate the incoming value structure from the body, params, or query before and/or after the validators are called.

Example:

const { v, f } = require('koa-fluent-validation');

app.use(async ctx => {
    if (ctx.method !== 'POST') {
        ctx.throw(404);
        return;
    }

    ctx.validateBody({
        firstName: v().required().string(),
        lastName: v().required().string(),
        email: v().required().email()
    }, {
        before: {
            firstName: f().trim().toUpper(),
            lastName: f().trim().toUpper(),
            email: f().trim().normalizeEmail()
        }
    });
});

FilterBuilder

The beginning of any filter chain. It's a function that returns an instance of the FilterBuilder class. Each function that is called on this instance returns the same type.

Example:

// v is a function ready to give you a builder to work off of.
const { f } = require('koa-fluent-validation');

Filter Functions

#trim()

If the value is a string, it trims it.

#toUpper()

If the value is a string, it converts it to uppercase.

#toLower()

If the value is a string, it converts it to lowercase.

#padStart(len: number[, fill: string])

If the value is a string, it fills it on the left with the fill parameter it until the length is reached. If fill is not specified, empty spaces will be applied.

See padStart for more information.

#padEnd(len: number[, fill: string])

If the value is a string, it fills it on the right with the fill parameter it until the length is reached. If fill is not specified, empty spaces will be applied.

See padEnd for more information.

#normalizeEmail([options: Object])

If the value is a string, it normalizes the email.

See validator for more information and the options parameter.

#toInt([radix: number])

If the value is a string, it will try to convert to an integer. NaN is outputted when it can't.

#toBoolean([strict: boolean])

convert the input string to a boolean. Everything except for '0', 'false' and '' returns true. In strict mode only '1' and 'true' return true.

See validator for more information.