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

Improve errors for invalid IDs in content collections #12892

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

louisescher
Copy link
Contributor

@louisescher louisescher commented Jan 5, 2025

Changes

This PR improves the error messages for IDs in content collections.

IDs in content collections have always been required to be strings, however the error message when passing (for example) a number would look like this:

[ContentLoaderInvalidDataError] todos entry is missing an ID.
Entry missing ID:
{
  "userId": 1,
  "title": "delectus aut autem",
  "completed": false
}

This would even be the case if the original entry looked like this:

{
  "userId": 1,
  "id": 1,
  "title": "delectus aut autem",
  "completed": false
}

This PR changes these error messages so that they throw a new and more verbose AstroError based on the Zod validation.
For this to work, the parsing logic had to be moved from the functions return type schema to an external schema, and the validation had to be moved into the simpleLoader function itself due to performance issues with Zod when using z.function().returns(...).

Testing

The changes in this PR should be covered by the existing tests as no behavior has been added or changed.

Docs

/cc @withastro/maintainers-docs for feedback!

A docs PR has been made here

Copy link

changeset-bot bot commented Jan 5, 2025

🦋 Changeset detected

Latest commit: 99397b0

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

codspeed-hq bot commented Jan 5, 2025

CodSpeed Performance Report

Merging #12892 will not alter performance

Comparing louisescher:verbose-id-errors (99397b0) with main (f00c2dd)

Summary

✅ 6 untouched benchmarks

@ascorbic
Copy link
Contributor

ascorbic commented Jan 5, 2025

Great! A definite improvement. Could you add a test, checking that it throws the right error when passed a number for ID? Also can you add a patch changeset? Thanks!

@louisescher
Copy link
Contributor Author

@ascorbic Will do!

Copy link
Member

@sarah11918 sarah11918 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just commenting on the error message for docs in general! I indicated a couple of places (but would apply throughout if appropriate) where this might be targeted specifically to IDs, if that is in fact what this is solely focused on.

I'll leave it to y'all, but just pointing out that if it makes sense to be more specific, it's often more helpful and informative to the user! (Also easier to come up with another error name in the case of a different kind of "invalid" result. 😄 )

packages/astro/src/core/errors/errors-data.ts Outdated Show resolved Hide resolved
packages/astro/src/core/errors/errors-data.ts Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs pr pkg: astro Related to the core `astro` package (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants