Skip to content

Universal SEO skill for Claude Code. 26 sub-skills + 19 sub-agents covering technical SEO, E-E-A-T, schema, GEO/AEO, agent readiness (Lighthouse Agentic Browsing, WebMCP, llms.txt), backlinks, local SEO, e-commerce, international SEO, Google APIs, and PDF/Excel reporting. 9 optional extensions for live SEO data.

fix(hooks): hand schema warnings to Claude as additionalContext

Agrici Daniele9d089e20675
diff --git a/hooks/validate-schema.py b/hooks/validate-schema.py
index 7471b2e..9b3bb0f 100755
--- a/hooks/validate-schema.py
+++ b/hooks/validate-schema.py
@@ -1,8 +1,15 @@
#!/usr/bin/env python3
"""Post-edit schema validation hook for Claude Code.
-Validates JSON-LD schema after file edits. Returns exit code 2 to block
-if critical validation errors found.
+Validates JSON-LD schema after file edits. The file is already written when
+a PostToolUse hook runs, so nothing here can undo the edit; the hook makes
+sure Claude reads what is wrong:
+
+- critical errors (placeholders, deprecated or retired types): exit 2, with
+ the errors on stderr, which Claude Code feeds back to Claude;
+- warnings only: exit 0, with the warnings as hookSpecificOutput
+ additionalContext JSON on stdout, which Claude reads without an error
+ notice (exit 1 would show them to the user only).
Hook configuration in ~/.claude/settings.json:
{
@@ -313,21 +320,21 @@ def main():
critical = [e for e in errors if any(kw in e.lower() for kw in critical_keywords)]
warnings = [e for e in errors if e not in critical]
- # Claude Code reads a hook's stderr, not its stdout: on exit 2 a PostToolUse
- # hook's stderr is fed back to Claude, and on exit 1 it is shown to the user.
- # Anything printed to stdout here would be dropped.
- if warnings:
- print("⚠️ Schema validation warnings:", file=sys.stderr)
- for w in warnings:
- print(f" - {w}", file=sys.stderr)
-
if critical:
- print("🛑 Schema validation ERRORS (blocking):", file=sys.stderr)
+ # Exit 2: Claude Code feeds stderr back to Claude (stdout is ignored).
+ print("🛑 Schema validation ERRORS (flagged for Claude, fix before shipping):", file=sys.stderr)
for e in critical:
print(f" - {e}", file=sys.stderr)
- sys.exit(2) # Block the edit
-
- sys.exit(1) # Warnings only; proceed
+ if warnings:
+ print("⚠️ Also warnings:", file=sys.stderr)
+ for w in warnings:
+ print(f" - {w}", file=sys.stderr)
+ sys.exit(2)
+
+ # Warnings only: exit 0 with JSON, so Claude reads them as context.
+ context = "Schema validation warnings for " + filepath + ":\n" + "\n".join(f"- {w}" for w in warnings)
+ print(json.dumps({"hookSpecificOutput": {"hookEventName": "PostToolUse", "additionalContext": context}}))
+ sys.exit(0)
if __name__ == "__main__":
diff --git a/tests/test_schema_hook_detection.py b/tests/test_schema_hook_detection.py
index d782ceb..899d7f6 100644
--- a/tests/test_schema_hook_detection.py
+++ b/tests/test_schema_hook_detection.py
@@ -13,6 +13,7 @@ Two failure classes are covered:
from __future__ import annotations
+import json
import subprocess
import sys
from pathlib import Path
@@ -37,6 +38,18 @@ def _run(tmp_path: Path, filename: str, head: str) -> subprocess.CompletedProces
)
+def _warnings(result: subprocess.CompletedProcess) -> str:
+ """The warnings a warnings-only run hands Claude: exit 0, JSON additionalContext on stdout."""
+ if result.returncode != 0 or not result.stdout.strip():
+ return ""
+ return json.loads(result.stdout)["hookSpecificOutput"]["additionalContext"]
+
+
+def _clean(result: subprocess.CompletedProcess) -> bool:
+ """Nothing to report: exit 0 and nothing on either stream."""
+ return result.returncode == 0 and result.stdout == "" and result.stderr == ""
+
+
# --- false negatives -------------------------------------------------------
@@ -70,12 +83,12 @@ def test_uppercase_tag_and_attribute_are_validated(tmp_path: Path) -> None:
def test_other_script_types_are_ignored(tmp_path: Path) -> None:
head = f'<script type="module">{RETIRED}</script><script type="text/javascript">var x = {RETIRED};</script>'
- assert _run(tmp_path, "page.html", head).returncode == 0
+ assert _clean(_run(tmp_path, "page.html", head))
def test_valid_block_with_extra_attributes_passes(tmp_path: Path) -> None:
head = f'<script data-testid="ld" type="application/ld+json" nonce="abc">{VALID}</script>'
- assert _run(tmp_path, "page.html", head).returncode == 0
+ assert _clean(_run(tmp_path, "page.html", head))
# --- false positives on templates -------------------------------------------
@@ -84,33 +97,32 @@ def test_valid_block_with_extra_attributes_passes(tmp_path: Path) -> None:
def test_jsx_expression_body_is_not_reported(tmp_path: Path) -> None:
head = '<script type="application/ld+json">{JSON.stringify(schema)}</script>'
result = _run(tmp_path, "page.tsx", head)
- assert result.returncode == 0
- assert result.stderr == ""
+ assert _clean(result)
def test_jsx_identifier_body_is_not_reported(tmp_path: Path) -> None:
result = _run(tmp_path, "Page.jsx", '<script type="application/ld+json">{jsonLd}</script>')
- assert result.returncode == 0
+ assert _clean(result)
def test_vue_mustache_body_is_not_reported(tmp_path: Path) -> None:
result = _run(tmp_path, "Page.vue", '<script type="application/ld+json">{{ jsonLd }}</script>')
- assert result.returncode == 0
+ assert _clean(result)
def test_svelte_html_body_is_not_reported(tmp_path: Path) -> None:
head = '<script type="application/ld+json">{@html JSON.stringify(schema)}</script>'
- assert _run(tmp_path, "Page.svelte", head).returncode == 0
+ assert _clean(_run(tmp_path, "Page.svelte", head))
def test_php_echo_body_is_not_reported(tmp_path: Path) -> None:
head = '<script type="application/ld+json"><?php echo json_encode($schema); ?></script>'
- assert _run(tmp_path, "page.php", head).returncode == 0
+ assert _clean(_run(tmp_path, "page.php", head))
def test_ejs_body_is_not_reported(tmp_path: Path) -> None:
head = '<script type="application/ld+json"><%- JSON.stringify(schema) %></script>'
- assert _run(tmp_path, "page.ejs", head).returncode == 0
+ assert _clean(_run(tmp_path, "page.ejs", head))
def test_literal_json_in_component_file_is_still_validated(tmp_path: Path) -> None:
@@ -122,8 +134,7 @@ def test_malformed_object_literal_in_html_is_still_reported(tmp_path: Path) -> N
result = _run(
tmp_path, "page.html", '<script type="application/ld+json">{name: "unquoted"}</script>'
)
- assert result.returncode == 1
- assert "Invalid JSON" in result.stderr
+ assert "Invalid JSON" in _warnings(result)
# --- @context forms ---------------------------------------------------------
@@ -132,8 +143,7 @@ def test_malformed_object_literal_in_html_is_still_reported(tmp_path: Path) -> N
def test_context_with_trailing_slash_is_accepted(tmp_path: Path) -> None:
head = '<script type="application/ld+json">{"@context":"https://schema.org/","@type":"Organization","name":"X"}</script>'
result = _run(tmp_path, "page.html", head)
- assert result.returncode == 0
- assert result.stderr == ""
+ assert _clean(result)
def test_context_object_with_schema_vocab_is_accepted(tmp_path: Path) -> None:
@@ -142,7 +152,7 @@ def test_context_object_with_schema_vocab_is_accepted(tmp_path: Path) -> None:
'{"@context":{"@vocab":"https://schema.org/"},"@type":"Organization","name":"X"}'
"</script>"
)
- assert _run(tmp_path, "page.html", head).returncode == 0
+ assert _clean(_run(tmp_path, "page.html", head))
def test_context_list_containing_schema_org_is_accepted(tmp_path: Path) -> None:
@@ -151,14 +161,13 @@ def test_context_list_containing_schema_org_is_accepted(tmp_path: Path) -> None:
'{"@context":["https://schema.org",{"ex":"https://example.com/ns#"}],"@type":"Organization","name":"X"}'
"</script>"
)
- assert _run(tmp_path, "page.html", head).returncode == 0
+ assert _clean(_run(tmp_path, "page.html", head))
def test_foreign_context_is_still_reported(tmp_path: Path) -> None:
head = '<script type="application/ld+json">{"@context":"https://example.com/ns","@type":"Organization"}</script>'
result = _run(tmp_path, "page.html", head)
- assert result.returncode == 1
- assert "@context should be" in result.stderr
+ assert "@context should be" in _warnings(result)
# --- tokenizer regressions (a quoted attribute value may contain ">") ------
@@ -189,15 +198,13 @@ def test_attribute_value_containing_angle_bracket_is_detected(tmp_path: Path) ->
"""
head = f'<script type="application/ld+json" data-cond="a>b">{VALID}</script>'
result = _run(tmp_path, "page.html", head)
- assert result.returncode == 0
- assert result.stderr == ""
+ assert _clean(result)
def test_jsx_json_stringify_inside_ldjson_script_is_not_flagged(tmp_path: Path) -> None:
head = '<script type="application/ld+json">{JSON.stringify(schema)}</script>'
result = _run(tmp_path, "page.jsx", head)
- assert result.returncode == 0
- assert result.stderr == ""
+ assert _clean(result)
def test_renal_replacement_therapy_string_is_not_flagged(tmp_path: Path) -> None:
@@ -208,8 +215,7 @@ def test_renal_replacement_therapy_string_is_not_flagged(tmp_path: Path) -> None
"</script>"
)
result = _run(tmp_path, "page.html", head)
- assert result.returncode == 0
- assert result.stderr == ""
+ assert _clean(result)
def test_top_level_graph_is_detected(tmp_path: Path) -> None:
diff --git a/tests/test_schema_hook_policy.py b/tests/test_schema_hook_policy.py
index b7e5d7a..a3e1012 100644
--- a/tests/test_schema_hook_policy.py
+++ b/tests/test_schema_hook_policy.py
@@ -18,6 +18,7 @@ HOOK = ROOT / "hooks" / "validate-schema.py"
def _run(tmp_path: Path, schema_type: str, extra: str = "") -> int:
+ """2 for critical errors, 1 when warnings were handed to Claude, 0 when clean."""
html = tmp_path / "page.html"
html.write_text(
'<html><head><script type="application/ld+json">\n'
@@ -25,7 +26,8 @@ def _run(tmp_path: Path, schema_type: str, extra: str = "") -> int:
"</script></head></html>",
encoding="utf-8",
)
- return subprocess.run([sys.executable, str(HOOK), str(html)]).returncode
+ result = subprocess.run([sys.executable, str(HOOK), str(html)], capture_output=True, encoding="utf-8")
+ return 1 if result.returncode == 0 and result.stdout.strip() else result.returncode
def _run_payload(tmp_path: Path, payload: object, **kwargs: object) -> subprocess.CompletedProcess:
@@ -39,6 +41,18 @@ def _run_payload(tmp_path: Path, payload: object, **kwargs: object) -> subproces
return subprocess.run([sys.executable, str(HOOK), str(html)], **kwargs)
+def _warnings(result: subprocess.CompletedProcess) -> str:
+ """The warnings a warnings-only run hands Claude: exit 0, JSON additionalContext on stdout."""
+ if result.returncode != 0 or not result.stdout.strip():
+ return ""
+ return json.loads(result.stdout)["hookSpecificOutput"]["additionalContext"]
+
+
+def _clean(result: subprocess.CompletedProcess) -> bool:
+ """Nothing to report: exit 0 and nothing on either stream."""
+ return result.returncode == 0 and result.stdout == "" and result.stderr == ""
+
+
def test_faqpage_not_blocked(tmp_path):
assert _run(tmp_path, "FAQPage") == 0
@@ -55,7 +69,7 @@ def test_valid_top_level_graph_does_not_require_container_type(tmp_path: Path) -
{"@type": "WebSite", "name": "Example"},
],
}
- assert _run_payload(tmp_path, payload).returncode == 0
+ assert _clean(_run_payload(tmp_path, payload, capture_output=True, encoding="utf-8"))
def test_graph_members_inherit_context_but_still_require_type(tmp_path: Path) -> None:
@@ -67,9 +81,8 @@ def test_graph_members_inherit_context_but_still_require_type(tmp_path: Path) ->
# same codec, not the locale default, which is cp1252 on Windows and cannot
# represent the emoji markers.
result = _run_payload(tmp_path, payload, capture_output=True, encoding="utf-8")
- assert result.returncode == 1
- assert "Missing @type" in result.stderr
- assert "Missing @context" not in result.stderr
+ assert "Missing @type" in _warnings(result)
+ assert "Missing @context" not in _warnings(result)
def test_deprecated_graph_member_still_blocks(tmp_path: Path) -> None:
@@ -86,9 +99,8 @@ def test_non_object_graph_members_are_reported_without_crashing(tmp_path: Path)
"@graph": ["not-a-node", 42],
}
result = _run_payload(tmp_path, payload, capture_output=True, encoding="utf-8")
- assert result.returncode == 1
- assert "@graph member 1 must be an object" in result.stderr
- assert "@graph member 2 must be an object" in result.stderr
+ assert "@graph member 1 must be an object" in _warnings(result)
+ assert "@graph member 2 must be an object" in _warnings(result)
def test_graph_must_be_a_list(tmp_path: Path) -> None:
@@ -97,8 +109,7 @@ def test_graph_must_be_a_list(tmp_path: Path) -> None:
"@graph": {"@type": "Organization"},
}
result = _run_payload(tmp_path, payload, capture_output=True, encoding="utf-8")
- assert result.returncode == 1
- assert "@graph must be a list" in result.stderr
+ assert "@graph must be a list" in _warnings(result)
def test_replace_placeholder_matches_tokens_not_normal_words(tmp_path: Path) -> None:
@@ -108,7 +119,7 @@ def test_replace_placeholder_matches_tokens_not_normal_words(tmp_path: Path) ->
"name": "Replacement coils",
"description": "Replace the filter yearly",
}
- assert _run_payload(tmp_path, safe).returncode == 0
+ assert _clean(_run_payload(tmp_path, safe, capture_output=True, encoding="utf-8"))
for value in ("REPLACE", "REPLACE_TITLE"):
blocked = {**safe, "name": value}