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

fix(pages-components): schema type validation #75

Merged
merged 11 commits into from
Jan 7, 2025
Merged

Conversation

benlife5
Copy link
Contributor

@benlife5 benlife5 commented Jan 6, 2025

As far as I can tell, faq is the only place where a bad input type would cause an error as opposed to just returning undefined/empty data.

@mkilpatrick Do we want to throw errors or print warnings in other places?

For example:

const Product = (document: Record<string, any>, schemaType?: string) => {
  return {
    ...BaseSchema(document, schemaType ?? "Product"),
    ...PhotoGallerySchema(document.photoGallery),
    ...ReviewSchema(document.c_reviews),
    ...AggregateRatingSchema(document.c_aggregateRating),
    ...OfferSchema({
      url: "",
      priceCurrency: document.c_currency,
      price: document.price,
      priceValidUntil: document.expirationDate,
      itemCondition: document.stockStatus,
      availability: document.availabilityDate,
    }),
    description: document.description,
    sku: document.sku,
    mpn: document.mpn,
    brand: {
      "@type": "Brand",
      name: document.brand,
    },
  };
};

export const AggregateRatingSchema = (rating?: AggregateRating) => {
  return (
    rating && {
      aggregateRating: {
        "@type": "AggregateRating",
        ratingValue: rating.ratingValue,
        reviewCount: rating.reviewCount,
      },
    }
  );
};

If there is no c_aggregateRating field on the document, aggregateRating won't appear in the returned schema.

If c_aggregateRating is on the document but is a Number instead of an Object, Product will return undefined values

...
  aggregateRating: {
    '@type': 'AggregateRating',
    ratingValue: undefined,
    reviewCount: undefined
  },

Copy link

changeset-bot bot commented Jan 6, 2025

⚠️ No Changeset found

Latest commit: da84727

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

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

@mkilpatrick
Copy link
Collaborator

I think we return nothing if the object isn't what we expect. Confirm with @apav-dev.

@benlife5 benlife5 changed the title fix(pages-components): faq type check fix(pages-components): schema type validation Jan 6, 2025
asanehisa
asanehisa previously approved these changes Jan 6, 2025
mkilpatrick
mkilpatrick previously approved these changes Jan 6, 2025
@benlife5 benlife5 dismissed stale reviews from mkilpatrick and asanehisa via 6dc7a69 January 7, 2025 17:44
@benlife5 benlife5 merged commit fc56271 into main Jan 7, 2025
13 checks passed
@benlife5 benlife5 deleted the schema-type-checking branch January 7, 2025 17:50
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.

3 participants