Skip to main content

validate_jsonld

Function validate_jsonld 

Source
pub fn validate_jsonld(html: &str) -> Vec<JsonLdValidationError>
Expand description

Walks an HTML string, extracts every <script type="application/ld+json"> block, parses it as JSON, and validates required fields per schema.org @type.

Supported types (with their required-field guards):

  • Article — headline, datePublished, author, image
  • WebPage — name (Google rich-results requirement; url and inLanguage are Recommended only and not flagged here)
  • BreadcrumbList — itemListElement (non-empty array)
  • FAQPage — mainEntity (non-empty array of Question)
  • LocalBusiness — name, address
  • Organization — name, url

Returns the empty vector if every block parses and passes its required-field check. Unknown @type values are treated as pass-through (no required fields enforced) so user-extended schemas don’t trigger false negatives.

§Examples

use ssg::seo::validate_jsonld;

let html = r#"<script type="application/ld+json">
{"@type":"Article","headline":"x","datePublished":"2024","author":"y","image":"i"}
</script>"#;
assert!(validate_jsonld(html).is_empty());