Skip to content

Conversation

@EdamAme-x
Copy link
Contributor

Related: #3707

The author should do the following, if applicable

  • Add tests
  • Run tests
  • bun run format:fix && bun run lint:fix to format the code
  • Add TSDoc/JSDoc to document the code

@EdamAme-x EdamAme-x changed the title feat: warn when json validator does not find a content type in the request feat(validator): warn when json validator does not find a content type in the request Apr 25, 2025
@EdamAme-x EdamAme-x marked this pull request as draft April 25, 2025 03:41
@EdamAme-x
Copy link
Contributor Author

EdamAme-x commented Apr 25, 2025

hi @Soviut

Combine middleware now handles multiple validator types well.
(This is a breaking change for validator, however, and will require a v5 release.)

@EdamAme-x EdamAme-x changed the title feat(validator): warn when json validator does not find a content type in the request feat(validator)!: warn when json validator does not find a content type in the request Apr 25, 2025
@EdamAme-x
Copy link
Contributor Author

E.x.

describe('JSON and FormData', () => {
  const app = new Hono()
  app.post(
    '/',
    some(
      validator('json', (value) => value),
      validator('form', (value) => value)
    ),
    async (c) => {
      const jsonData = c.req.valid('json')
      const formData = c.req.valid('form')
      return c.json({
        json: jsonData,
        form: formData,
      })
    }
  )

  it('Should validate a JSON request', async () => {
    const res = await app.request('/', {
      method: 'POST',
      body: JSON.stringify({ foo: 'bar' }),
      headers: {
        'Content-Type': 'application/json',
      },
    })
    expect(res.status).toBe(200)
    const data = await res.json()
    expect(data.json).toEqual({ foo: 'bar' })
  })

  it('Should validate a FormData request', async () => {
    const form = new FormData()
    form.append('foo', 'bar')
    const res = await app.request('/', {
      method: 'POST',
      body: form,
    })
    expect(res.status).toBe(200)
    const data = await res.json()
    expect(data.form).toEqual({ foo: 'bar' })
  })
})

@codecov
Copy link

codecov bot commented Apr 25, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.31%. Comparing base (15a83b1) to head (e9fe5ae).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4109   +/-   ##
=======================================
  Coverage   91.30%   91.31%           
=======================================
  Files         168      168           
  Lines       10780    10787    +7     
  Branches     3085     3095   +10     
=======================================
+ Hits         9843     9850    +7     
  Misses        936      936           
  Partials        1        1           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@EdamAme-x EdamAme-x changed the title feat(validator)!: warn when json validator does not find a content type in the request feat(validator)!: warn when json validator does not find a valid content type in the request Apr 25, 2025
@EdamAme-x EdamAme-x changed the title feat(validator)!: warn when json validator does not find a valid content type in the request feat(validator)!: warn when validator does not find a valid content type in the request Apr 25, 2025
@EdamAme-x
Copy link
Contributor Author

EdamAme-x commented Apr 26, 2025

The problem here is that in c.req.valid, if you use form and json validators at the same time, you will get type inconsistency. (This has been a problem for some time.)
Types magic can solve this problem.

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

Successfully merging this pull request may close these issues.

1 participant