Oak middleware for parameter and body validator
-
As a router middleware
import { validatorMiddleware, ValidatorMiddlewareOptions } from "https://raw.githubusercontent.com/halvardssm/oak-middleware-validator/master/mod.ts" import { RouterMiddleware } from "https://deno.land/x/oak/mod.ts"; const router = new Router(); const app = new Application(); const options: ValidatorMiddlewareOptions = { validations: [ { key: "a", validationOption: "string", isUrlParam: true }, { key: "b", validationOption: "b", isOptional: true }, { key: "c", validationOption: validateStringIn("b") }, { key: "d", validationOption: validateIsNumber(0, 1) }, ], bodyRequired: true, errorMessages: { ERROR_NO_BODY: "Custom message" } } router .get("/bar", validatorMiddleware(options),...) app.use(router.routes()); await app.listen(appOptions);
- validations: ValidationT[]; // Array of validations
- bodyRequired?: boolean; // If true, will return error if body is empty
- bodyType?: BodyType; // Will validate the body against a provided body type
- errorMessages?: Partial; // Customize your own error messages
- key: string; // The key
- validationOption: ValidationFn | string; // The validation function, also accepts a
typeof
or a string to compare strings - isOptional?: boolean; // Will allow the key to be missing
- isUrlParam?: boolean; // Will check the url instead of the body
- ERROR_NO_BODY: "No body was provided",
- ERROR_INVALID_BODY: "Invalid body type",
- ERROR_MISSING_REQUIRED: "No value was provided",
- ERROR_VALIDATION_FAILED: "Validation failed",
- ERROR_NOT_IN_ARRAY: "Value not in array",
- ERROR_NUMBER_MIN: "Value is smaller than the allowed size",
- ERROR_NUMBER_MAX: "Value is larger than the allowed size"
(
key: string, // The key
value: string | Uint8Array, // The value
validationError: ValidationErrorFn, // The validation error callback
errorMessages: ErrorMessagesT, // Exposes the error messages
options: ValidatorMiddlewareOptions // Exposes the options
) => void
validateStringInArray(...values: string[])
// Checks if the value is in the arrayvalidateIsNumber(min?: number, max?: number, radix?: number)
// Checks if the value is a number, and if min/max is given it compares them inclusively
All contributions are welcome, make sure to read the contributing guidelines.