-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generate jsonschema from pydantic v2 #159
base: main
Are you sure you want to change the base?
Changes from all commits
6484bab
8ea4b77
12573aa
b99f338
3fd5b97
c684478
51e3409
bce1628
cb47ad2
df5ded7
fe143ae
eef936c
d9dd764
2254ebc
603b8f7
a6b4e46
7b00793
97e850a
2f1b25e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
|
||
.DS_Store | ||
node_modules/ | ||
.venv | ||
.venv | ||
env | ||
__pycache__ |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you mean to move this to |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import json | ||
from pathlib import Path | ||
from typing import Any, Dict | ||
|
||
from src.latest.dbt_cloud import DbtCloud | ||
from src.latest.dbt_yml_files import DbtYmlFiles | ||
from src.latest.dependencies import Dependencies, Packages | ||
from src.latest.selectors import Selectors | ||
from pydantic.json_schema import GenerateJsonSchema | ||
|
||
|
||
class RemoveNullsGenerateJsonSchema(GenerateJsonSchema): | ||
"""A GenerateJsonSchema which removes nullability from types. | ||
|
||
We do not want to include optional values in the json schema because | ||
that would inhibit code completion and validation. | ||
|
||
Certain properties (such as freshness overrides) need to be nullable, | ||
which can be achieved by setting the $comment value below. | ||
""" | ||
|
||
def _remove_null(self, json_schema: Dict[str, Any]): | ||
if "$comment" in json_schema and json_schema["$comment"] == "truly_nullable": | ||
return | ||
if "anyOf" in json_schema: | ||
json_schema["anyOf"] = [ | ||
item for item in json_schema["anyOf"] if item != {"type": "null"} | ||
] | ||
for v in json_schema.values(): | ||
if isinstance(v, dict): | ||
self._remove_null(v) | ||
|
||
def generate(self, schema, mode="validation"): | ||
json_schema = super().generate(schema, mode=mode) | ||
json_schema["$schema"] = "http://json-schema.org/draft-07/schema#" | ||
self._remove_null(json_schema) | ||
return json_schema | ||
|
||
|
||
if __name__ == "__main__": | ||
files = { | ||
"dbt_yml_files": DbtYmlFiles, | ||
"dependencies": Dependencies, | ||
"packages": Packages, | ||
"selectors": Selectors, | ||
"dbt_cloud": DbtCloud, | ||
} | ||
output_directory = Path("schemas/latest") | ||
for file_name, model in files.items(): | ||
schema_file = output_directory / f"{file_name}-latest.json" | ||
schema_file.parent.mkdir(parents=True, exist_ok=True) | ||
schema = model.model_json_schema( | ||
mode="validation", schema_generator=RemoveNullsGenerateJsonSchema | ||
) | ||
print("Generating", schema_file) | ||
schema_file.write_text(json.dumps(schema, indent=2)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
datamodel-code-generator | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This shouldn't be necessary, right? This package was only necessary to initially transform json-schema to Pydantic, right? |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"type": "object", | ||
"properties": { | ||
"project-id": { | ||
"type": "string" | ||
}, | ||
"defer-env-id": { | ||
"type": "string" | ||
} | ||
}, | ||
"required": ["project-id"], | ||
"additionalProperties": false | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you meant to reference a directory, not a file