🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

@hono/typebox-validator

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@hono/typebox-validator - npm Package Compare versions

Comparing version
0.3.3
to
1.0.0
+91
CHANGELOG.md
# @hono/typebox-validator
## 1.0.0
### Major Changes
- [#1501](https://github.com/honojs/middleware/pull/1501) [`f90b6e48f08dfbd8949041e3076b28ba67c25da4`](https://github.com/honojs/middleware/commit/f90b6e48f08dfbd8949041e3076b28ba67c25da4) Thanks [@sinclairzx81](https://github.com/sinclairzx81)! - Upgrade TypeBox Version 1
## 0.3.3
### Patch Changes
- [#1080](https://github.com/honojs/middleware/pull/1080) [`83ca99d3377380e2e2c4699af14786f1e7d0b7c6`](https://github.com/honojs/middleware/commit/83ca99d3377380e2e2c4699af14786f1e7d0b7c6) Thanks [@yusukebe](https://github.com/yusukebe)! - fix: export modules correctly
## 0.3.2
### Patch Changes
- [#949](https://github.com/honojs/middleware/pull/949) [`401dddc484adab7660efb31586ecb2fbcca03b9b`](https://github.com/honojs/middleware/commit/401dddc484adab7660efb31586ecb2fbcca03b9b) Thanks [@yusukebe](https://github.com/yusukebe)! - fix: correct exporting in `package.json`
## 0.3.1
### Patch Changes
- [#942](https://github.com/honojs/middleware/pull/942) [`63508a56bba1d39e7dd6d95c15f85622ed832133`](https://github.com/honojs/middleware/commit/63508a56bba1d39e7dd6d95c15f85622ed832133) Thanks [@cfraz89](https://github.com/cfraz89)! - Fix transiently importing typebox as cjs even when typebox-validator imported as esm
## 0.3.0
### Minor Changes
- [#866](https://github.com/honojs/middleware/pull/866) [`c815055bb6669aff8fccf5f478983906c65aca9d`](https://github.com/honojs/middleware/commit/c815055bb6669aff8fccf5f478983906c65aca9d) Thanks [@Micnubinub](https://github.com/Micnubinub)! - Added ability to remove properties that are not in the schema to emulate other validators like zod
## 0.2.6
### Patch Changes
- [#821](https://github.com/honojs/middleware/pull/821) [`553112c2c6440f3326e9fde839681620acb3090e`](https://github.com/honojs/middleware/commit/553112c2c6440f3326e9fde839681620acb3090e) Thanks [@MathurAditya724](https://github.com/MathurAditya724)! - exported the Hook type
## 0.2.5
### Patch Changes
- [#742](https://github.com/honojs/middleware/pull/742) [`39cb252df9a0829b8397b9edbcc1de27e49230e1`](https://github.com/honojs/middleware/commit/39cb252df9a0829b8397b9edbcc1de27e49230e1) Thanks [@Micnubinub](https://github.com/Micnubinub)! - Fix fail case not returning error array in hook
## 0.2.4
### Patch Changes
- [#488](https://github.com/honojs/middleware/pull/488) [`1cc5e0a5b07a14723c5d21ceea33ad9caef33025`](https://github.com/honojs/middleware/commit/1cc5e0a5b07a14723c5d21ceea33ad9caef33025) Thanks [@Frog-kt](https://github.com/Frog-kt)! - Fixed a part of deprecated response json method in hono since v4.
## 0.2.3
### Patch Changes
- [#463](https://github.com/honojs/middleware/pull/463) [`892ea7da85fb2c799eacc4d29651e1fa45a6aec0`](https://github.com/honojs/middleware/commit/892ea7da85fb2c799eacc4d29651e1fa45a6aec0) Thanks [@yudai-nkt](https://github.com/yudai-nkt)! - Update peer dependencies so that newer validators can be installed.
## 0.2.2
### Patch Changes
- [#219](https://github.com/honojs/middleware/pull/219) [`b3d80a0`](https://github.com/honojs/middleware/commit/b3d80a0cca92db6b243d3a6e9761c20d931136a2) Thanks [@yusukebe](https://github.com/yusukebe)! - bump hono version of `peerDependencies`
## 0.2.1
### Patch Changes
- [#178](https://github.com/honojs/middleware/pull/178) [`7c6573d`](https://github.com/honojs/middleware/commit/7c6573da690dc002489b99c0a7a00285ebf011f4) Thanks [@melbourne2991](https://github.com/melbourne2991)! - fix(typebox): Update typebox dependency
## 0.2.0
### Minor Changes
- [#90](https://github.com/honojs/middleware/pull/90) [`65ed112`](https://github.com/honojs/middleware/commit/65ed112e0e6cc50c79d34986703d156bc88bb304) Thanks [@NotAmaan](https://github.com/NotAmaan)! - Fixed EvalError
## 0.1.1
### Patch Changes
- [#72](https://github.com/honojs/middleware/pull/72) [`9ce42c2`](https://github.com/honojs/middleware/commit/9ce42c23dd622a70628d5e0138366b5646df7da7) Thanks [@yusukebe](https://github.com/yusukebe)! - refactor: refactored types
## 0.1.0
### Minor Changes
- [#65](https://github.com/honojs/middleware/pull/65) [`5723cc0`](https://github.com/honojs/middleware/commit/5723cc08c505ab817166cf79d2dee56bd15d8c19) Thanks [@yusukebe](https://github.com/yusukebe)! - feat: Support Hono v3.1.0
## 0.0.1
### Patch Changes
- [#55](https://github.com/honojs/middleware/pull/55) [`2a3245a`](https://github.com/honojs/middleware/commit/2a3245ad060590fee85b4c7b4188196817b41945) Thanks [@curtislarson](https://github.com/curtislarson)! - Add TypeBox validator middleware
+6
-28

@@ -26,11 +26,9 @@ "use strict";

module.exports = __toCommonJS(index_exports);
var import_typebox = require("@sinclair/typebox");
var import_value = require("@sinclair/typebox/value");
var import_validator = require("hono/validator");
var IsObject = import_typebox.ValueGuard.IsObject;
var IsArray = import_typebox.ValueGuard.IsArray;
function tbValidator(target, schema, hook, stripNonSchemaItems) {
var import_compile = require("typebox/compile");
function tbValidator(target, schema, hook, useClean) {
const compiled = (0, import_compile.Compile)(schema);
return (0, import_validator.validator)(target, (unprocessedData, c) => {
const data = stripNonSchemaItems ? removeNonSchemaItems(schema, unprocessedData) : unprocessedData;
if (import_value.Value.Check(schema, data)) {
const data = useClean ? compiled.Clean(unprocessedData) : unprocessedData;
if (compiled.Check(data)) {
if (hook) {

@@ -44,3 +42,3 @@ const hookResult = hook({ success: true, data }, c);

}
const errors = Array.from(import_value.Value.Errors(schema, data));
const errors = compiled.Errors(data);
if (hook) {

@@ -55,22 +53,2 @@ const hookResult = hook({ success: false, errors }, c);

}
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 (Object.prototype.hasOwnProperty.call(obj, key)) {
const propertySchema = schema.properties[key];
if (IsObject(propertySchema) && !IsArray(propertySchema)) {
result[key] = removeNonSchemaItems(propertySchema, obj[key]);
} else {
result[key] = obj[key];
}
}
}
return result;
}
// Annotate the CommonJS export names for ESM import in node:

@@ -77,0 +55,0 @@ 0 && (module.exports = {

@@ -1,4 +0,4 @@

import { TSchema, Static } from '@sinclair/typebox';
import { ValueError } from '@sinclair/typebox/value';
import { Env, Context, ValidationTargets, TypedResponse, MiddlewareHandler } from 'hono';
import { TSchema, Static } from 'typebox';
import { TLocalizedValidationError } from 'typebox/error';

@@ -10,3 +10,3 @@ type Hook<T, E extends Env, P extends string> = (result: {

success: false;
errors: ValueError[];
errors: TLocalizedValidationError[];
}, c: Context<E, P>) => Response | Promise<Response> | void;

@@ -22,10 +22,10 @@ /**

* import { tbValidator } from '@hono/typebox-validator'
* import { Type as T } from '@sinclair/typebox'
* import { Type } from 'typebox'
*
* const schema = T.Object({
* name: T.String(),
* age: T.Number(),
* const User = Type.Object({
* name: Type.String(),
* age: Type.Number(),
* })
*
* const route = app.post('/user', tbValidator('json', schema), (c) => {
* const route = app.post('/user', tbValidator('json', User), (c) => {
* const user = c.req.valid('json')

@@ -41,7 +41,7 @@ * return c.json({ success: true, message: `${user.name} is ${user.age}` })

* import { tbValidator } from '@hono/typebox-validator'
* import { Type as T } from '@sinclair/typebox'
* import { Type } from 'typebox'
*
* const schema = T.Object({
* name: T.String(),
* age: T.Number(),
* const User = Type.Object({
* name: Type.String(),
* age: Type.Number(),
* })

@@ -51,3 +51,3 @@ *

* '/user',
* tbValidator('json', schema, (result, c) => {
* tbValidator('json', User, (result, c) => {
* if (!result.success) {

@@ -62,2 +62,11 @@ * return c.text('Invalid!', 400)

type ExcludeResponseType<T> = T extends Response & TypedResponse<any> ? never : T;
/**
* TypeBox Validator Middleware for Hono with support for JSON Schema.
*
* @param target - Validation target: 'json', 'query', 'param', 'header', 'cookie', or 'form'.
* @param schema - TypeBox type or JSON Schema.
* @param hook - Optional hook function for handling validation results.
* @param useClean - Removes excess properties (TypeBox types only). Defaults to `false`.
* @returns A Hono middleware handler.
*/
declare function tbValidator<T extends TSchema, Target extends keyof ValidationTargets, E extends Env, P extends string, V extends {

@@ -70,4 +79,4 @@ in: {

};
}>(target: Target, schema: T, hook?: Hook<Static<T>, E, P>, stripNonSchemaItems?: boolean): MiddlewareHandler<E, P, V>;
}>(target: Target, schema: T, hook?: Hook<Static<T>, E, P>, useClean?: boolean): MiddlewareHandler<E, P, V>;
export { type Hook, tbValidator };

@@ -1,4 +0,4 @@

import { TSchema, Static } from '@sinclair/typebox';
import { ValueError } from '@sinclair/typebox/value';
import { Env, Context, ValidationTargets, TypedResponse, MiddlewareHandler } from 'hono';
import { TSchema, Static } from 'typebox';
import { TLocalizedValidationError } from 'typebox/error';

@@ -10,3 +10,3 @@ type Hook<T, E extends Env, P extends string> = (result: {

success: false;
errors: ValueError[];
errors: TLocalizedValidationError[];
}, c: Context<E, P>) => Response | Promise<Response> | void;

@@ -22,10 +22,10 @@ /**

* import { tbValidator } from '@hono/typebox-validator'
* import { Type as T } from '@sinclair/typebox'
* import { Type } from 'typebox'
*
* const schema = T.Object({
* name: T.String(),
* age: T.Number(),
* const User = Type.Object({
* name: Type.String(),
* age: Type.Number(),
* })
*
* const route = app.post('/user', tbValidator('json', schema), (c) => {
* const route = app.post('/user', tbValidator('json', User), (c) => {
* const user = c.req.valid('json')

@@ -41,7 +41,7 @@ * return c.json({ success: true, message: `${user.name} is ${user.age}` })

* import { tbValidator } from '@hono/typebox-validator'
* import { Type as T } from '@sinclair/typebox'
* import { Type } from 'typebox'
*
* const schema = T.Object({
* name: T.String(),
* age: T.Number(),
* const User = Type.Object({
* name: Type.String(),
* age: Type.Number(),
* })

@@ -51,3 +51,3 @@ *

* '/user',
* tbValidator('json', schema, (result, c) => {
* tbValidator('json', User, (result, c) => {
* if (!result.success) {

@@ -62,2 +62,11 @@ * return c.text('Invalid!', 400)

type ExcludeResponseType<T> = T extends Response & TypedResponse<any> ? never : T;
/**
* TypeBox Validator Middleware for Hono with support for JSON Schema.
*
* @param target - Validation target: 'json', 'query', 'param', 'header', 'cookie', or 'form'.
* @param schema - TypeBox type or JSON Schema.
* @param hook - Optional hook function for handling validation results.
* @param useClean - Removes excess properties (TypeBox types only). Defaults to `false`.
* @returns A Hono middleware handler.
*/
declare function tbValidator<T extends TSchema, Target extends keyof ValidationTargets, E extends Env, P extends string, V extends {

@@ -70,4 +79,4 @@ in: {

};
}>(target: Target, schema: T, hook?: Hook<Static<T>, E, P>, stripNonSchemaItems?: boolean): MiddlewareHandler<E, P, V>;
}>(target: Target, schema: T, hook?: Hook<Static<T>, E, P>, useClean?: boolean): MiddlewareHandler<E, P, V>;
export { type Hook, tbValidator };
// src/index.ts
import { ValueGuard } from "@sinclair/typebox";
import { Value } from "@sinclair/typebox/value";
import { validator } from "hono/validator";
var IsObject = ValueGuard.IsObject;
var IsArray = ValueGuard.IsArray;
function tbValidator(target, schema, hook, stripNonSchemaItems) {
import { Compile } from "typebox/compile";
function tbValidator(target, schema, hook, useClean) {
const compiled = Compile(schema);
return validator(target, (unprocessedData, c) => {
const data = stripNonSchemaItems ? removeNonSchemaItems(schema, unprocessedData) : unprocessedData;
if (Value.Check(schema, data)) {
const data = useClean ? compiled.Clean(unprocessedData) : unprocessedData;
if (compiled.Check(data)) {
if (hook) {

@@ -19,3 +17,3 @@ const hookResult = hook({ success: true, data }, c);

}
const errors = Array.from(Value.Errors(schema, data));
const errors = compiled.Errors(data);
if (hook) {

@@ -30,24 +28,4 @@ const hookResult = hook({ success: false, errors }, c);

}
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 (Object.prototype.hasOwnProperty.call(obj, key)) {
const propertySchema = schema.properties[key];
if (IsObject(propertySchema) && !IsArray(propertySchema)) {
result[key] = removeNonSchemaItems(propertySchema, obj[key]);
} else {
result[key] = obj[key];
}
}
}
return result;
}
export {
tbValidator
};
{
"name": "@hono/typebox-validator",
"version": "0.3.3",
"version": "1.0.0",
"description": "Validator middleware using TypeBox",

@@ -25,5 +25,9 @@ "type": "module",

"build": "tsup ./src/index.ts",
"format": "prettier --check . --ignore-path ../../.gitignore",
"lint": "eslint",
"prepack": "yarn build",
"publint": "attw --pack && publint",
"test": "vitest"
"typecheck": "tsc -b tsconfig.json",
"test": "vitest",
"version:jsr": "yarn version:set $npm_package_version"
},

@@ -42,13 +46,14 @@ "license": "MIT",

"peerDependencies": {
"@sinclair/typebox": ">=0.31.15 <1",
"hono": ">=3.9.0"
"hono": ">=3.9.0",
"typebox": "^1.0.30"
},
"devDependencies": {
"@arethetypeswrong/cli": "^0.17.4",
"@sinclair/typebox": "^0.31.15",
"hono": "^3.11.7",
"publint": "^0.3.9",
"tsup": "^8.4.0",
"vitest": "^3.0.8"
"@arethetypeswrong/cli": "^0.18.2",
"hono": "^4.9.10",
"publint": "^0.3.14",
"tsup": "^8.5.0",
"typebox": "^1.0.30",
"typescript": "^5.8.2",
"vitest": "^3.2.4"
}
}
}
+10
-10

@@ -14,10 +14,10 @@ # TypeBox validator middleware for Hono

import { tbValidator } from '@hono/typebox-validator'
import { Type as T } from '@sinclair/typebox'
import Type from 'typebox'
const schema = T.Object({
name: T.String(),
age: T.Number(),
const User = Type.Object({
name: Type.String(),
age: Type.Number(),
})
const route = app.post('/user', tbValidator('json', schema), (c) => {
const route = app.post('/user', tbValidator('json', User), (c) => {
const user = c.req.valid('json')

@@ -32,7 +32,7 @@ return c.json({ success: true, message: `${user.name} is ${user.age}` })

import { tbValidator } from '@hono/typebox-validator'
import { Type as T } from '@sinclair/typebox'
import Type from 'typebox'
const schema = T.Object({
name: T.String(),
age: T.Number(),
const User = Type.Object({
name: Type.String(),
age: Type.Number(),
})

@@ -42,3 +42,3 @@

'/user',
tbValidator('json', schema, (result, c) => {
tbValidator('json', User, (result, c) => {
if (!result.success) {

@@ -45,0 +45,0 @@ return c.text('Invalid!', 400)