Skip to content

Commit

Permalink
Add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
manzt committed Nov 11, 2024
1 parent 7ec6102 commit 3682b82
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
32 changes: 26 additions & 6 deletions src/higlass_schema/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,47 @@

class _GenerateJsonSchema(GenerateJsonSchema):
def field_title_should_be_set(self, schema: CoreSchemaOrField) -> bool:
"""Check if the title should be set for a field.
Override the default implementation to not set the title for core
schemas. Makes the final schema more readable by removing
redundant titles. Explicit Field(title=...) can still be used.
"""
return_value = super().field_title_should_be_set(schema)
if return_value and is_core_schema(schema):
return False
return return_value

def nullable_schema(self, schema: core_schema.NullableSchema) -> JsonSchemaValue:
inner_json_schema = self.generate_inner(schema["schema"])
# ignore the nullable
return inner_json_schema
"""Generate a JSON schema for a nullable schema.
This overrides the default implementation to ignore the nullable
and generate a more simple schema. All the Optional[T] fields
are converted to T (instead of the
default {"anyOf": [{"type": "null"}, {"type": "T"}]}).
"""
return self.generate_inner(schema["schema"])

def default_schema(self, schema: core_schema.WithDefaultSchema) -> JsonSchemaValue:
"""Generate a JSON schema for a schema with a default value.
Similar to above, this overrides the default implementation to
not explicity set {"default": null} in the schema when the field
is Optional[T] = None.
"""
if schema.get("default") is None:
return self.generate_inner(schema["schema"])
return super().default_schema(schema)

def union_schema(self, schema: core_schema.UnionSchema) -> JsonSchemaValue:
return super().union_schema(schema)

def generate(
self, schema: core_schema.CoreSchema, mode: JsonSchemaMode = "validation"
) -> JsonSchemaValue:
"""Generate a JSON schema.
This overrides the default implementation to remove the titles
from the definitions. This makes the final schema more readable.
"""

json_schema = super().generate(schema, mode=mode)
# clear the titles from the definitions
for d in json_schema.get("$defs", {}).values():
Expand Down
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3682b82

Please sign in to comment.