Coverage for src / rendercv / schema / json_schema_generator.py: 100%
20 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-25 13:49 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-25 13:49 +0000
1import json
2import pathlib
4import pydantic
5import pydantic_core
7from rendercv import __description__
9from .models.rendercv_model import RenderCVModel
12def generate_json_schema() -> dict:
13 """Generate JSON Schema (Draft-07) from RenderCV Pydantic models.
15 Why:
16 IDEs and validators need machine-readable schema for autocompletion
17 and real-time validation. Custom generator adds RenderCV-specific
18 metadata like title, description, and canonical schema URL.
20 Returns:
21 Draft-07 JSON Schema dictionary.
22 """
24 class RenderCVSchemaGenerator(pydantic.json_schema.GenerateJsonSchema):
25 def generate(
26 self,
27 schema: pydantic_core.CoreSchema,
28 mode: pydantic.json_schema.JsonSchemaMode = "validation",
29 ) -> pydantic.json_schema.JsonSchemaValue:
30 json_schema = super().generate(schema, mode=mode)
31 json_schema["title"] = "RenderCV"
32 json_schema["description"] = __description__
33 json_schema["$id"] = (
34 "https://raw.githubusercontent.com/rendercv/rendercv/main/schema.json"
35 )
36 json_schema["$schema"] = "http://json-schema.org/draft-07/schema#"
37 return json_schema
39 return RenderCVModel.model_json_schema(schema_generator=RenderCVSchemaGenerator)
42def generate_json_schema_file(json_schema_path: pathlib.Path) -> None:
43 """Generate and save JSON Schema to file.
45 Args:
46 json_schema_path: Target file path for schema output.
47 """
48 schema = generate_json_schema()
49 schema_json = json.dumps(schema, indent=2, ensure_ascii=False)
50 json_schema_path.write_text(schema_json, encoding="utf-8")