crawlscope 0.12.1 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +26 -0
- data/README.md +60 -0
- data/UPGRADE.md +29 -0
- data/docs/architecture.md +19 -0
- data/lib/crawlscope/schema_registry.rb +3 -88
- data/lib/crawlscope/schemas.rb +67 -5
- data/lib/crawlscope/structured_data/builder.rb +127 -0
- data/lib/crawlscope/structured_data/properties.rb +42 -0
- data/lib/crawlscope/structured_data/schema_recommendations.rb +54 -0
- data/lib/crawlscope/structured_data/schema_validation.rb +84 -0
- data/lib/crawlscope/structured_data/serialization.rb +15 -0
- data/lib/crawlscope/structured_data/validator.rb +6 -6
- data/lib/crawlscope/version.rb +1 -1
- data/test/crawlscope/schema_registry_test.rb +33 -0
- data/test/crawlscope/structured_data_builder_test.rb +135 -0
- metadata +7 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: fa7ccbc9a8a12e7e8dc50fa443b190987d9d9d67ab6f4146069c624b18c8a30a
|
|
4
|
+
data.tar.gz: afc353d212d0e4ff6468395b20034c0ad85e0c3d182c55e792e7759e8796b519
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6c47c54c0dd4ba105a50b0f150c8c95d0ec5b7eccd2479fccf6282dc676edac6a02b3383cc3b0d1770774745e8c9ed4efa83ab5756dd59cf9b7024a72996c911
|
|
7
|
+
data.tar.gz: 6b48818c240627a94554b74e6fbc94cb3459fae45091de57e5674a1237c76ec75fe8720da18b07e37e7294e0dd0bb032c08bc7d1c73d540ef329127d5b1bcb32
|
data/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,32 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [0.13.0] - 2026-12-03
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Added
|
|
12
|
+
|
|
13
|
+
- add reusable JSON-LD builders
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### Changed
|
|
19
|
+
|
|
20
|
+
- separate validation from registry storage
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
### Upgrade instructions
|
|
25
|
+
|
|
26
|
+
- Update the dependency to 0.13.0 and run `bundle update crawlscope --conservative`.
|
|
27
|
+
- Replace generic host constructors with `Crawlscope::StructuredData::Builder`;
|
|
28
|
+
keep domain facts and page classification in the host.
|
|
29
|
+
- Use `StructuredData::Serialization.json` for the exact CSP-registered script
|
|
30
|
+
payload. Verify newly covered place and TechArticle markup using the
|
|
31
|
+
independent validator. See the [0.13.0 upgrade guide](https://github.com/ethos-link/crawlscope/blob/v0.13.0/UPGRADE.md#0130).
|
|
32
|
+
|
|
33
|
+
|
|
8
34
|
## [0.12.1] - 2026-12-03
|
|
9
35
|
|
|
10
36
|
|
data/README.md
CHANGED
|
@@ -82,6 +82,66 @@ gem "ferrum"
|
|
|
82
82
|
|
|
83
83
|
`crawlscope` only loads Ferrum when you run in browser mode. Install a compatible Chrome or Chromium executable as well.
|
|
84
84
|
|
|
85
|
+
## Build structured data
|
|
86
|
+
|
|
87
|
+
Use the builder to express application-owned facts as Schema.org JSON-LD.
|
|
88
|
+
It does not fetch pages, run a validation audit, or depend on Rails.
|
|
89
|
+
|
|
90
|
+
```ruby
|
|
91
|
+
require "crawlscope"
|
|
92
|
+
|
|
93
|
+
schema = Crawlscope::StructuredData::Builder.new(
|
|
94
|
+
base_url: "https://example.com",
|
|
95
|
+
organization: {name: "Example", logo: "https://example.com/logo.png"},
|
|
96
|
+
language: "en"
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
article = schema.article(
|
|
100
|
+
headline: "A guide",
|
|
101
|
+
date_published: Date.new(2026, 1, 2),
|
|
102
|
+
main_entity_of_page: "https://example.com/guide",
|
|
103
|
+
author: schema.person(name: "Jane Smith")
|
|
104
|
+
)
|
|
105
|
+
breadcrumbs = schema.breadcrumb([
|
|
106
|
+
{name: "Home", item: "https://example.com"},
|
|
107
|
+
{name: "A guide", item: "https://example.com/guide"}
|
|
108
|
+
])
|
|
109
|
+
payload = Crawlscope::StructuredData::Serialization.json(
|
|
110
|
+
schema.graph(article, breadcrumbs)
|
|
111
|
+
)
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Constructors return ordinary hashes with string Schema.org keys. Snake-case
|
|
115
|
+
symbol keys become lower camel case, and `:id` becomes `"@id"`; literal string
|
|
116
|
+
keys are preserved. Dates and times become ISO 8601 strings. Nil hash values
|
|
117
|
+
are omitted; zero and false remain. Nested nodes and graphs inherit the
|
|
118
|
+
standard context. Other properties can be supplied explicitly as keywords.
|
|
119
|
+
|
|
120
|
+
- Identity: `organization`, `person`, `website`.
|
|
121
|
+
- Navigation: `webpage`, `collection_page`, `item_list(items)`, `breadcrumb(items)`.
|
|
122
|
+
- Editorial: `article`, `faq(items)` (question/answer pairs), `how_to(name:, steps:)`.
|
|
123
|
+
- Commercial: `software_application`, `web_application`, `product`, `offer`.
|
|
124
|
+
- Places: `local_business(name:, type:)`, `postal_address`, `geo_coordinates`,
|
|
125
|
+
`opening_hours`.
|
|
126
|
+
- Research: `dataset`, `data_catalog`, `data_download`.
|
|
127
|
+
- Extension: `node(type, **properties)` for other Schema.org types.
|
|
128
|
+
|
|
129
|
+
The builder numbers breadcrumb, list and instruction entries from one. The
|
|
130
|
+
configured organization becomes the default publisher for articles and
|
|
131
|
+
applications; the configured language applies to articles, web pages, websites
|
|
132
|
+
and datasets. Override these defaults per call, including with nil to omit them.
|
|
133
|
+
The base URL is the default organization and website URL; other URLs must come
|
|
134
|
+
from the host. Publish only accurate facts that correspond to the page. The
|
|
135
|
+
builder does not infer ratings, dates, price, license, author or place category.
|
|
136
|
+
|
|
137
|
+
`Serialization.json` returns a JSON string escaped for a script element;
|
|
138
|
+
it is not marked HTML-safe. Rails applications can insert the payload as raw
|
|
139
|
+
content in a JSON-LD script after registering it with their own CSP policy.
|
|
140
|
+
Keep nonce, script hashes and HTML rendering in the host. Use the same
|
|
141
|
+
`SchemaRegistry` or the independent HTML validator in tests. Constructors do
|
|
142
|
+
not automatically validate; unsupported types remain available through
|
|
143
|
+
`node` and can be registered for validation.
|
|
144
|
+
|
|
85
145
|
## CLI Usage
|
|
86
146
|
|
|
87
147
|
Validate a site from its default sitemap:
|
data/UPGRADE.md
CHANGED
|
@@ -3,6 +3,35 @@
|
|
|
3
3
|
Run `bundle exec rake verify` locally before pushing or preparing a release.
|
|
4
4
|
GitHub Actions runs commitlint and release packaging/publication only.
|
|
5
5
|
|
|
6
|
+
## 0.13.0
|
|
7
|
+
|
|
8
|
+
### Shared structured data generation
|
|
9
|
+
|
|
10
|
+
Update the host's Crawlscope dependency to 0.13.0 and run
|
|
11
|
+
`bundle update crawlscope --conservative`. Require `crawlscope` at the
|
|
12
|
+
structured-data generation boundary (the builder does not load the HTTP,
|
|
13
|
+
HTML parsing or validation stack).
|
|
14
|
+
|
|
15
|
+
Replace application-local generic constructors with
|
|
16
|
+
`Crawlscope::StructuredData::Builder.new(base_url:, organization:, language:)`.
|
|
17
|
+
Pass actual application facts into its named constructors; retain domain
|
|
18
|
+
mapping, publication policy, licensing and pricing in the host. Use
|
|
19
|
+
`builder.node(type, **properties)` for additional types and
|
|
20
|
+
`builder.graph(*nodes)` for a shared JSON-LD context. Constructors return
|
|
21
|
+
string-keyed hashes. Snake-case symbol keys become Schema.org keys, nil
|
|
22
|
+
properties are omitted, and false and zero are preserved. Explicitly supply
|
|
23
|
+
dates, authors, ratings and other factual values.
|
|
24
|
+
|
|
25
|
+
Use `Crawlscope::StructuredData::Serialization.json(data)` for the script
|
|
26
|
+
payload. Register those exact bytes with the host's CSP implementation and
|
|
27
|
+
render the payload as raw script content. Do not HTML-escape it again.
|
|
28
|
+
|
|
29
|
+
Update audit expectations for newly validated TechArticle and place types.
|
|
30
|
+
Configure `required_structured_data_types` from actual host page semantics,
|
|
31
|
+
not crawler guesses. Retain stricter host-specific registry extensions, such
|
|
32
|
+
as dataset licensing. Verify rendered markup and both audit paths before
|
|
33
|
+
removing superseded helpers. See [the builder API](README.md#build-structured-data).
|
|
34
|
+
|
|
6
35
|
## 0.9.1
|
|
7
36
|
|
|
8
37
|
Development and release tooling uses Gemkit 0.3.1. Update the gem and workflow
|
data/docs/architecture.md
CHANGED
|
@@ -29,8 +29,13 @@ lib/crawlscope/
|
|
|
29
29
|
canonical_checks.rb Canonical target status and inbound-link policies
|
|
30
30
|
url_checks.rb URL hygiene
|
|
31
31
|
structured_data/
|
|
32
|
+
builder.rb Typed JSON-LD constructors and instance defaults
|
|
33
|
+
properties.rb Schema.org key and value normalization
|
|
34
|
+
serialization.rb JSON payload escaping for script elements
|
|
32
35
|
document.rb JSON-LD and microdata extraction
|
|
33
36
|
validator.rb Shared parse, type-presence, and schema validation
|
|
37
|
+
schema_validation.rb Schema traversal and JSON Schema error details
|
|
38
|
+
schema_recommendations.rb Advisory properties and unsupported types
|
|
34
39
|
entry.rb Per-URL outcome and failure classification
|
|
35
40
|
outcome.rb Overall success derived from entries
|
|
36
41
|
audit.rb Fetches direct URLs and adapts validation findings
|
|
@@ -49,6 +54,20 @@ retains full URL identity. Inbound and canonical checks ask the same graph for
|
|
|
49
54
|
counts instead of maintaining separate counting algorithms. Path inclusion
|
|
50
55
|
policy lives in `Scope`.
|
|
51
56
|
|
|
57
|
+
Generation and auditing are independent. The builder maps supplied facts to
|
|
58
|
+
Schema.org hashes; it never calls the schema registry or audit pipeline. The
|
|
59
|
+
host owns page semantics, factual values, CSP and rendering. Serialization
|
|
60
|
+
escapes the JSON payload without depending on Rails. The registry validates
|
|
61
|
+
known structural contracts, including inherited graph context, but cannot
|
|
62
|
+
establish factual accuracy or search-engine eligibility.
|
|
63
|
+
|
|
64
|
+
`SchemaRegistry` owns type registration, lookup and independent registry
|
|
65
|
+
copies. Its existing validation and warning APIs delegate to separate
|
|
66
|
+
structural and advisory checks using the current registry. Structural checks
|
|
67
|
+
inherit graph context, validate each declared type and translate JSON Schema
|
|
68
|
+
errors; advisory checks interpret recommended properties, including dataset
|
|
69
|
+
downloads. Neither check changes the supplied document or registry.
|
|
70
|
+
|
|
52
71
|
Both structured-data entry points use `StructuredData::Validator`. It returns
|
|
53
72
|
items and findings without writing output or choosing command exit status.
|
|
54
73
|
`Rules::StructuredData` translates findings into the sitemap audit's existing
|
|
@@ -1,9 +1,5 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require "json-schema"
|
|
4
|
-
|
|
5
|
-
JSON::Validator.use_multi_json = false
|
|
6
|
-
|
|
7
3
|
module Crawlscope
|
|
8
4
|
class SchemaRegistry
|
|
9
5
|
def initialize(schemas: {})
|
|
@@ -32,69 +28,11 @@ module Crawlscope
|
|
|
32
28
|
end
|
|
33
29
|
|
|
34
30
|
def validate(item)
|
|
35
|
-
|
|
36
|
-
return item.flat_map { |entry| validate(entry) }
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
errors = []
|
|
40
|
-
|
|
41
|
-
if item.is_a?(Hash) && item["@graph"].is_a?(Array)
|
|
42
|
-
item["@graph"].each do |graph_item|
|
|
43
|
-
errors.concat(validate(graph_item))
|
|
44
|
-
end
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
type = item.is_a?(Hash) ? item["@type"] : nil
|
|
48
|
-
return errors if type.nil?
|
|
49
|
-
|
|
50
|
-
if type.is_a?(Array)
|
|
51
|
-
return errors + type.flat_map { |entry| validate(item.except("@graph").merge("@type" => entry)) }
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
schema = @schemas[type.to_s]
|
|
55
|
-
return errors if schema.nil?
|
|
56
|
-
|
|
57
|
-
JSON::Validator.fully_validate(schema, item, errors_as_objects: true).each do |error|
|
|
58
|
-
details = {
|
|
59
|
-
field: error[:fragment].to_s.sub("#/", ""),
|
|
60
|
-
issue: error[:message],
|
|
61
|
-
type: type
|
|
62
|
-
}
|
|
63
|
-
if %w[MaxLength MinLength].include?(error[:failed_attribute])
|
|
64
|
-
value = value_at_fragment(item, error[:fragment])
|
|
65
|
-
details[:length] = value.length if value.is_a?(String)
|
|
66
|
-
end
|
|
67
|
-
errors << details
|
|
68
|
-
end
|
|
69
|
-
|
|
70
|
-
errors
|
|
71
|
-
rescue JSON::Schema::ValidationError => error
|
|
72
|
-
[{field: "unknown", issue: error.message, type: type}]
|
|
31
|
+
StructuredData::SchemaValidation.new(@schemas).call(item)
|
|
73
32
|
end
|
|
74
33
|
|
|
75
|
-
# Warnings are separate from schema errors: unsupported types and missing
|
|
76
|
-
# recommendations do not make the document invalid.
|
|
77
34
|
def warnings(item)
|
|
78
|
-
|
|
79
|
-
return [] unless item.is_a?(Hash)
|
|
80
|
-
|
|
81
|
-
findings = warnings(item["@graph"])
|
|
82
|
-
Array(item["@type"]).each do |type|
|
|
83
|
-
schema = @schemas[type.to_s]
|
|
84
|
-
unless schema
|
|
85
|
-
findings << {code: :structured_data_unsupported_type, field: "@type", type: type, issue: "has no registered validation schema"}
|
|
86
|
-
next
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
findings.concat(recommendation_warnings(item, schema, type))
|
|
90
|
-
if type == "Dataset" && item.key?("distribution") && registered?("DataDownload")
|
|
91
|
-
downloads = item["distribution"].is_a?(Array) ? item["distribution"] : [item["distribution"]]
|
|
92
|
-
downloads.grep(Hash).each do |download|
|
|
93
|
-
findings.concat(recommendation_warnings(download, fetch("DataDownload"), "DataDownload"))
|
|
94
|
-
end
|
|
95
|
-
end
|
|
96
|
-
end
|
|
97
|
-
findings
|
|
35
|
+
StructuredData::SchemaRecommendations.new(@schemas).call(item)
|
|
98
36
|
end
|
|
99
37
|
|
|
100
38
|
def to_h
|
|
@@ -103,33 +41,10 @@ module Crawlscope
|
|
|
103
41
|
|
|
104
42
|
private
|
|
105
43
|
|
|
106
|
-
def recommendation_warnings(item, schema, type)
|
|
107
|
-
Array(schema[:recommended] || schema["recommended"]).filter_map do |property|
|
|
108
|
-
value = item[property.to_s]
|
|
109
|
-
missing = value.nil? || (value.respond_to?(:empty?) && value.empty?) || (value.is_a?(String) && value.strip.empty?)
|
|
110
|
-
next unless missing
|
|
111
|
-
|
|
112
|
-
{code: :structured_data_missing_recommended_property, field: property.to_s, type: type, issue: "is recommended"}
|
|
113
|
-
end
|
|
114
|
-
end
|
|
115
|
-
|
|
116
|
-
def value_at_fragment(item, fragment)
|
|
117
|
-
fragment.to_s.delete_prefix("#/").split("/").reduce(item) do |value, key|
|
|
118
|
-
case value
|
|
119
|
-
when Hash
|
|
120
|
-
value[key]
|
|
121
|
-
when Array
|
|
122
|
-
value[key.to_i] if key.match?(/\A\d+\z/)
|
|
123
|
-
end
|
|
124
|
-
end
|
|
125
|
-
end
|
|
126
|
-
|
|
127
44
|
def deep_copy(value)
|
|
128
45
|
case value
|
|
129
46
|
when Hash
|
|
130
|
-
value.
|
|
131
|
-
copy[key] = deep_copy(entry)
|
|
132
|
-
end
|
|
47
|
+
value.transform_values { |entry| deep_copy(entry) }
|
|
133
48
|
when Array
|
|
134
49
|
value.map { |entry| deep_copy(entry) }
|
|
135
50
|
else
|
data/lib/crawlscope/schemas.rb
CHANGED
|
@@ -47,11 +47,11 @@ module Crawlscope
|
|
|
47
47
|
type: "object",
|
|
48
48
|
required: ["@type", "headline"],
|
|
49
49
|
properties: {
|
|
50
|
-
"@type" => {enum: ["Article", "NewsArticle", "BlogPosting"]},
|
|
50
|
+
"@type" => {enum: ["Article", "NewsArticle", "BlogPosting", "TechArticle"]},
|
|
51
51
|
:headline => {type: "string", maxLength: 110},
|
|
52
52
|
:image => {type: "string", format: "uri"},
|
|
53
|
-
:datePublished =>
|
|
54
|
-
:dateModified =>
|
|
53
|
+
:datePublished => DATE_OR_DATE_TIME,
|
|
54
|
+
:dateModified => DATE_OR_DATE_TIME,
|
|
55
55
|
:author => {type: "object"},
|
|
56
56
|
:publisher => {type: "object"}
|
|
57
57
|
}
|
|
@@ -468,12 +468,71 @@ module Crawlscope
|
|
|
468
468
|
}
|
|
469
469
|
}.freeze
|
|
470
470
|
|
|
471
|
+
POSTAL_ADDRESS = {
|
|
472
|
+
type: "object",
|
|
473
|
+
required: ["@type"],
|
|
474
|
+
properties: {
|
|
475
|
+
"@type" => {const: "PostalAddress"},
|
|
476
|
+
:streetAddress => {type: "string"},
|
|
477
|
+
:addressLocality => {type: "string"},
|
|
478
|
+
:addressRegion => {type: "string"},
|
|
479
|
+
:postalCode => {type: "string"},
|
|
480
|
+
:addressCountry => {type: ["string", "object"]}
|
|
481
|
+
}
|
|
482
|
+
}.freeze
|
|
483
|
+
|
|
484
|
+
GEO_COORDINATES = {
|
|
485
|
+
type: "object",
|
|
486
|
+
required: ["@type", "latitude", "longitude"],
|
|
487
|
+
properties: {
|
|
488
|
+
"@type" => {const: "GeoCoordinates"},
|
|
489
|
+
:latitude => {type: ["number", "string"]},
|
|
490
|
+
:longitude => {type: ["number", "string"]}
|
|
491
|
+
}
|
|
492
|
+
}.freeze
|
|
493
|
+
|
|
494
|
+
OPENING_HOURS = {
|
|
495
|
+
type: "object",
|
|
496
|
+
required: ["@type"],
|
|
497
|
+
properties: {
|
|
498
|
+
"@type" => {const: "OpeningHoursSpecification"},
|
|
499
|
+
:dayOfWeek => {type: ["string", "array"]},
|
|
500
|
+
:opens => {type: "string"},
|
|
501
|
+
:closes => {type: "string"},
|
|
502
|
+
:validFrom => DATE_OR_DATE_TIME,
|
|
503
|
+
:validThrough => DATE_OR_DATE_TIME
|
|
504
|
+
}
|
|
505
|
+
}.freeze
|
|
506
|
+
|
|
507
|
+
PLACE_TYPES = %w[
|
|
508
|
+
LocalBusiness Hotel VacationRental Restaurant BarOrPub CafeOrCoffeeShop
|
|
509
|
+
TouristAttraction MedicalBusiness MedicalClinic Dentist VeterinaryCare
|
|
510
|
+
Pharmacy DaySpa BeautySalon HairSalon Store GroceryStore AutoRental
|
|
511
|
+
Park Museum Beach LandmarksOrHistoricalBuildings
|
|
512
|
+
].freeze
|
|
513
|
+
|
|
514
|
+
PLACE = {
|
|
515
|
+
type: "object",
|
|
516
|
+
required: ["@type", "name"],
|
|
517
|
+
properties: {
|
|
518
|
+
"@type" => {enum: PLACE_TYPES},
|
|
519
|
+
:name => {type: "string", minLength: 1},
|
|
520
|
+
:url => {type: "string", format: "uri"},
|
|
521
|
+
:telephone => {type: "string"},
|
|
522
|
+
:address => {anyOf: [{type: "string"}, POSTAL_ADDRESS]},
|
|
523
|
+
:geo => GEO_COORDINATES,
|
|
524
|
+
:openingHoursSpecification => {anyOf: [OPENING_HOURS, {type: "array", items: OPENING_HOURS}]},
|
|
525
|
+
:specialOpeningHoursSpecification => {anyOf: [OPENING_HOURS, {type: "array", items: OPENING_HOURS}]}
|
|
526
|
+
}
|
|
527
|
+
}.freeze
|
|
528
|
+
|
|
471
529
|
def self.schemas
|
|
472
530
|
{
|
|
473
531
|
"FAQPage" => FAQ_PAGE,
|
|
474
532
|
"Article" => ARTICLE,
|
|
475
533
|
"NewsArticle" => ARTICLE,
|
|
476
534
|
"BlogPosting" => ARTICLE,
|
|
535
|
+
"TechArticle" => ARTICLE,
|
|
477
536
|
"Organization" => ORGANIZATION,
|
|
478
537
|
"SoftwareApplication" => SOFTWARE_APPLICATION,
|
|
479
538
|
"WebApplication" => WEB_APPLICATION,
|
|
@@ -493,8 +552,11 @@ module Crawlscope
|
|
|
493
552
|
"JobPosting" => JOB_POSTING,
|
|
494
553
|
"Dataset" => DATASET,
|
|
495
554
|
"DataCatalog" => DATA_CATALOG,
|
|
496
|
-
"DataDownload" => DATA_DOWNLOAD
|
|
497
|
-
|
|
555
|
+
"DataDownload" => DATA_DOWNLOAD,
|
|
556
|
+
"PostalAddress" => POSTAL_ADDRESS,
|
|
557
|
+
"GeoCoordinates" => GEO_COORDINATES,
|
|
558
|
+
"OpeningHoursSpecification" => OPENING_HOURS
|
|
559
|
+
}.merge(PLACE_TYPES.to_h { |type| [type, PLACE] })
|
|
498
560
|
end
|
|
499
561
|
end
|
|
500
562
|
end
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Crawlscope
|
|
4
|
+
module StructuredData
|
|
5
|
+
class Builder
|
|
6
|
+
CONTEXT = Properties::CONTEXT
|
|
7
|
+
|
|
8
|
+
def initialize(base_url: nil, organization: {}, language: nil)
|
|
9
|
+
@base_url = base_url&.dup
|
|
10
|
+
@organization = Properties.normalize(organization)
|
|
11
|
+
@language = language&.dup
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def organization(name: @organization["name"], url: @organization["url"] || @base_url, **properties)
|
|
15
|
+
attributes = @organization.merge(properties.transform_keys { |key| Properties.name(key) })
|
|
16
|
+
attributes["logo"] = {"@type" => "ImageObject", "url" => attributes["logo"]} if attributes["logo"].is_a?(String)
|
|
17
|
+
node("Organization", **attributes.merge("name" => name, "url" => url))
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def person(name:, **properties)
|
|
21
|
+
node("Person", name: name, **properties)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def website(name:, url: @base_url, **properties)
|
|
25
|
+
node("WebSite", name: name, url: url, in_language: @language, **properties)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def webpage(name:, url:, type: "WebPage", **properties)
|
|
29
|
+
node(type, name: name, url: url, in_language: @language, **properties)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def collection_page(name:, url:, **properties)
|
|
33
|
+
webpage(name: name, url: url, type: "CollectionPage", **properties)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def item_list(items, **properties)
|
|
37
|
+
node("ItemList", item_list_element: positioned(items, "ListItem"), **properties)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def breadcrumb(items)
|
|
41
|
+
node("BreadcrumbList", item_list_element: positioned(items, "ListItem"))
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def article(headline:, type: "Article", publisher: default_publisher, **properties)
|
|
45
|
+
node(type, headline: headline, publisher: publisher, in_language: @language, **properties)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def faq(items)
|
|
49
|
+
questions = items.map do |item|
|
|
50
|
+
question, answer = item.transform_keys(&:to_s).values_at("question", "answer")
|
|
51
|
+
{"@type" => "Question", "name" => question, "acceptedAnswer" => {"@type" => "Answer", "text" => answer}}
|
|
52
|
+
end
|
|
53
|
+
node("FAQPage", main_entity: questions)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def how_to(name:, steps:, **properties)
|
|
57
|
+
node("HowTo", name: name, step: positioned(steps, "HowToStep"), **properties)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def software_application(name:, publisher: default_publisher, **properties)
|
|
61
|
+
node("SoftwareApplication", name: name, publisher: publisher, **properties)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def web_application(name:, publisher: default_publisher, **properties)
|
|
65
|
+
node("WebApplication", name: name, publisher: publisher, **properties)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def product(name:, **properties)
|
|
69
|
+
node("Product", name: name, **properties)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def offer(**properties)
|
|
73
|
+
node("Offer", **properties)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def local_business(name:, type: "LocalBusiness", **properties)
|
|
77
|
+
node(type, name: name, **properties)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def postal_address(**properties)
|
|
81
|
+
node("PostalAddress", **properties)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def geo_coordinates(latitude:, longitude:)
|
|
85
|
+
node("GeoCoordinates", latitude: latitude, longitude: longitude)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def opening_hours(**properties)
|
|
89
|
+
node("OpeningHoursSpecification", **properties)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def dataset(name:, description:, **properties)
|
|
93
|
+
node("Dataset", name: name, description: description, in_language: @language, **properties)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def data_catalog(name:, **properties)
|
|
97
|
+
node("DataCatalog", name: name, **properties)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def data_download(content_url:, **properties)
|
|
101
|
+
node("DataDownload", content_url: content_url, **properties)
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def node(type, **properties)
|
|
105
|
+
{"@context" => CONTEXT}.merge(nested(type, **properties))
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def graph(*nodes)
|
|
109
|
+
{"@context" => CONTEXT, "@graph" => nodes.map { |entry| entry.reject { |key, value| key == "@context" && value == CONTEXT } }}
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
private
|
|
113
|
+
|
|
114
|
+
def default_publisher
|
|
115
|
+
organization.except("@context") if @organization["name"]
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def positioned(items, type)
|
|
119
|
+
items.map.with_index(1) { |item, position| item.transform_keys { |key| Properties.name(key) }.merge("position" => position, "@type" => type) }
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def nested(type, **properties)
|
|
123
|
+
Properties.normalize(properties).except("@context").merge("@type" => type)
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "date"
|
|
4
|
+
require "time"
|
|
5
|
+
|
|
6
|
+
module Crawlscope
|
|
7
|
+
module StructuredData
|
|
8
|
+
module Properties
|
|
9
|
+
CONTEXT = "https://schema.org"
|
|
10
|
+
|
|
11
|
+
def self.normalize(value)
|
|
12
|
+
case value
|
|
13
|
+
when Hash then normalize_hash(value)
|
|
14
|
+
when Array then value.map { |entry| normalize(entry) }
|
|
15
|
+
when Date, Time then value.iso8601
|
|
16
|
+
when String then value.dup
|
|
17
|
+
else value
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def self.name(key)
|
|
22
|
+
return key if key.is_a?(String)
|
|
23
|
+
return "@id" if key == :id
|
|
24
|
+
|
|
25
|
+
text = key.to_s
|
|
26
|
+
return text unless text.include?("_")
|
|
27
|
+
|
|
28
|
+
first, *rest = text.split("_")
|
|
29
|
+
first + rest.map(&:capitalize).join
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def self.normalize_hash(value)
|
|
33
|
+
value.each_with_object({}) do |(key, entry), result|
|
|
34
|
+
next if entry.nil? || (key == "@context" && entry == CONTEXT)
|
|
35
|
+
|
|
36
|
+
result[name(key)] = normalize(entry)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
private_class_method :normalize_hash
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Crawlscope
|
|
4
|
+
module StructuredData
|
|
5
|
+
class SchemaRecommendations
|
|
6
|
+
def initialize(schemas)
|
|
7
|
+
@schemas = schemas
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def call(item)
|
|
11
|
+
case item
|
|
12
|
+
when Array then item.flat_map { |entry| call(entry) }
|
|
13
|
+
when Hash then call(item["@graph"]) + Array(item["@type"]).flat_map { |type| warnings_for_type(item, type) }
|
|
14
|
+
else []
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
private
|
|
19
|
+
|
|
20
|
+
def warnings_for_type(item, type)
|
|
21
|
+
schema = @schemas[type.to_s]
|
|
22
|
+
return [{code: :structured_data_unsupported_type, field: "@type", type: type, issue: "has no registered validation schema"}] unless schema
|
|
23
|
+
|
|
24
|
+
warnings = recommendations(item, schema, type)
|
|
25
|
+
warnings.concat(download_warnings(item["distribution"])) if type == "Dataset"
|
|
26
|
+
warnings
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def download_warnings(distribution)
|
|
30
|
+
schema = @schemas["DataDownload"]
|
|
31
|
+
return [] unless schema
|
|
32
|
+
|
|
33
|
+
downloads = distribution.is_a?(Array) ? distribution : [distribution]
|
|
34
|
+
downloads.grep(Hash).flat_map { |download| recommendations(download, schema, "DataDownload") }
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def recommendations(item, schema, type)
|
|
38
|
+
Array(schema[:recommended] || schema["recommended"]).filter_map do |property|
|
|
39
|
+
field = property.to_s
|
|
40
|
+
next unless missing?(item[field])
|
|
41
|
+
|
|
42
|
+
{code: :structured_data_missing_recommended_property, field: field, type: type, issue: "is recommended"}
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def missing?(value)
|
|
47
|
+
return true if value.nil?
|
|
48
|
+
return value.strip.empty? if value.is_a?(String)
|
|
49
|
+
|
|
50
|
+
value.respond_to?(:empty?) && value.empty?
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json-schema"
|
|
4
|
+
|
|
5
|
+
JSON::Validator.use_multi_json = false
|
|
6
|
+
|
|
7
|
+
module Crawlscope
|
|
8
|
+
module StructuredData
|
|
9
|
+
class SchemaValidation
|
|
10
|
+
def initialize(schemas)
|
|
11
|
+
@schemas = schemas
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def call(item)
|
|
15
|
+
case item
|
|
16
|
+
when Array then item.flat_map { |entry| call(entry) }
|
|
17
|
+
when Hash then graph_errors(item) + typed_errors(item)
|
|
18
|
+
else []
|
|
19
|
+
end
|
|
20
|
+
rescue JSON::Schema::ValidationError => error
|
|
21
|
+
type = item["@type"] if item.is_a?(Hash)
|
|
22
|
+
[{field: "unknown", issue: error.message, type: type}]
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
def graph_errors(item)
|
|
28
|
+
graph = item["@graph"]
|
|
29
|
+
return [] unless graph.is_a?(Array)
|
|
30
|
+
|
|
31
|
+
graph.flat_map do |node|
|
|
32
|
+
call(inherit_context(node, item))
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def inherit_context(node, parent)
|
|
37
|
+
return node unless node.is_a?(Hash) && parent.key?("@context") && !node.key?("@context")
|
|
38
|
+
|
|
39
|
+
node.merge("@context" => parent["@context"])
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def typed_errors(item)
|
|
43
|
+
type = item["@type"]
|
|
44
|
+
case type
|
|
45
|
+
when nil then []
|
|
46
|
+
when Array
|
|
47
|
+
type.flat_map { |entry| call(item.except("@graph").merge("@type" => entry)) }
|
|
48
|
+
else
|
|
49
|
+
schema_errors(item, type)
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def schema_errors(item, type)
|
|
54
|
+
schema = @schemas[type.to_s]
|
|
55
|
+
return [] unless schema
|
|
56
|
+
|
|
57
|
+
JSON::Validator.fully_validate(schema, item, errors_as_objects: true).map do |error|
|
|
58
|
+
error_details(error, item, type)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def error_details(error, item, type)
|
|
63
|
+
fragment = error[:fragment]
|
|
64
|
+
details = {field: fragment.to_s.sub("#/", ""), issue: error[:message], type: type}
|
|
65
|
+
if %w[MaxLength MinLength].include?(error[:failed_attribute])
|
|
66
|
+
value = value_at_fragment(item, fragment)
|
|
67
|
+
details[:length] = value.length if value.is_a?(String)
|
|
68
|
+
end
|
|
69
|
+
details
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def value_at_fragment(item, fragment)
|
|
73
|
+
fragment.to_s.delete_prefix("#/").split("/").reduce(item) do |value, key|
|
|
74
|
+
case value
|
|
75
|
+
when Hash
|
|
76
|
+
value[key]
|
|
77
|
+
when Array
|
|
78
|
+
value[key.to_i] if key.match?(/\A\d+\z/)
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module Crawlscope
|
|
6
|
+
module StructuredData
|
|
7
|
+
module Serialization
|
|
8
|
+
ESCAPES = {"<" => "\\u003c", ">" => "\\u003e", "&" => "\\u0026", "\u2028" => "\\u2028", "\u2029" => "\\u2029"}.freeze
|
|
9
|
+
|
|
10
|
+
def self.json(data)
|
|
11
|
+
JSON.generate(data).gsub(/[<>&\u2028\u2029]/, ESCAPES)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -56,13 +56,13 @@ module Crawlscope
|
|
|
56
56
|
end
|
|
57
57
|
|
|
58
58
|
def missing_type_paths(data, path = "$")
|
|
59
|
-
return [] unless data.is_a?(Hash)
|
|
59
|
+
return [path] unless data.is_a?(Hash)
|
|
60
60
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
61
|
+
graph = data["@graph"]
|
|
62
|
+
graph = [] unless graph.is_a?(Array)
|
|
63
|
+
paths = (data["@type"].to_s.strip.empty? && graph.empty?) ? [path] : []
|
|
64
|
+
graph.each_with_index do |entry, index|
|
|
65
|
+
paths.concat(missing_type_paths(entry, "#{path}.@graph[#{index}]"))
|
|
66
66
|
end
|
|
67
67
|
paths
|
|
68
68
|
end
|
data/lib/crawlscope/version.rb
CHANGED
|
@@ -197,6 +197,39 @@ class CrawlscopeSchemaRegistryTest < Minitest::Test
|
|
|
197
197
|
refute_empty registry.validate({"@type" => ["WebPage", "Article"]})
|
|
198
198
|
end
|
|
199
199
|
|
|
200
|
+
def test_graph_checks_preserve_context_overrides_and_finding_order
|
|
201
|
+
registry = Crawlscope::SchemaRegistry.default
|
|
202
|
+
faq = {"@type" => "FAQPage", "mainEntity" => [
|
|
203
|
+
{"@type" => "Question", "name" => "Question", "acceptedAnswer" => {"@type" => "Answer", "text" => "Answer"}}
|
|
204
|
+
]}
|
|
205
|
+
document = {"@context" => "https://schema.org", "@type" => ["Article", "Organization"], "@graph" => [
|
|
206
|
+
faq, faq.merge("@context" => "https://example.com"), {"@type" => "Article"}
|
|
207
|
+
]}
|
|
208
|
+
|
|
209
|
+
errors = registry.validate(document)
|
|
210
|
+
|
|
211
|
+
assert_equal %w[FAQPage Article Article Organization], errors.map { |error| error.fetch(:type) }
|
|
212
|
+
refute faq.key?("@context")
|
|
213
|
+
assert_empty registry.warnings(document)
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
def test_registered_recommendations_and_downloads_follow_current_registry
|
|
217
|
+
registry = Crawlscope::SchemaRegistry.new(schemas: {
|
|
218
|
+
"Dataset" => {type: "object", recommended: %w[enabled count label]},
|
|
219
|
+
"DataDownload" => {"type" => "object", "recommended" => ["encodingFormat"]}
|
|
220
|
+
})
|
|
221
|
+
dataset = {"@type" => "Dataset", "enabled" => false, "count" => 0, "label" => " ",
|
|
222
|
+
"distribution" => [{"contentUrl" => "https://example.com/data.csv"}, {"encodingFormat" => "text/csv"}]}
|
|
223
|
+
assert_equal [["Dataset", "label"], ["DataDownload", "encodingFormat"]],
|
|
224
|
+
registry.warnings(dataset).map { |warning| warning.values_at(:type, :field) }
|
|
225
|
+
copy = registry.dup
|
|
226
|
+
copy.fetch("Dataset").fetch(:recommended) << "license"
|
|
227
|
+
assert_equal %w[label license encodingFormat], copy.warnings(dataset).map { |warning| warning.fetch(:field) }
|
|
228
|
+
assert_equal %w[label encodingFormat], registry.warnings(dataset).map { |warning| warning.fetch(:field) }
|
|
229
|
+
registry.register("DataDownload", {type: "object"})
|
|
230
|
+
assert_equal ["label"], registry.warnings(dataset).map { |warning| warning.fetch(:field) }
|
|
231
|
+
end
|
|
232
|
+
|
|
200
233
|
def test_validate_accepts_arrays_graphs_unknown_types_and_non_hashes
|
|
201
234
|
registry = Crawlscope::SchemaRegistry.default
|
|
202
235
|
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "test_helper"
|
|
4
|
+
|
|
5
|
+
class CrawlscopeStructuredDataBuilderTest < Minitest::Test
|
|
6
|
+
Builder = Crawlscope::StructuredData::Builder
|
|
7
|
+
Serialization = Crawlscope::StructuredData::Serialization
|
|
8
|
+
|
|
9
|
+
def test_configured_identity_defaults_are_local_and_overridable
|
|
10
|
+
builder = Builder.new(base_url: "https://example.com", organization: {name: "Example", logo: "https://example.com/logo.png"}, language: "en")
|
|
11
|
+
article = builder.article(headline: "Guide", date_published: Date.new(2026, 1, 2), description: nil)
|
|
12
|
+
|
|
13
|
+
assert_equal({
|
|
14
|
+
"@context" => "https://schema.org",
|
|
15
|
+
"@type" => "Article",
|
|
16
|
+
"headline" => "Guide",
|
|
17
|
+
"datePublished" => "2026-01-02",
|
|
18
|
+
"inLanguage" => "en",
|
|
19
|
+
"publisher" => {"@type" => "Organization", "name" => "Example", "url" => "https://example.com",
|
|
20
|
+
"logo" => {"@type" => "ImageObject", "url" => "https://example.com/logo.png"}}
|
|
21
|
+
}, article)
|
|
22
|
+
assert_empty Crawlscope::SchemaRegistry.default.validate(article)
|
|
23
|
+
assert_equal "fr", builder.article(headline: "Guide", in_language: "fr")["inLanguage"]
|
|
24
|
+
refute builder.article(headline: "Guide", publisher: nil).key?("publisher")
|
|
25
|
+
refute Builder.new.article(headline: "Guide").key?("publisher")
|
|
26
|
+
refute builder.organization(logo: nil).key?("logo")
|
|
27
|
+
assert_equal "https://example.com/logo.png", builder.organization.fetch("logo").fetch("url")
|
|
28
|
+
article.fetch("publisher").fetch("name").replace("Other")
|
|
29
|
+
assert_equal "Example", builder.organization.fetch("name")
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def test_graph_uses_one_context_and_independently_validates
|
|
33
|
+
builder = Builder.new
|
|
34
|
+
breadcrumb = builder.breadcrumb([{name: "Home", item: "https://example.com"}, {name: "Guide", item: "https://example.com/guide"}])
|
|
35
|
+
faq = builder.faq([{"question" => "Why?", "answer" => "Because."}])
|
|
36
|
+
graph = builder.graph(breadcrumb, faq)
|
|
37
|
+
|
|
38
|
+
assert_equal [1, 2], graph.fetch("@graph").first.fetch("itemListElement").map { |item| item.fetch("position") }
|
|
39
|
+
assert_equal({"@type" => "Question", "name" => "Why?", "acceptedAnswer" => {"@type" => "Answer", "text" => "Because."}}, faq.fetch("mainEntity").first)
|
|
40
|
+
assert graph.fetch("@graph").none? { |entry| entry.key?("@context") }
|
|
41
|
+
assert_equal "https://schema.org", faq.fetch("@context")
|
|
42
|
+
assert_empty validator.call(html: script(graph), required_types: %w[BreadcrumbList FAQPage]).findings
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def test_generates_commercial_and_navigation_nodes_without_inventing_facts
|
|
46
|
+
builder = Builder.new
|
|
47
|
+
offer = builder.offer(price: 0, price_currency: "USD", availability: "https://schema.org/InStock")
|
|
48
|
+
software = builder.software_application(name: "Example", offers: offer)
|
|
49
|
+
|
|
50
|
+
assert_equal 0, software.dig("offers", "price")
|
|
51
|
+
refute software.key?("aggregateRating")
|
|
52
|
+
assert_empty Crawlscope::SchemaRegistry.default.validate(software)
|
|
53
|
+
assert_equal false, builder.dataset(name: "Research", description: "Public data", is_accessible_for_free: false)["isAccessibleForFree"]
|
|
54
|
+
refute builder.dataset(name: "Research", description: "Public data").key?("license")
|
|
55
|
+
assert_equal "WebApplication", builder.web_application(name: "Tool")["@type"]
|
|
56
|
+
assert_equal "Product", builder.product(name: "Product")["@type"]
|
|
57
|
+
assert_equal "Person", builder.person(name: "Author")["@type"]
|
|
58
|
+
assert_equal "WebSite", builder.website(name: "Site")["@type"]
|
|
59
|
+
assert_equal "CollectionPage", builder.collection_page(name: "Guides", url: "https://example.com/guides")["@type"]
|
|
60
|
+
assert_equal 1, builder.item_list([{name: "Guide"}]).dig("itemListElement", 0, "position")
|
|
61
|
+
assert_equal 1, builder.how_to(name: "Guide", steps: [{name: "Step", text: "Do it"}]).dig("step", 0, "position")
|
|
62
|
+
assert_equal "DataCatalog", builder.data_catalog(name: "Research")["@type"]
|
|
63
|
+
assert_equal "https://example.com/data.csv", builder.data_download(content_url: "https://example.com/data.csv")["contentUrl"]
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def test_place_constructor_respects_host_type_and_facts
|
|
67
|
+
builder = Builder.new
|
|
68
|
+
place = builder.local_business(type: "Restaurant", name: "Cafe",
|
|
69
|
+
address: builder.postal_address(street_address: "Main Street", address_region: "CA"),
|
|
70
|
+
geo: builder.geo_coordinates(latitude: 0, longitude: 1.5),
|
|
71
|
+
opening_hours_specification: [builder.opening_hours(day_of_week: "Monday", opens: "09:00", closes: "17:00")])
|
|
72
|
+
|
|
73
|
+
assert_equal "Restaurant", place["@type"]
|
|
74
|
+
assert_equal "CA", place.dig("address", "addressRegion")
|
|
75
|
+
refute place.fetch("address").key?("@context")
|
|
76
|
+
assert_equal 0, place.dig("geo", "latitude")
|
|
77
|
+
assert_equal "09:00", place.dig("openingHoursSpecification", 0, "opens")
|
|
78
|
+
assert_empty validator.call(html: script(place), required_types: ["Restaurant"]).findings
|
|
79
|
+
place.fetch("geo").delete("latitude")
|
|
80
|
+
assert_includes validator.call(html: script(place)).findings.map(&:code), :structured_data_schema_error
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def test_validator_rejects_invalid_nodes_even_inside_untyped_graph
|
|
84
|
+
graph = {"@context" => "https://schema.org", "@graph" => [
|
|
85
|
+
{"@type" => "FAQPage", "mainEntity" => []},
|
|
86
|
+
{"name" => "Untyped"}
|
|
87
|
+
]}
|
|
88
|
+
result = validator.call(html: script(graph))
|
|
89
|
+
assert_equal [:structured_data_missing_type, :structured_data_schema_error], result.findings.map(&:code)
|
|
90
|
+
assert_equal ["$.@graph[1]"], result.findings.first.paths
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def test_empty_or_non_object_graph_cannot_pass_type_validation
|
|
94
|
+
[[], [1]].each do |nodes|
|
|
95
|
+
result = validator.call(html: script({"@context" => "https://schema.org", "@graph" => nodes}))
|
|
96
|
+
assert_includes result.findings.map(&:code), :structured_data_missing_type
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def test_technical_articles_are_validated
|
|
101
|
+
registry = Crawlscope::SchemaRegistry.default
|
|
102
|
+
article = Builder.new.article(type: "TechArticle", headline: "Help", date_modified: Date.new(2026, 1, 1))
|
|
103
|
+
assert_empty registry.validate(article)
|
|
104
|
+
assert_empty registry.warnings(article)
|
|
105
|
+
refute_empty registry.validate(article.except("headline"))
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def test_serialization_preserves_data_without_terminating_script
|
|
109
|
+
data = Builder.new.person(name: "</script><p>&\u2028\u2029</p>")
|
|
110
|
+
payload = Serialization.json(data)
|
|
111
|
+
|
|
112
|
+
refute_match(/[<>&\u2028\u2029]/, payload)
|
|
113
|
+
assert_equal data, JSON.parse(payload)
|
|
114
|
+
assert_equal data, Crawlscope::StructuredData::Document.new(html: script(data)).json_ld_items.first
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def test_generic_node_accepts_explicit_schema_keys_and_does_not_mutate_input
|
|
118
|
+
fields = {:id => "https://example.com/#id", "sameAs" => ["https://example.org"], :date_modified => Time.utc(2026, 2, 1)}
|
|
119
|
+
data = Builder.new.node("CustomType", **fields)
|
|
120
|
+
assert_equal "https://example.com/#id", data["@id"]
|
|
121
|
+
assert_equal ["https://example.org"], data["sameAs"]
|
|
122
|
+
assert_equal "2026-02-01T00:00:00Z", data["dateModified"]
|
|
123
|
+
assert_instance_of Time, fields.fetch(:date_modified)
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
private
|
|
127
|
+
|
|
128
|
+
def script(data)
|
|
129
|
+
%(<script type="application/ld+json">#{Serialization.json(data)}</script>)
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def validator
|
|
133
|
+
Crawlscope::StructuredData::Validator.new(schema_registry: Crawlscope::SchemaRegistry.default)
|
|
134
|
+
end
|
|
135
|
+
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: crawlscope
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.13.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Paulo Fidalgo
|
|
@@ -259,13 +259,18 @@ files:
|
|
|
259
259
|
- lib/crawlscope/server_timing/summary.rb
|
|
260
260
|
- lib/crawlscope/sitemap.rb
|
|
261
261
|
- lib/crawlscope/structured_data/audit.rb
|
|
262
|
+
- lib/crawlscope/structured_data/builder.rb
|
|
262
263
|
- lib/crawlscope/structured_data/check.rb
|
|
263
264
|
- lib/crawlscope/structured_data/document.rb
|
|
264
265
|
- lib/crawlscope/structured_data/entry.rb
|
|
265
266
|
- lib/crawlscope/structured_data/error_formatter.rb
|
|
266
267
|
- lib/crawlscope/structured_data/outcome.rb
|
|
268
|
+
- lib/crawlscope/structured_data/properties.rb
|
|
267
269
|
- lib/crawlscope/structured_data/report.rb
|
|
268
270
|
- lib/crawlscope/structured_data/reporter.rb
|
|
271
|
+
- lib/crawlscope/structured_data/schema_recommendations.rb
|
|
272
|
+
- lib/crawlscope/structured_data/schema_validation.rb
|
|
273
|
+
- lib/crawlscope/structured_data/serialization.rb
|
|
269
274
|
- lib/crawlscope/structured_data/validator.rb
|
|
270
275
|
- lib/crawlscope/structured_data/writer.rb
|
|
271
276
|
- lib/crawlscope/tasks.rb
|
|
@@ -298,6 +303,7 @@ files:
|
|
|
298
303
|
- test/crawlscope/server_timing_test.rb
|
|
299
304
|
- test/crawlscope/sitemap_test.rb
|
|
300
305
|
- test/crawlscope/structured_data_audit_test.rb
|
|
306
|
+
- test/crawlscope/structured_data_builder_test.rb
|
|
301
307
|
- test/crawlscope/structured_data_document_test.rb
|
|
302
308
|
- test/crawlscope/structured_data_error_formatter_test.rb
|
|
303
309
|
- test/crawlscope/structured_data_report_test.rb
|