validate static method
Validate a JSON-LD schema and return a list of issues found.
Implementation
static List<ValidationIssue> validate(JsonLdSchema schema) {
final issues = <ValidationIssue>[];
final map = schema.toMap();
// Check @type exists
if (!map.containsKey('@type')) {
issues.add(
const ValidationIssue(
severity: IssueSeverity.error,
message: 'Missing @type — every schema must have a @type field',
field: '@type',
),
);
return issues; // Can't validate further without type
}
final type = map['@type'] as String;
// Type-specific validation
switch (type) {
case 'Product':
_validateProduct(map, issues);
break;
case 'Article':
case 'NewsArticle':
case 'BlogPosting':
case 'TechArticle':
_validateArticle(map, issues);
break;
case 'Organization':
_validateOrganization(map, issues);
break;
case 'FAQPage':
_validateFaq(map, issues);
break;
case 'BreadcrumbList':
_validateBreadcrumb(map, issues);
break;
case 'LocalBusiness':
case 'Restaurant':
case 'Store':
_validateLocalBusiness(map, issues);
break;
case 'Event':
_validateEvent(map, issues);
break;
}
// Validate JSON output
try {
final jsonStr = schema.toJsonLd();
jsonDecode(jsonStr); // Make sure it's valid JSON
} catch (e) {
issues.add(
ValidationIssue(
severity: IssueSeverity.error,
message: 'Generated JSON-LD is not valid JSON: $e',
),
);
}
return issues;
}