@hono/typebox-validator
Advanced tools
+27
-2
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.tbValidator = void 0; | ||
| const typebox_1 = require("@sinclair/typebox"); | ||
| const value_1 = require("@sinclair/typebox/value"); | ||
| const validator_1 = require("hono/validator"); | ||
| var IsObject = typebox_1.ValueGuard.IsObject; | ||
| var IsArray = typebox_1.ValueGuard.IsArray; | ||
| /** | ||
@@ -51,6 +54,7 @@ * Hono middleware that validates incoming data via a [TypeBox](https://github.com/sinclairzx81/typebox) schema. | ||
| */ | ||
| function tbValidator(target, schema, hook) { | ||
| function tbValidator(target, schema, hook, stripNonSchemaItems) { | ||
| // Compile the provided schema once rather than per validation. This could be optimized further using a shared schema | ||
| // compilation pool similar to the Fastify implementation. | ||
| return (0, validator_1.validator)(target, (data, c) => { | ||
| return (0, validator_1.validator)(target, (unprocessedData, c) => { | ||
| const data = stripNonSchemaItems ? removeNonSchemaItems(schema, unprocessedData) : unprocessedData; | ||
| if (value_1.Value.Check(schema, data)) { | ||
@@ -76,1 +80,22 @@ if (hook) { | ||
| exports.tbValidator = tbValidator; | ||
| function removeNonSchemaItems(schema, obj) { | ||
| if (typeof obj !== 'object' || obj === null) | ||
| return obj; | ||
| if (Array.isArray(obj)) { | ||
| return obj.map((item) => removeNonSchemaItems(schema.items, item)); | ||
| } | ||
| const result = {}; | ||
| for (const key in schema.properties) { | ||
| if (obj.hasOwnProperty(key)) { | ||
| const propertySchema = schema.properties[key]; | ||
| if (IsObject(propertySchema) && | ||
| !IsArray(propertySchema)) { | ||
| result[key] = removeNonSchemaItems(propertySchema, obj[key]); | ||
| } | ||
| else { | ||
| result[key] = obj[key]; | ||
| } | ||
| } | ||
| } | ||
| return result; | ||
| } |
@@ -1,2 +0,2 @@ | ||
| import type { TSchema, Static } from '@sinclair/typebox'; | ||
| import { TSchema, Static } from '@sinclair/typebox'; | ||
| import { type ValueError } from '@sinclair/typebox/value'; | ||
@@ -63,2 +63,2 @@ import type { Context, Env, MiddlewareHandler, ValidationTargets } from 'hono'; | ||
| }; | ||
| }>(target: Target, schema: T, hook?: Hook<Static<T>, E, P>): MiddlewareHandler<E, P, V>; | ||
| }>(target: Target, schema: T, hook?: Hook<Static<T>, E, P>, stripNonSchemaItems?: boolean): MiddlewareHandler<E, P, V>; |
+27
-2
@@ -0,3 +1,6 @@ | ||
| import { ValueGuard } from '@sinclair/typebox'; | ||
| import { Value } from '@sinclair/typebox/value'; | ||
| import { validator } from 'hono/validator'; | ||
| var IsObject = ValueGuard.IsObject; | ||
| var IsArray = ValueGuard.IsArray; | ||
| /** | ||
@@ -48,6 +51,7 @@ * Hono middleware that validates incoming data via a [TypeBox](https://github.com/sinclairzx81/typebox) schema. | ||
| */ | ||
| export function tbValidator(target, schema, hook) { | ||
| export function tbValidator(target, schema, hook, stripNonSchemaItems) { | ||
| // Compile the provided schema once rather than per validation. This could be optimized further using a shared schema | ||
| // compilation pool similar to the Fastify implementation. | ||
| return validator(target, (data, c) => { | ||
| return validator(target, (unprocessedData, c) => { | ||
| const data = stripNonSchemaItems ? removeNonSchemaItems(schema, unprocessedData) : unprocessedData; | ||
| if (Value.Check(schema, data)) { | ||
@@ -72,1 +76,22 @@ if (hook) { | ||
| } | ||
| function removeNonSchemaItems(schema, obj) { | ||
| if (typeof obj !== 'object' || obj === null) | ||
| return obj; | ||
| if (Array.isArray(obj)) { | ||
| return obj.map((item) => removeNonSchemaItems(schema.items, item)); | ||
| } | ||
| const result = {}; | ||
| for (const key in schema.properties) { | ||
| if (obj.hasOwnProperty(key)) { | ||
| const propertySchema = schema.properties[key]; | ||
| if (IsObject(propertySchema) && | ||
| !IsArray(propertySchema)) { | ||
| result[key] = removeNonSchemaItems(propertySchema, obj[key]); | ||
| } | ||
| else { | ||
| result[key] = obj[key]; | ||
| } | ||
| } | ||
| } | ||
| return result; | ||
| } |
+1
-1
| { | ||
| "name": "@hono/typebox-validator", | ||
| "version": "0.2.6", | ||
| "version": "0.3.0", | ||
| "description": "Validator middleware using TypeBox", | ||
@@ -5,0 +5,0 @@ "main": "dist/cjs/index.js", |
9870
24.18%257
24.15%