Skip to content

Commit

Permalink
upgrade effect to 3.10.0
Browse files Browse the repository at this point in the history
  • Loading branch information
baseballyama committed Nov 30, 2024
1 parent 3bd19ae commit 0d26451
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 19 deletions.
5 changes: 2 additions & 3 deletions packages/effect-validator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,11 @@
},
"homepage": "https://github.com/honojs/middleware",
"peerDependencies": {
"@effect/schema": ">=0.68.18",
"effect": ">=3.10.0",
"hono": ">=4.4.13"
},
"devDependencies": {
"@effect/schema": "^0.68.21",
"effect": "^3.4.8",
"effect": "3.10.0",
"hono": "^4.4.13",
"tsup": "^8.1.0",
"typescript": "^5.5.3",
Expand Down
24 changes: 11 additions & 13 deletions packages/effect-validator/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ArrayFormatter } from '@effect/schema'
import * as S from '@effect/schema/Schema'
import { Schema as S, ParseResult } from 'effect'
import { Either } from 'effect'
import type { Env, Input, MiddlewareHandler, ValidationTargets } from 'hono'
import type { Simplify } from 'hono/utils/types'
Expand All @@ -10,13 +9,13 @@ type RemoveReadonly<T> = { -readonly [P in keyof T]: RemoveReadonly<T[P]> }
type HasUndefined<T> = undefined extends T ? true : false

export const effectValidator = <
// eslint-disable-next-line @typescript-eslint/no-explicit-any
T extends S.Schema.Variance<any, any, any>,
Target extends keyof ValidationTargets,
E extends Env,
P extends string,
In = Simplify<RemoveReadonly<S.Schema.Type<T>>>,
Out = Simplify<RemoveReadonly<S.Schema.Type<T>>>,
Type,
Encoded,
In = Simplify<RemoveReadonly<Encoded>>,
Out = Simplify<RemoveReadonly<Type>>,
I extends Input = {
in: HasUndefined<In> extends true
? {
Expand All @@ -34,23 +33,22 @@ export const effectValidator = <
: { [K2 in keyof In]: ValidationTargets[K][K2] }
}
out: { [K in Target]: Out }
},
V extends I = I
}
>(
target: Target,
schema: T
): MiddlewareHandler<E, P, V> =>
schema: S.Schema<Type, Encoded, never>
): MiddlewareHandler<E, P, I> => {
// @ts-expect-error not typed well
validator(target, async (value, c) => {
// @ts-expect-error not typed well
return validator(target, async (value, c) => {
const result = S.decodeUnknownEither(schema)(value)

return Either.match(result, {
onLeft: (error) =>
c.json({ success: false, error: ArrayFormatter.formatErrorSync(error) }, 400),
c.json({ success: false, error: ParseResult.ArrayFormatter.formatErrorSync(error) }, 400),
onRight: (data) => {
c.req.addValidatedData(target, data as object)
return data
},
})
})
}
5 changes: 2 additions & 3 deletions packages/effect-validator/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { ArrayFormatter } from '@effect/schema'
import { Schema as S } from '@effect/schema'
import { Schema as S } from 'effect'
import { Hono } from 'hono'
import type { StatusCode } from 'hono/utils/http-status'
import type { Equal, Expect } from 'hono/utils/types'
Expand Down Expand Up @@ -104,7 +103,7 @@ describe('Basic', () => {
expect(res).not.toBeNull()
expect(res.status).toBe(400)

const data = await res.json<{ success: boolean; error: ArrayFormatter.Issue[] }>()
const data = await res.json()
expect(data.success).toBe(false)
expect(data.error[0].path).toEqual(['age'])
expect(data.error[0].message).toBeDefined()
Expand Down

0 comments on commit 0d26451

Please sign in to comment.