-
Notifications
You must be signed in to change notification settings - Fork 1
Fix Zod schema error for optional wp:featuredmedia fields and add schema validation to smoke tests #940
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
Draft
Copilot
wants to merge
7
commits into
master
Choose a base branch
from
copilot/fix-zod-schema-error
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Fix Zod schema error for optional wp:featuredmedia fields and add schema validation to smoke tests #940
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
637ed18
Initial plan
Copilot db59efd
Add test to verify Zod schema fix for optional wp:featuredmedia fields
Copilot 0bf2e84
Fix Zod schema to make wp:featuredmedia fields optional
Copilot a8631c6
Export WpJsonFeedEntrySchema and import in tests instead of inlining
Copilot 4923404
Add explicit smoke test validation for /v1/news/named/stolaf endpoint
Copilot f55df85
Add Zod schema validation script for smoke tests
Copilot ca54c55
extend the timeout on the bamco endpoint tests
hawkrives File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,221 @@ | ||
| import {test, expect} from 'vitest' | ||
| import {z} from 'zod' | ||
| import {convertWpJsonItemToStory} from './wp-json.js' | ||
|
|
||
| // Import the schema to test it directly | ||
| type WpJsonFeedEntryType = z.infer<typeof WpJsonFeedEntrySchema> | ||
| const WpJsonFeedEntrySchema = z.object({ | ||
| _embedded: z.optional( | ||
| z.object({ | ||
| author: z.array(z.object({id: z.unknown(), name: z.string().or(z.undefined())})).optional(), | ||
| 'wp:featuredmedia': z | ||
| .array( | ||
| z.object({ | ||
| id: z.unknown(), | ||
| media_type: z.union([z.literal('image'), z.string()]).optional(), | ||
| media_details: z | ||
| .object({ | ||
| sizes: z.optional(z.record(z.object({source_url: z.string().url()}))), | ||
| }) | ||
| .optional(), | ||
| source_url: z.string().url().optional(), | ||
| }), | ||
| ) | ||
| .nullable() | ||
| .optional(), | ||
| 'wp:term': z.array(z.array(z.object({taxonomy: z.string(), name: z.string()}))), | ||
| }), | ||
| ), | ||
| /** this is "author ID," not "author name" */ | ||
| author: z.unknown(), | ||
| featured_media: z.number().optional(), | ||
| content: z.object({rendered: z.string()}), | ||
| excerpt: z.object({rendered: z.string()}), | ||
| title: z.object({rendered: z.string()}), | ||
| date_gmt: z.string(), | ||
| link: z.string().url(), | ||
| }) | ||
|
|
||
| test('WpJsonFeedEntrySchema should parse items with missing featuredmedia fields', () => { | ||
| const itemWithMissingFields = { | ||
| _embedded: { | ||
| author: [{id: 1, name: 'Test Author'}], | ||
| 'wp:featuredmedia': [ | ||
| { | ||
| id: 123, | ||
| // missing media_type, media_details, and source_url | ||
| }, | ||
| ], | ||
| 'wp:term': [[{taxonomy: 'category', name: 'News'}]], | ||
| }, | ||
| author: 1, | ||
| featured_media: 123, | ||
| content: {rendered: '<p>Test content</p>'}, | ||
| excerpt: {rendered: '<p>Test excerpt</p>'}, | ||
| title: {rendered: 'Test Title'}, | ||
| date_gmt: '2024-01-01T12:00:00', | ||
| link: 'https://example.com/post', | ||
| } | ||
|
|
||
| expect(() => WpJsonFeedEntrySchema.parse(itemWithMissingFields)).not.toThrow() | ||
| }) | ||
|
|
||
| test('WpJsonFeedEntrySchema should parse items with complete featuredmedia fields', () => { | ||
| const itemWithCompleteFields = { | ||
| _embedded: { | ||
| author: [{id: 1, name: 'Test Author'}], | ||
| 'wp:featuredmedia': [ | ||
| { | ||
| id: 123, | ||
| media_type: 'image', | ||
| media_details: { | ||
| sizes: { | ||
| medium_large: { | ||
| source_url: 'https://example.com/image-medium.jpg', | ||
| }, | ||
| }, | ||
| }, | ||
| source_url: 'https://example.com/image.jpg', | ||
| }, | ||
| ], | ||
| 'wp:term': [[{taxonomy: 'category', name: 'News'}]], | ||
| }, | ||
| author: 1, | ||
| featured_media: 123, | ||
| content: {rendered: '<p>Test content</p>'}, | ||
| excerpt: {rendered: '<p>Test excerpt</p>'}, | ||
| title: {rendered: 'Test Title'}, | ||
| date_gmt: '2024-01-01T12:00:00', | ||
| link: 'https://example.com/post', | ||
| } | ||
|
|
||
| expect(() => WpJsonFeedEntrySchema.parse(itemWithCompleteFields)).not.toThrow() | ||
| }) | ||
|
|
||
| test('WpJsonFeedEntrySchema should parse items with null wp:featuredmedia', () => { | ||
| const itemWithNullFeaturedMedia = { | ||
| _embedded: { | ||
| author: [{id: 1, name: 'Test Author'}], | ||
| 'wp:featuredmedia': null, | ||
| 'wp:term': [[{taxonomy: 'category', name: 'News'}]], | ||
| }, | ||
| author: 1, | ||
| content: {rendered: '<p>Test content</p>'}, | ||
| excerpt: {rendered: '<p>Test excerpt</p>'}, | ||
| title: {rendered: 'Test Title'}, | ||
| date_gmt: '2024-01-01T12:00:00', | ||
| link: 'https://example.com/post', | ||
| } | ||
|
|
||
| expect(() => WpJsonFeedEntrySchema.parse(itemWithNullFeaturedMedia)).not.toThrow() | ||
| }) | ||
|
|
||
| test('convertWpJsonItemToStory should handle missing featuredmedia fields', () => { | ||
| const itemWithMissingFields: WpJsonFeedEntryType = { | ||
| _embedded: { | ||
| author: [{id: 1, name: 'Test Author'}], | ||
| 'wp:featuredmedia': [ | ||
| { | ||
| id: 123, | ||
| // missing media_type, media_details, and source_url | ||
| }, | ||
| ], | ||
| 'wp:term': [[{taxonomy: 'category', name: 'News'}]], | ||
| }, | ||
| author: 1, | ||
| featured_media: 123, | ||
| content: {rendered: '<p>Test content</p>'}, | ||
| excerpt: {rendered: '<p>Test excerpt</p>'}, | ||
| title: {rendered: 'Test Title'}, | ||
| date_gmt: '2024-01-01T12:00:00', | ||
| link: 'https://example.com/post', | ||
| } | ||
|
|
||
| // Should not throw and featuredImage should be null when fields are missing | ||
| const result = convertWpJsonItemToStory(itemWithMissingFields) | ||
| expect(result.featuredImage).toBe(null) | ||
| }) | ||
|
|
||
| test('convertWpJsonItemToStory should handle complete featuredmedia fields', () => { | ||
| const itemWithCompleteFields: WpJsonFeedEntryType = { | ||
| _embedded: { | ||
| author: [{id: 1, name: 'Test Author'}], | ||
| 'wp:featuredmedia': [ | ||
| { | ||
| id: 123, | ||
| media_type: 'image', | ||
| media_details: { | ||
| sizes: { | ||
| medium_large: { | ||
| source_url: 'https://example.com/image-medium.jpg', | ||
| }, | ||
| }, | ||
| }, | ||
| source_url: 'https://example.com/image.jpg', | ||
| }, | ||
| ], | ||
| 'wp:term': [[{taxonomy: 'category', name: 'News'}]], | ||
| }, | ||
| author: 1, | ||
| featured_media: 123, | ||
| content: {rendered: '<p>Test content</p>'}, | ||
| excerpt: {rendered: '<p>Test excerpt</p>'}, | ||
| title: {rendered: 'Test Title'}, | ||
| date_gmt: '2024-01-01T12:00:00', | ||
| link: 'https://example.com/post', | ||
| } | ||
|
|
||
| const result = convertWpJsonItemToStory(itemWithCompleteFields) | ||
| expect(result.featuredImage).toBe('https://example.com/image-medium.jpg') | ||
| }) | ||
|
|
||
| test('convertWpJsonItemToStory should handle null wp:featuredmedia', () => { | ||
| const itemWithNullFeaturedMedia: WpJsonFeedEntryType = { | ||
| _embedded: { | ||
| author: [{id: 1, name: 'Test Author'}], | ||
| 'wp:featuredmedia': null, | ||
| 'wp:term': [[{taxonomy: 'category', name: 'News'}]], | ||
| }, | ||
| author: 1, | ||
| content: {rendered: '<p>Test content</p>'}, | ||
| excerpt: {rendered: '<p>Test excerpt</p>'}, | ||
| title: {rendered: 'Test Title'}, | ||
| date_gmt: '2024-01-01T12:00:00', | ||
| link: 'https://example.com/post', | ||
| } | ||
|
|
||
| const result = convertWpJsonItemToStory(itemWithNullFeaturedMedia) | ||
| expect(result.featuredImage).toBe(null) | ||
| }) | ||
|
|
||
| test('WpJsonFeedEntrySchema parsing should work with real-world incomplete data', () => { | ||
| // This simulates what WordPress might return when a featured media | ||
| // entry exists but has incomplete information | ||
| const incompleteData = { | ||
| _embedded: { | ||
| author: [{id: 1, name: 'Test Author'}], | ||
| 'wp:featuredmedia': [ | ||
| { | ||
| id: 123, | ||
| // All other fields missing | ||
| }, | ||
| ], | ||
| 'wp:term': [[{taxonomy: 'category', name: 'News'}]], | ||
| }, | ||
| author: 1, | ||
| featured_media: 123, | ||
| content: {rendered: '<p>Test content</p>'}, | ||
| excerpt: {rendered: '<p>Test excerpt</p>'}, | ||
| title: {rendered: 'Test Title'}, | ||
| date_gmt: '2024-01-01T12:00:00', | ||
| link: 'https://example.com/post', | ||
| } | ||
|
|
||
| // Should parse without error | ||
| const parsed = WpJsonFeedEntrySchema.parse(incompleteData) | ||
| expect(parsed).toBeDefined() | ||
|
|
||
| // Convert should also work and return null for featuredImage | ||
| const converted = convertWpJsonItemToStory(parsed) | ||
| expect(converted.featuredImage).toBe(null) | ||
| }) | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.