Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 6, 2025

Problem

The /v1/news/named/stolaf endpoint was returning 500 errors due to Zod schema validation failures. The WordPress JSON API sometimes returns wp:featuredmedia array items with incomplete data, where media_type, media_details, and source_url fields are undefined.

Solution

Made the following fields optional in the WpJsonFeedEntrySchema:

  • media_type: Changed from required to .optional()
  • media_details: Changed from required to .optional()
  • source_url: Changed from required to .optional()

Updated convertWpJsonItemToStory to safely handle these optional fields using optional chaining (?.) and added explicit null coalescing to ensure featuredImage defaults to null when fields are missing.

Testing

Unit Tests

Added comprehensive test coverage in source/feeds/wp-json.test.ts:

  • ✅ Schema parsing with missing featuredmedia fields
  • ✅ Schema parsing with complete featuredmedia fields
  • ✅ Schema parsing with null wp:featuredmedia
  • ✅ Story conversion handles missing fields gracefully
  • ✅ Real-world incomplete data scenarios

All tests pass (7 new tests added). Schemas are exported from source and imported in tests to avoid duplication.

Smoke Tests

Created a schema validation system for smoke tests:

  • New validation script (scripts/validate-schema.ts): Validates API responses against their Zod schemas by dynamically importing the schema modules
  • Updated smoke tests: /v1/news/named/stolaf and /v1/news/named/krlx endpoints now validate responses against their actual Zod schemas instead of manual JSON structure checks
  • Schema registry: Maintains mappings between endpoints and their schemas for easy extensibility

This provides strong protection against regressions since we don't control the upstream APIs. The validation uses the same Zod schemas that the application uses, ensuring smoke tests validate against the actual contract.

Impact

This fix is backwards compatible. Posts with complete featured media information continue to work as before, while posts with incomplete or missing featured media fields now parse successfully without throwing validation errors.

Fixes #908

Original prompt

This section details on the original issue you should resolve

<issue_title>Zod schema error for /v1/news/named/stolaf</issue_title>
<issue_description>```sh
xxx GET /v1/news/named/stolaf 500 813ms -

ZodError: [
{
"code": "invalid_union",
"unionErrors": [
{
"issues": [
{
"code": "invalid_literal",
"expected": "image",
"path": [
3,
"_embedded",
"wp:featuredmedia",
0,
"media_type"
],
"message": "Invalid literal value, expected "image""
}
],
"name": "ZodError"
},
{
"issues": [
{
"code": "invalid_type",
"expected": "string",
"received": "undefined",
"path": [
3,
"_embedded",
"wp:featuredmedia",
0,
"media_type"
],
"message": "Required"
}
],
"name": "ZodError"
}
],
"path": [
3,
"_embedded",
"wp:featuredmedia",
0,
"media_type"
],
"message": "Invalid input"
},
{
"code": "invalid_type",
"expected": "object",
"received": "undefined",
"path": [
3,
"_embedded",
"wp:featuredmedia",
0,
"media_details"
],
"message": "Required"
},
{
"code": "invalid_type",
"expected": "string",
"received": "undefined",
"path": [
3,
"_embedded",
"wp:featuredmedia",
0,
"source_url"
],
"message": "Required"
}
]


## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>

Fixes #908

💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 2 commits October 6, 2025 22:31
Co-authored-by: hawkrives <464441+hawkrives@users.noreply.github.com>
Co-authored-by: hawkrives <464441+hawkrives@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix Zod schema error for news endpoint Fix Zod schema error for optional wp:featuredmedia fields in WordPress JSON feeds Oct 6, 2025
Copilot AI requested a review from hawkrives October 6, 2025 22:43
Co-authored-by: hawkrives <464441+hawkrives@users.noreply.github.com>
Copilot AI requested a review from hawkrives October 6, 2025 22:48
drewvolz
drewvolz previously approved these changes Oct 6, 2025
@hawkrives
Copy link
Member

@copilot Please add this to the existing St. Olaf smoke tests. As we don't control the upstream endpoint, we can't rely solely on unit tests to ensure that this continues to work in production.

Co-authored-by: hawkrives <464441+hawkrives@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Oct 6, 2025

@copilot Please add this to the existing St. Olaf smoke tests. As we don't control the upstream endpoint, we can't rely solely on unit tests to ensure that this continues to work in production.

Added explicit validation for /v1/news/named/stolaf in the smoke test script. The test now:

  1. Validates the endpoint returns valid JSON
  2. Verifies the response is an array
  3. Checks that items contain required fields (title, link, datePublished)

This ensures the WordPress JSON feed parsing with optional wp:featuredmedia fields continues to work in production. Commit: 51e5c25

@hawkrives
Copy link
Member

@copilot For each endpoint which has a Zod schema, I'd like to have a smoke test which ensures that the response is valid according to the schema.

Co-authored-by: hawkrives <464441+hawkrives@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Oct 6, 2025

@copilot For each endpoint which has a Zod schema, I'd like to have a smoke test which ensures that the response is valid according to the schema.

Created scripts/validate-schema.ts that validates API responses against their Zod schemas. Updated smoke tests to use this script for /v1/news/named/stolaf and /v1/news/named/krlx endpoints.

The script:

  • Maintains a registry mapping endpoints to their schemas
  • Dynamically imports and validates against the actual Zod schemas
  • Returns detailed error messages on validation failures

This ensures responses match the schema definitions and will catch any future regressions. Commit: 0a3c962

Copilot AI changed the title Fix Zod schema error for optional wp:featuredmedia fields in WordPress JSON feeds Fix Zod schema error for optional wp:featuredmedia fields and add schema validation to smoke tests Oct 6, 2025
@hawkrives hawkrives marked this pull request as ready for review October 6, 2025 23:39
@hawkrives hawkrives requested a review from rye as a code owner October 6, 2025 23:39
@hawkrives hawkrives force-pushed the copilot/fix-zod-schema-error branch from 4f64a9e to ca54c55 Compare October 7, 2025 00:00
;;

"/v1/news/named/stolaf" | "/v1/news/named/krlx")
# Validate news endpoints with Zod schema
Copy link
Member

Choose a reason for hiding this comment

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

This comment doesn't add much value.

echo "validating $route with Zod schema"
RESPONSE=$(curl --silent --fail "localhost:3000$route")
# Validate against the Zod schema
echo "$RESPONSE" | node dist/scripts/validate-schema.js "$route"
Copy link
Member

Choose a reason for hiding this comment

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

This is the only place in this shell script where we depend on the script having somehow gotten to this path. We should not begin the tests unless the script is present here.

Are the Zod checks applied on ccc output (responses we send to apps) or input (responses we get from upstreams?)?

If they're applied on upstreams, then this test is also not actually testing what we want — in that case it's testing that… the output happens to also conform to the input spec? Which doesn't apply to other types of routes like food menus.

Why not just let these two routes

@hawkrives hawkrives marked this pull request as draft October 8, 2025 01:48
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.

Zod schema error for /v1/news/named/stolaf

4 participants