Skip to main content
Home

Built and signed on GitHub Actions

Validator & Sanitizer middleware for Oak.

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
7 months ago (1.2.0)

Validator4Oak
Validator & Sanitizer middleware for Oak.

Master CI Quality Gate Status deno.land/x/validator4oak JSR License: MIT


This module provides a middleware for Oak to validate request using JSON Schema.

Compatibility: Oak v14.0.0+

  • Validate request query, body, header parameters
  • Sanitize request query and body parameters
  • Define custom error handler
  • Create custom validators and sanitizers
  • Stack up multiple validators and sanitizers

Import module with:

import * as mod from "@trackerforce/validator4oak@[VERSION]";
import * as mod from "https://deno.land/x/validator4oak@v[VERSION]/mod.ts";

Usage

Validate query parameters

const router = new Router();
const { query } = ValidatorMiddleware.createMiddleware<Context, Next>();
const { isUrl } = ValidatorFn.createValidator();

router.get('/api/v1/shorten',
  query([
    { key: 'url', validators: [isUrl()] },
  ]), (ctx: Context) => {
    // ...
  },
);

Optional query parameters:

router.get('/api/v1/shorten',
  query([
    { key: 'url', optional: true },
  ]), (ctx: Context) => {
    // ...
  },
);

Validate body parameters

Key body parameters can be accessed using complex keys, e.g. order.number.
Since Oak v14, body request can only be consumed once, so you'll need to use state.request_body to access it within the route.

router.post('/checkout/v1/confirm',
  body([
    { key: 'order.number', validators: [isNumeric()] },
  ]), (ctx: Context) => {
    // ...
  },
);

It's also possible to validate array of complex objects using isArray() and * wildcard to access array elements.

router.post('/checkout/v1/confirm',
  body([
    { key: 'order.items', validators: [isArray()] },
    { key: 'order.items.*.sku', validators: [isString()] },
    { key: 'order.items.*.quantity', validators: [isNumeric()] },
  ]), (ctx: Context) => {
    // ...
  },
);

Validate header parameters

const { header } = ValidatorMiddleware.createMiddleware<Context, Next>();
const { isNumber } = ValidatorFn.createValidator();

router.post('/example/v1/shorten',
  header([
    { key: 'x-api-key', validators: [isNumber()] },
  ]), (ctx: Context) => {
    // ...
  },
);

Sanitize parameters

const { escape } = ValidatorSn.createSanitizer();

router.post('/message/v1/send',
  body([
    { key: 'message', validators: [hasLenght({ max: 500 })], sanitizers: [escape()] },
  ]), (ctx: Context) => {
    // ...
  },
);

Testing

Use deno task test to run tests.

Check Oak compatibility

Replace the Oak version in the test/deps.ts file to verify if middleware is compatible.

export { Router, Context, Application } from "jsr:@oak/oak@[OAK_VERSION]";

Contributing

Please do open an issue if you have some cool ideas to contribute.

Built and signed on
GitHub Actions

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@trackerforce/validator4oak

Import symbol

import * as validator_oak from "@trackerforce/validator4oak";
or

Import directly with a jsr specifier

import * as validator_oak from "jsr:@trackerforce/validator4oak";

Add Package

pnpm i jsr:@trackerforce/validator4oak
or (using pnpm 10.8 or older)
pnpm dlx jsr add @trackerforce/validator4oak

Import symbol

import * as validator_oak from "@trackerforce/validator4oak";

Add Package

yarn add jsr:@trackerforce/validator4oak
or (using Yarn 4.8 or older)
yarn dlx jsr add @trackerforce/validator4oak

Import symbol

import * as validator_oak from "@trackerforce/validator4oak";

Add Package

vlt install jsr:@trackerforce/validator4oak

Import symbol

import * as validator_oak from "@trackerforce/validator4oak";

Add Package

npx jsr add @trackerforce/validator4oak

Import symbol

import * as validator_oak from "@trackerforce/validator4oak";

Add Package

bunx jsr add @trackerforce/validator4oak

Import symbol

import * as validator_oak from "@trackerforce/validator4oak";