https://github.com/petruki/validator4oak
Validator & Sanitizer middleware for Oak.
https://github.com/petruki/validator4oak
deno oak validator
Last synced: 4 months ago
JSON representation
Validator & Sanitizer middleware for Oak.
- Host: GitHub
- URL: https://github.com/petruki/validator4oak
- Owner: petruki
- License: mit
- Created: 2024-03-06T06:03:43.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2025-03-08T21:12:54.000Z (4 months ago)
- Last Synced: 2025-03-08T22:19:50.922Z (4 months ago)
- Topics: deno, oak, validator
- Language: TypeScript
- Homepage:
- Size: 93.8 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
***
Validator4Oak
Validator & Sanitizer middleware for Oak.[](https://github.com/petruki/validator4oak/actions/workflows/master.yml)
[](https://sonarcloud.io/summary/new_code?id=petruki_validator4oak)
[](https://deno.land/x/validator4oak)
[](https://jsr.io/@trackerforce/validator4oak)
[](https://opensource.org/licenses/MIT)***
This module provides a middleware for [Oak](https://github.com/oakserver/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 sanitizersImport module with:
```typescript
import * as mod from "@trackerforce/validator4oak@[VERSION]";
import * as mod from "https://deno.land/x/validator4oak@v[VERSION]/mod.ts";
```# Usage
### Validate query parameters
```typescript
const router = new Router();
const { query } = ValidatorMiddleware.createMiddleware();
const { isUrl } = ValidatorFn.createValidator();router.get('/api/v1/shorten',
query([
{ key: 'url', validators: [isUrl()] },
]), (ctx: Context) => {
// ...
},
);
```Optional query parameters:
```typescript
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.```typescript
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.
```typescript
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
```typescript
const { header } = ValidatorMiddleware.createMiddleware();
const { isNumber } = ValidatorFn.createValidator();router.post('/example/v1/shorten',
header([
{ key: 'x-api-key', validators: [isNumber()] },
]), (ctx: Context) => {
// ...
},
);
```### Sanitize parameters
```typescript
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 `tests/deps.ts` file to verify if middleware is compatible.
```typescript
export { Router, Context, Application } from "jsr:@oak/oak@[OAK_VERSION]";
```## Contributing
Please do open an issue if you have some cool ideas to contribute.