mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 00:31:35 +00:00
This commit introduces a new blog feature by implementing a JSON schema for blog posts and providing five example content files. Key changes: - Created a new directory structure for schemas and content (`schemas/`, `content/blog/`). - Implemented a JSON schema for blog posts, split into `blog.json` and `_defs.json` for reusability. - Added five example blog post files with full, realistic content. - Included a Python script (`validate.py`) to validate the example content against the schema.
46 lines
1.5 KiB
Python
46 lines
1.5 KiB
Python
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()
|