Skip to content

Validators

Josh Newman edited this page Nov 20, 2018 · 8 revisions

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

Example:

const { v } = 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()
    });
});

ValidatorBuilder

The beginning of any validation chain. It's a function that returns an instance of the ValidatorBuilder 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 { v } = require('koa-fluent-validation');

Validator Functions

#required()

Requires the value.

#requiredIf(path: string, predicate: Function)

Requires the value if the predicate returns true. Predicate requires a value to check against.

// lastName is required if the value of firstName is 'John'
ctx.validateBody({
    firstName: v().string(),
    lastName: v().requiredIf('firstName', (value) => value === 'John')
});

#requiredNotIf(path: string, predicate: Function)

The opposite of requiredIf().

#notNull()

If the value is defined, it ensures that the value is not null.

#string()

The value should be a string.

#email([options: Object])

See validator for options.

#uuid([version: number])

The value should be a UUID. The version defaults to 4.

#number([strict: boolean])

The value should be a number.

#float([strict: boolean, options: Object])

The value should be a float. See validator for options.

#currency([options])

The value should be currency. See validator for options.

#int([strict: boolean, options: Object])

The value should be an int. See validator for options.

#length(min: number[, max: number])

The value should be a minimum of min and a maximum of max.

min defaults to 1.

#base64()

The value should be a base64 string.

#boolean()

The value should be able to parse to a boolean. e.g: true, false, 1, 0.

Keep in mind that anything > 0 is true and anything <= 0 is false.

#in(values: [])

The value should be one of the values specified.

// if firstName exists in body, it must be 'John', 'Sally', or 'Tim'
ctx.validateBody({
    firstName: v().in(['John', 'Sally', 'Tim'])
})

#url([options: Object])

The value should be a valid URL. See validator for options.

#contains(seed: string)

The value should contain seed.

#min(num: number[, strict: boolean])

The value should be >= num.

#max(num: number[, strict: boolean])

The value should be <= num.

#mobilePhone([locale: string])

The value should be a valid phone number for the locale. Default for locale is 'en-US'.

See validator for more usage and locale.

#ipAddress([version: number])

The value should be a valid IP address. Version is 4 or 6.

See validator for more usage.

#test(regex: RegExp)

The value should pass a test on the regex specified.