Skip to content

Allow users to control type coercion for route schema #1145

Open
@el-tumakov

Description

@el-tumakov

What is the problem this feature would solve?

Currently, Elysia automatically coerces t.Number() and t.Boolean() to t.Numeric() and t.BooleanString() when used in route schemas for parameters, headers, and queries. While this behavior is useful in many cases, it is not configurable. There may be scenarios where users want to explicitly disable this coercion for more strict type validation.

What is the feature you are proposing to solve the problem?

Introduce a configuration option that allows users to enable or disable automatic type coercion for route schemas. This could be implemented via a framework-level setting (e.g., config.coerce) or per-route configuration.

For example:

new Elysia({ coerce: false })
    .get('/:id', ({ id }) => id, {
        params: t.Object({
            id: t.Number() // Should not be coerced to t.Numeric()
        })
    });

Alternatively, a per-route override could be implemented:

new Elysia()
    .get('/:id', ({ id }) => id, {
        params: t.Object({
            id: t.Number()
        }),
        options: { coerce: false }
    });

What alternatives have you considered?

  • Manually validating and parsing values within route handlers, which adds extra boilerplate code and defeats the purpose of using schemas.
  • Extending t.Number() with a custom validation schema, which is less intuitive and requires additional work.

Adding configurability would provide more flexibility while maintaining Elysia’s ease of use.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions