From 009875ef2322c2cb43c1c0899588b8be0b9d65b9 Mon Sep 17 00:00:00 2001 From: Sina Atalay Date: Tue, 23 Jul 2024 18:14:08 +0300 Subject: [PATCH] data: fix schema --- rendercv/data/generator.py | 15 +++++++++++++++ rendercv/data/models/curriculum_vitae.py | 5 +---- rendercv/data/models/entry_types.py | 10 ++++++++++ 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/rendercv/data/generator.py b/rendercv/data/generator.py index ea0b6d9e..9f326164 100644 --- a/rendercv/data/generator.py +++ b/rendercv/data/generator.py @@ -156,6 +156,21 @@ def generate_json_schema() -> dict: field["oneOf"] = field["anyOf"] del field["anyOf"] + # Currently, YAML extension in VS Code doesn't work properly with the + # `ListOfEntries` objects. For the best user experience, we will update + # the JSON Schema. If YAML extension in VS Code starts to work properly, + # then we should remove the following code for the correct JSON Schema. + ListOfEntriesForJsonSchema = list[models.Entry] + list_of_entries_json_schema = pydantic.TypeAdapter( + ListOfEntriesForJsonSchema + ).json_schema() + del list_of_entries_json_schema["$defs"] + + # Update the JSON Schema: + json_schema["$defs"]["CurriculumVitae"]["properties"]["sections"]["oneOf"][ + 0 + ]["additionalProperties"] = list_of_entries_json_schema + return json_schema schema = models.RenderCVDataModel.model_json_schema( diff --git a/rendercv/data/models/curriculum_vitae.py b/rendercv/data/models/curriculum_vitae.py index 797d334a..6c44e802 100644 --- a/rendercv/data/models/curriculum_vitae.py +++ b/rendercv/data/models/curriculum_vitae.py @@ -270,14 +270,11 @@ def validate_a_social_network_username(username: str, network: str) -> str: # Create custom types: ================================================================= # ====================================================================================== -# Create a custom type named ListOfEntries: -ListOfEntries = list[entry_types.Entry] - # Create a custom type named SectionContents, which is a list of entries. The entries # can be any of the available entry types. The section is validated with the # `validate_a_section` function. SectionContents = Annotated[ - pydantic.json_schema.SkipJsonSchema[Any] | ListOfEntries, + pydantic.json_schema.SkipJsonSchema[Any] | entry_types.ListOfEntries, pydantic.BeforeValidator( lambda entries: validate_a_section( entries, entry_types=entry_types.available_entry_models diff --git a/rendercv/data/models/entry_types.py b/rendercv/data/models/entry_types.py index b9594a58..d5a52441 100644 --- a/rendercv/data/models/entry_types.py +++ b/rendercv/data/models/entry_types.py @@ -472,6 +472,16 @@ Entry = ( | str ) +# Create a custom type named ListOfEntries: +ListOfEntries = ( + list[OneLineEntry] + | list[NormalEntry] + | list[ExperienceEntry] + | list[EducationEntry] + | list[PublicationEntry] + | list[BulletEntry] + | list[str] +) # ====================================================================================== # Store the available entry types: =====================================================