Skip to content

Commit

Permalink
Factor out generic json schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Munro authored Aug 30, 2023
1 parent 635cd7c commit 3c34ff9
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions monty/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ def validate_monty_v2(cls, __input_value, _):
"""
return cls._validate_monty(__input_value)

Check warning on line 263 in monty/json.py

View check run for this annotation

Codecov / codecov/patch

monty/json.py#L263

Added line #L263 was not covered by tests

@classmethod
def __get_validators__(cls):
"""Return validators for use in pydantic"""
yield cls.validate_monty_v1

@classmethod
def __get_pydantic_core_schema__(cls, source_type, handler):
"""
Expand All @@ -279,9 +284,7 @@ def __get_pydantic_core_schema__(cls, source_type, handler):
)

@classmethod
def __get_pydantic_json_schema__(cls, core_schema, handler):
"""JSON schema for MSONable pattern"""

def _generic_json_schema(cls):
return {
"type": "object",
"properties": {
Expand All @@ -293,24 +296,16 @@ def __get_pydantic_json_schema__(cls, core_schema, handler):
}

@classmethod
def __get_validators__(cls):
"""Return validators for use in pydantic"""
yield cls.validate_monty_v1
def __get_pydantic_json_schema__(cls, core_schema, handler):
"""JSON schema for MSONable pattern"""
return cls._generic_json_schema()

Check warning on line 301 in monty/json.py

View check run for this annotation

Codecov / codecov/patch

monty/json.py#L301

Added line #L301 was not covered by tests


@classmethod
def __modify_schema__(cls, field_schema):
"""JSON schema for MSONable pattern"""
field_schema.update(
{
"type": "object",
"properties": {
"@class": {"enum": [cls.__name__], "type": "string"},
"@module": {"enum": [cls.__module__], "type": "string"},
"@version": {"type": "string"},
},
"required": ["@class", "@module"],
}
)
custom_schema = cls._generic_json_schema()
field_schema.update(custom_schema)


class MontyEncoder(json.JSONEncoder):
Expand Down

0 comments on commit 3c34ff9

Please sign in to comment.