Skip to content

halvardssm/oak-middleware-validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Oak Middleware Validator

GitHub Workflow Status (branch) (Deno) (Oak) deno doc

Oak middleware for parameter and body validator

Usage

  • 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);

Options

  • 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

validationT

  • 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

errorMessagesT

  • 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"

validationFn

(
  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

Available validation methods

  • validateStringInArray(...values: string[]) // Checks if the value is in the array
  • validateIsNumber(min?: number, max?: number, radix?: number) // Checks if the value is a number, and if min/max is given it compares them inclusively

Contributing

All contributions are welcome, make sure to read the contributing guidelines.

Uses

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published