Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible enhancement for @hono/typebox-validator ? #400

Closed
otma2677 opened this issue Feb 17, 2024 · 1 comment
Closed

Possible enhancement for @hono/typebox-validator ? #400

otma2677 opened this issue Feb 17, 2024 · 1 comment

Comments

@otma2677
Copy link

In advance, sorry if my issue is not appropriate, either because it is a duplicate (not seen any), or if I have miss something, I'm an aspiring web developer and quite new to that world.

I'm actually using @hono/typebox-validator and I've found that in the case of param validation, I cannot type the params with something else than string, which is understandable, like explained in the issue #91 by @yusukebe

I've seen that with zod, transform types will automatically convert values after the validation, while with Typebox, you need to use "Value.Decode()" after the check to get the transformed data, so it would be cool to have it doing that by default, as follow;

From

// ...

return validator(target, (data, c) => {
    if (Value.Check(schema, data)) {
      if (hook) {
        const hookResult = hook({ success: true, data }, c)
        if (hookResult instanceof Response || hookResult instanceof Promise) {
          return hookResult
        }
      }
      return data
    }
    return c.json({ success: false, errors: [...Value.Errors(schema, data)] }, 400)
  })

// ...

To

// ...

return validator(target, (data, c) => {
    if (Value.Check(schema, data)) {
      if (hook) {
        const hookResult = hook({ success: true, data: Value.Decode(schema, data) }, c)
        if (hookResult instanceof Response || hookResult instanceof Promise) {
          return hookResult
        }
      }
      return Value.Decode(schema, data)
    }
    return c.json({ success: false, errors: [...Value.Errors(schema, data)] }, 400)
  })

// ...

Or maybe even another dedicated function specifically for case you use Transform values ?

Which means that now we would get back decoded values;

const schema = Type.Object({
  id: Type.Transform(Type.String())
    .Decode(value => Number(value))
    .Encode(value => String(value));
});

app.get(
  '/users/:id',
  tbValidator('param', schema),
  c => {
    const params = c.req.valid('param'); // The id is a number there
   
    // ... do something
  }
);

Maybe the behavior is expected to be that way and I have missed something ? If not, I want to create a pull request for that, would it be ok ? I feel like its doable for me.

I've tried locally (modifying directly the dependency) and its nice, maybe adding some checks (if the string cannot be converted, it becomes null) would be good too ?

@yusukebe
Copy link
Member

Hi @otma2677

I'm sorry for my late response! That makes sense. You can create a PR.

I'm actually using @hono/typebox-validator and I've found that in the case of param validation, I cannot type the params with something else than string, which is understandable, like explained in the issue #91 by @yusukebe

Fyi. The API of Validator is changed from I explained in #91 though it may be what you want. coerce works with Zod Validator #411

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants