Intevel / h3-valibot

🤖 Schema validation for h3 using Valibot
MIT License
57 stars 3 forks source link

Update to latest version #6

Open kilisei opened 6 days ago

kilisei commented 6 days ago

I have fixed the code for my own project, maybe you can use this for an update, I have not tried to build it in this project, maybe this is useful

import type { BaseIssue, BaseSchemaAsync } from 'valibot'
import { createError, getQuery, getRouterParams, type H3Event, readBody } from 'h3'
import { parseAsync } from 'valibot'

const DEFAULT_ERROR_MESSAGE = 'Bad Request'
const DEFAULT_ERROR_STATUS = 400

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function createBadRequest(error: any) {
  return createError({
    statusCode: DEFAULT_ERROR_STATUS,
    statusText: DEFAULT_ERROR_MESSAGE,
    data: error,
  })
}

/**
 * Parse and validate request query from event handler. Throws an error if validation fails.
 * @param event - A H3 event object.
 * @param schema - A Valibot Schema
 */
export async function useValidatedQuery<TInput, TOutput, TIssue extends BaseIssue<unknown>>(
  event: H3Event,
  schema: BaseSchemaAsync<TInput, TOutput, TIssue>,
): Promise<TOutput> {
  try {
    const query = getQuery(event)
    const parsed = await parseAsync(schema, query)
    return parsed
  }
  catch (error) {
    throw createBadRequest(error)
  }
}

/**
 * Parse and validate request body from event handler. Throws an error if validation fails.
 * @param event - A H3 event object.
 * @param schema - A Valibot Schema
 */
export async function useValidatedBody<TInput, TOutput, TIssue extends BaseIssue<unknown>>(
  event: H3Event,
  schema: BaseSchemaAsync<TInput, TOutput, TIssue>,
): Promise<TOutput> {
  try {
    const body = await readBody(event)
    const parsed = await parseAsync(schema, body)
    return parsed
  }
  catch (error) {
    throw createBadRequest(error)
  }
}

/**
 * Parse and validate request params from event handler. Throws an error if validation fails.
 * @param event - A H3 event object.
 * @param schema - A Valibot Schema
 */
export async function useValidatedParams<TInput, TOutput, TIssue extends BaseIssue<unknown>>(
  event: H3Event,
  schema: BaseSchemaAsync<TInput, TOutput, TIssue>,
): Promise<TOutput> {
  try {
    const params = getRouterParams(event)
    const parsed = await parseAsync(schema, params)
    return parsed
  }
  catch (error) {
    throw createBadRequest(error)
  }
}
Intevel commented 5 days ago

What specifically did you fix? And can you open a PR?

kilisei commented 5 days ago

For me the library was broken with the newest version of valibot

Intevel commented 5 days ago

For me the library was broken with the newest version of valibot

Can you make a pull request?