-
Notifications
You must be signed in to change notification settings - Fork 1
Validators
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()
});
});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');Requires the value.
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')
});The opposite of requiredIf().
If the value is defined, it ensures that the value is not null.
The value should be a string.
See validator for options.
The value should be a UUID. The version defaults to 4.
The value should be a number.
The value should be a float. See validator for options.
The value should be currency. See validator for options.
The value should be an int. See validator for options.
The value should be a minimum of min and a maximum of max.
min defaults to 1.
The value should be a base64 string.
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.
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'])
})The value should be a valid URL. See validator for options.
The value should contain seed.
The value should be >= num.
The value should be <= num.
The value should be a valid phone number for the locale. Default for locale is 'en-US'.
See validator for more usage and locale.
The value should be a valid IP address. Version is 4 or 6.
See validator for more usage.
The value should pass a test on the regex specified.