feat(arktype-validator): migrate to 2.0 (#542)

* feat(arktype-validator): migrate to 2.0

* add changeset
pull/541/head
David Blass 2024-05-23 16:59:20 -04:00 committed by GitHub
parent 30cee0c482
commit c05e5456cb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 46 additions and 17 deletions

View File

@ -0,0 +1,9 @@
---
'@hono/arktype-validator': major
---
Migrate to ArkType 2.0
If you're still using `arktype-1.x`, you should remain on `@hono/arktype-validator@1.0.0`.
If you're using `arktype-2.x` (latest), you'll need to upgrade to `@hono/arktype-validator@2.0.0`.

View File

@ -31,11 +31,11 @@
}, },
"homepage": "https://github.com/honojs/middleware", "homepage": "https://github.com/honojs/middleware",
"peerDependencies": { "peerDependencies": {
"arktype": "^1.0.28-alpha", "arktype": "^2.0.0-dev.14",
"hono": "*" "hono": "*"
}, },
"devDependencies": { "devDependencies": {
"arktype": "^1.0.28-alpha", "arktype": "^2.0.0-dev.14",
"hono": "^3.11.7", "hono": "^3.11.7",
"tsup": "^8.0.1", "tsup": "^8.0.1",
"vitest": "^1.0.4" "vitest": "^1.0.4"

View File

@ -1,17 +1,16 @@
import type { Type, Problems } from 'arktype' import { type, type Type, type ArkErrors } from 'arktype'
import type { Context, MiddlewareHandler, Env, ValidationTargets, TypedResponse } from 'hono' import type { Context, MiddlewareHandler, Env, ValidationTargets, TypedResponse } from 'hono'
import { validator } from 'hono/validator' import { validator } from 'hono/validator'
export type Hook<T, E extends Env, P extends string, O = {}> = ( export type Hook<T, E extends Env, P extends string, O = {}> = (
result: { success: false; data: unknown; problems: Problems } | { success: true; data: T }, result: { success: false; data: unknown; errors: ArkErrors } | { success: true; data: T },
c: Context<E, P> c: Context<E, P>
) => Response | Promise<Response> | void | Promise<Response | void> | TypedResponse<O> ) => Response | Promise<Response> | void | Promise<Response | void> | TypedResponse<O>
type HasUndefined<T> = undefined extends T ? true : false type HasUndefined<T> = undefined extends T ? true : false
export const arktypeValidator = < export const arktypeValidator = <
// eslint-disable-next-line @typescript-eslint/no-explicit-any T extends Type,
T extends Type<any>,
Target extends keyof ValidationTargets, Target extends keyof ValidationTargets,
E extends Env, E extends Env,
P extends string, P extends string,
@ -30,11 +29,13 @@ export const arktypeValidator = <
hook?: Hook<T['infer'], E, P> hook?: Hook<T['infer'], E, P>
): MiddlewareHandler<E, P, V> => ): MiddlewareHandler<E, P, V> =>
validator(target, (value, c) => { validator(target, (value, c) => {
const { data, problems } = schema(value) const out = schema(value)
const hasErrors = out instanceof type.errors
if (hook) { if (hook) {
const hookResult = hook( const hookResult = hook(
problems ? { success: false, data: value, problems } : { success: true, data }, hasErrors ? { success: false, data: value, errors: out } : { success: true, data: out },
c c
) )
if (hookResult) { if (hookResult) {
@ -47,15 +48,15 @@ export const arktypeValidator = <
} }
} }
if (problems) { if (hasErrors) {
return c.json( return c.json(
{ {
success: false, success: false,
problems: problems.map((problem) => ({ ...problem, message: problem.toString() })), errors: out,
}, },
400 400
) )
} }
return data return out
}) })

View File

@ -34,6 +34,22 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"@arktype/schema@npm:0.1.4-cjs":
version: 0.1.4-cjs
resolution: "@arktype/schema@npm:0.1.4-cjs"
dependencies:
"@arktype/util": "npm:0.0.41-cjs"
checksum: dd83edcbd29ab8b1faf4c8aee3d03b7d75a4cc7848c44cffd0580b89cfa0cabd280916fcd94317b89fc9d730a0fb6c9e7f53db9088788f54dd9077d5479c1b2f
languageName: node
linkType: hard
"@arktype/util@npm:0.0.41-cjs":
version: 0.0.41-cjs
resolution: "@arktype/util@npm:0.0.41-cjs"
checksum: 77013353924d4f1d81eacc782b7bf07ac21fd5e96368505e05f75dfc822933cb3fc65ba055cc60105f5a86f89c9f4d60aa8be8f0942ebee93f0165cbe4e4b2b9
languageName: node
linkType: hard
"@asteasolutions/zod-to-openapi@npm:^7.0.0": "@asteasolutions/zod-to-openapi@npm:^7.0.0":
version: 7.0.0 version: 7.0.0
resolution: "@asteasolutions/zod-to-openapi@npm:7.0.0" resolution: "@asteasolutions/zod-to-openapi@npm:7.0.0"
@ -1771,12 +1787,12 @@ __metadata:
version: 0.0.0-use.local version: 0.0.0-use.local
resolution: "@hono/arktype-validator@workspace:packages/arktype-validator" resolution: "@hono/arktype-validator@workspace:packages/arktype-validator"
dependencies: dependencies:
arktype: "npm:^1.0.28-alpha" arktype: "npm:^2.0.0-dev.14"
hono: "npm:^3.11.7" hono: "npm:^3.11.7"
tsup: "npm:^8.0.1" tsup: "npm:^8.0.1"
vitest: "npm:^1.0.4" vitest: "npm:^1.0.4"
peerDependencies: peerDependencies:
arktype: ^1.0.28-alpha arktype: ^2.0.0-dev.14
hono: "*" hono: "*"
languageName: unknown languageName: unknown
linkType: soft linkType: soft
@ -5091,10 +5107,13 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"arktype@npm:^1.0.28-alpha": "arktype@npm:^2.0.0-dev.14":
version: 1.0.28-alpha version: 2.0.0-dev.12-cjs
resolution: "arktype@npm:1.0.28-alpha" resolution: "arktype@npm:2.0.0-dev.12-cjs"
checksum: cf5a7a6303dcd42d54f10f83119c90ac382a0b75074716c4b6fcca6e6326ea070b6a8b1ae7a4da576e93e80b312d0d01368937e695074ddd3a53a0dc30fb98b2 dependencies:
"@arktype/schema": "npm:0.1.4-cjs"
"@arktype/util": "npm:0.0.41-cjs"
checksum: b0e7fc182bb6d4d0d4e125744a5b8233295d2ca30715f1bafdd158d37ed8abffafe0f5ef28ff8f99f82ffb1b8d1f36fcc282035c4ef4b909a748c1d520044afc
languageName: node languageName: node
linkType: hard linkType: hard