Skip to content

Commit 3950b2e

Browse files
committed
feat(ajv-coercion): implement strict type validation for request bodies
This change introduces two separate AJV instances for validating request bodies and query parameters, enhancing type safety. The new setup prevents silent coercion of incorrect types in request bodies, ensuring that numeric and boolean values are sent with the correct types. This may break existing API consumers who rely on previous coercion behavior.
1 parent c6ddf34 commit 3950b2e

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

.changeset/strict-ajv-coercion.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
"@rocket.chat/rest-typings": minor
3+
"@rocket.chat/meteor": patch
4+
---
5+
6+
Splits the single AJV validator instance into two: `ajv` (coerceTypes: false) for request **body** validation and `ajvQuery` (coerceTypes: true) for **query parameter** validation.
7+
8+
**Why this matters:** Previously, a single AJV instance with `coerceTypes: true` was used everywhere. This silently accepted values with wrong types — for example, sending `{ "rid": 12345 }` (number) where a string was expected would pass validation because `12345` was coerced to `"12345"`. With this change, body validation is now strict: the server will reject payloads with incorrect types instead of silently coercing them.
9+
10+
**What may break for API consumers:**
11+
12+
- **Numeric values sent as strings in POST/PUT/PATCH bodies** (e.g., `{ "count": "10" }` instead of `{ "count": 10 }`) will now be rejected. Ensure JSON bodies use proper types.
13+
- **Boolean values sent as strings in bodies** (e.g., `{ "readThreads": "true" }` instead of `{ "readThreads": true }`) will now be rejected.
14+
- **`null` values where a string is expected** (e.g., `{ "name": null }` for a `type: 'string'` field without `nullable: true`) will no longer be coerced to `""`.
15+
16+
**No change for query parameters:** GET query params (e.g., `?count=10&offset=0`) continue to be coerced via `ajvQuery`, since HTTP query strings are always strings.

0 commit comments

Comments
 (0)