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

Enable validation/transformation of JSON:API responses #471

Merged
merged 2 commits into from
Jun 12, 2023

Conversation

dbanck
Copy link
Contributor

@dbanck dbanck commented Jun 12, 2023

APIs that follow the JSON:API specification use application/vnd.api+json instead of application/json as the value of the Content-Type header on responses.

zodios returns these responses without validation or transformation; this PR updates that behaviour to transform them as well.

It's safe to validate/transform these responses as they are still plain JSON, see 7 Document Structure.

@ecyrbe
Copy link
Owner

ecyrbe commented Jun 12, 2023

Thank you.

@ecyrbe ecyrbe merged commit 7a23d4e into ecyrbe:main Jun 12, 2023
5 checks passed
@dbanck
Copy link
Contributor Author

dbanck commented Jun 12, 2023

Thanks for the quick review & merge 🌟

@dbanck dbanck deleted the patch-1 branch June 12, 2023 10:46
@ecyrbe
Copy link
Owner

ecyrbe commented Jun 12, 2023

Build in progress and new package in v10.9.2

@matis-dk
Copy link

@ecyrbe I'm running into a similar issue, were the origin webserver is returning 'content-type': 'text/html; in some circumstances. This skip the runtime validation, but still cast the response type. Essentially lying about the type (because the html payload is a string instead of the specified schema type). Is there anyway, I can enforce / opt into safe parsing the payload? This would at least catch non-conforming responses.

@ecyrbe
Copy link
Owner

ecyrbe commented Jun 20, 2023

@matis-dk be careful, actually the server may not be lying.
If the content has quotes like this :

"some json string"

It's actually lying, but if it does not contain quotes, it's actually really a text content and zodios and JSON.parse is right to not validate.
If you actually have control on the server, do a JSON.stringify to make it valid json.
Else, don't try to validate the string.

@rogerweb
Copy link

zodios and JSON.parse is right to not validate.

I'm trying to understand why that is the case. According to the official documentation of the Zodios client:

All your parameters and responses are fully typed. And by default they are also validated at runtime to prevent unrecoverable errors.

I expected the client to throw an error in any scenario where it didn't receive a valid object.

Else, don't try to validate the string.

Could you clarify how I can achieve this? Should I skip Zodios, perform the fetch myself and use Zod directly to validate the response?

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.

None yet

4 participants