import json import os from jsonschema import validate from referencing import Registry, Resource from referencing.jsonschema import DRAFT202012 def main(): """ Validates the example blog posts against the blog.json schema. """ schemas_dir = "schemas" content_dir = "content/blog" # Create a resource for each schema blog_schema_path = os.path.join(schemas_dir, "blog.json") with open(blog_schema_path, "r") as f: blog_schema_resource = Resource.from_contents(json.load(f), default_specification=DRAFT202012) defs_schema_path = os.path.join(schemas_dir, "_defs.json") with open(defs_schema_path, "r") as f: defs_schema_resource = Resource.from_contents(json.load(f), default_specification=DRAFT202012) # Create a registry and add the resources registry = Registry().with_resources( [ ("blog.json", blog_schema_resource), ("_defs.json", defs_schema_resource), ] ) # Validate each blog post for filename in os.listdir(content_dir): if filename.endswith(".json"): filepath = os.path.join(content_dir, filename) with open(filepath, "r") as f: instance = json.load(f) try: validate(instance=instance, schema=blog_schema_resource.contents, registry=registry) print(f"Successfully validated {filename}") except Exception as e: print(f"Validation failed for {filename}: {e}") if __name__ == "__main__": main()