fix(auth): map ObjectQL ValidationError to 4xx on better-auth paths (#3398)#3399
Merged
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
📓 Docs Drift CheckThis PR changes 1 package(s): 9 hand-written doc(s) reference the affected code and may need an implementation-accuracy re-verification:
|
…3398) better-auth only maps its own APIErrors to structured HTTP responses; a ValidationError thrown from the objectql-adapter (e.g. an invalid `image` on update-user) propagated to better-call's router as an unhandled fault and surfaced as a raw 500 with an empty body. Add the auth-path analogue of the REST layer's mapDataError: detect the ObjectQL validation envelope at the adapter boundary (duck-typed by code/name, so no hard dependency on @objectstack/objectql and no cross-realm instanceof) and re-throw it as APIError('BAD_REQUEST', …) so update-user & friends answer with a 400 carrying the human message plus per-field detail. Applied via withValidationErrorMapping over the production adapter factory. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
os-zhuang
pushed a commit
that referenced
this pull request
Jul 22, 2026
The record-validator's url-type check required an absolute `scheme://` URL, so it rejected the root-relative value the platform's own storage service returns for an uploaded file. The console avatar uploader PUTs the image to storage and then writes `sys_user.image` (a Field.url) = `/api/v1/storage/files/<id>`; that failed `invalid_url` and, on the better-auth `update-user` path, broke the avatar-upload profile save. `URL_RE` now also accepts root-/protocol-relative refs (`/path`, `//host/path`) and the `data:` / `blob:` inline forms, in addition to `scheme://…`. A bare scheme-less string (e.g. "notaurl") is still rejected. Verified end-to-end in the running Console: avatar upload → display → replace → remove all succeed. Complements #3399 (which maps the validation error to a clean 4xx); this makes the legitimate relative URL pass. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
On the better-auth endpoints, a field-level validation failure raised by the ObjectQL record-validator surfaced to the HTTP client as a raw 500 with an empty body, instead of a clean 4xx carrying the validation message.
Repro: sign in, then
POST /api/v1/auth/update-userwith{"image":"notaurl"}→500empty body (should be ~400with"image must be a valid URL").Fixes #3398.
Why
The better-auth
update-userendpoint writes through the objectql-adapter into the ObjectQL engine. When the engine's record-validator (packages/objectql/src/validation/record-validator.ts) throws aValidationError, better-auth does not map it — better-auth only turns its ownAPIErrors into structured responses, and any other error thrown from an adapter method propagates to better-call's router as an unhandled fault →500 {}.The REST data layer already handles this via
mapDataError/sendError(packages/rest/src/rest-server.ts), translating an ObjectQLValidationErrorinto400 { code: 'VALIDATION_FAILED', fields }. The better-auth path had no equivalent.How
packages/plugins/plugin-auth/src/objectql-adapter.ts:isObjectQLValidationError— duck-typed bycode === 'VALIDATION_FAILED'/name === 'ValidationError', so plugin-auth keeps no hard dependency on@objectstack/objectqland cross-realminstanceofcan't bite.rethrowAsBetterAuthError— on a hit, lazily importsbetter-auth/api'sAPIErrorand re-throwsAPIError('BAD_REQUEST', { message, code: 'VALIDATION_FAILED', fields })(→ HTTP 400 with the human message + per-field detail); every other error is re-thrown verbatim.withValidationErrorMapping— wraps each function-valued adapter method in a try/catch; non-function props pass through. Applied to the productioncreateObjectQLAdapterFactoryadapter.Net effect:
update-user(and any other better-auth write) now returns400 { code: 'VALIDATION_FAILED', message, fields }instead of a500empty body.Test
packages/plugins/plugin-auth/src/objectql-adapter.test.ts— a new suite (4 cases), including an end-to-end case through the productioncreateObjectQLAdapterFactory: a real engine that rejects an invalidimageonupdate→ asserts the caught error is better-auth's realAPIError,statusCode === 400, body carriesVALIDATION_FAILED. Also covers: non-validation errors re-thrown verbatim, and success / non-function props passing through untouched.vitest run src/objectql-adapter.test.ts→ 37 passed. Changed files are clean undertsc --noEmit(the one pre-existingsecondary-storage.tserror is unrelated).Notes
Noticed while fixing the avatar-upload 500 (data: URIs rejected by the url validator — fixed separately on
claude/user-avatar-upload-error-d4c8e5). This PR is the generic mapping gap, independent of that specific validator case.🤖 Generated with Claude Code