-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
2,351 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from .builder import JSONSchemaBuilder, build_json_schema | ||
from .dialects import DRAFT_2020_12, OPEN_API_3_1 | ||
|
||
__all__ = [ | ||
"JSONSchemaBuilder", | ||
"build_json_schema", | ||
"DRAFT_2020_12", | ||
"OPEN_API_3_1", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
from dataclasses import dataclass | ||
from numbers import Number | ||
from typing import Dict, Set | ||
|
||
from mashumaro.jsonschema.models import JSONSchema | ||
|
||
|
||
class Annotation: | ||
pass | ||
|
||
|
||
class Constraint(Annotation): | ||
pass | ||
|
||
|
||
class NumberConstraint(Constraint): | ||
pass | ||
|
||
|
||
@dataclass(unsafe_hash=True) | ||
class Minimum(NumberConstraint): | ||
value: Number | ||
|
||
|
||
@dataclass(unsafe_hash=True) | ||
class Maximum(NumberConstraint): | ||
value: Number | ||
|
||
|
||
@dataclass(unsafe_hash=True) | ||
class ExclusiveMinimum(NumberConstraint): | ||
value: Number | ||
|
||
|
||
@dataclass(unsafe_hash=True) | ||
class ExclusiveMaximum(NumberConstraint): | ||
value: Number | ||
|
||
|
||
@dataclass(unsafe_hash=True) | ||
class MultipleOf(NumberConstraint): | ||
value: Number | ||
|
||
|
||
class StringConstraint(Constraint): | ||
pass | ||
|
||
|
||
@dataclass(unsafe_hash=True) | ||
class MinLength(StringConstraint): | ||
value: int | ||
|
||
|
||
@dataclass(unsafe_hash=True) | ||
class MaxLength(StringConstraint): | ||
value: int | ||
|
||
|
||
@dataclass(unsafe_hash=True) | ||
class Pattern(StringConstraint): | ||
value: str | ||
|
||
|
||
class ArrayConstraint(Constraint): | ||
pass | ||
|
||
|
||
@dataclass(unsafe_hash=True) | ||
class MinItems(ArrayConstraint): | ||
value: int | ||
|
||
|
||
@dataclass(unsafe_hash=True) | ||
class MaxItems(ArrayConstraint): | ||
value: int | ||
|
||
|
||
@dataclass(unsafe_hash=True) | ||
class UniqueItems(ArrayConstraint): | ||
value: bool | ||
|
||
|
||
@dataclass(unsafe_hash=True) | ||
class Contains(ArrayConstraint): | ||
value: JSONSchema | ||
|
||
|
||
@dataclass(unsafe_hash=True) | ||
class MinContains(ArrayConstraint): | ||
value: int | ||
|
||
|
||
@dataclass(unsafe_hash=True) | ||
class MaxContains(ArrayConstraint): | ||
value: int | ||
|
||
|
||
class ObjectConstraint(Constraint): | ||
pass | ||
|
||
|
||
@dataclass(unsafe_hash=True) | ||
class MaxProperties(ObjectConstraint): | ||
value: int | ||
|
||
|
||
@dataclass(unsafe_hash=True) | ||
class MinProperties(ObjectConstraint): | ||
value: int | ||
|
||
|
||
@dataclass | ||
class DependentRequired(ObjectConstraint): | ||
value: Dict[str, Set[str]] | ||
|
||
|
||
__all__ = [ | ||
"Annotation", | ||
"MultipleOf", | ||
"Maximum", | ||
"ExclusiveMaximum", | ||
"Minimum", | ||
"ExclusiveMinimum", | ||
"MaxLength", | ||
"MinLength", | ||
"Pattern", | ||
"MaxItems", | ||
"MinItems", | ||
"UniqueItems", | ||
"Contains", | ||
"MaxContains", | ||
"MinContains", | ||
"MaxProperties", | ||
"MinProperties", | ||
"DependentRequired", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from dataclasses import dataclass | ||
from typing import Any, Dict, List, Optional, Type | ||
|
||
from mashumaro.jsonschema.dialects import DRAFT_2020_12, JSONSchemaDialect | ||
from mashumaro.jsonschema.models import Context, JSONSchema | ||
from mashumaro.jsonschema.schema import Instance, get_schema | ||
|
||
try: | ||
from mashumaro.mixins.orjson import ( | ||
DataClassORJSONMixin as DataClassJSONMixin, | ||
) | ||
except ImportError: # pragma no cover | ||
from mashumaro.mixins.json import DataClassJSONMixin # type: ignore | ||
|
||
|
||
def build_json_schema( | ||
instance_type: Type, | ||
context: Optional[Context] = None, | ||
with_definitions: bool = True, | ||
all_refs: Optional[bool] = None, | ||
) -> JSONSchema: | ||
if context is None: | ||
context = Context() | ||
if all_refs is not None: | ||
context.all_refs = all_refs | ||
instance = Instance(instance_type) | ||
schema = get_schema(instance, context) | ||
if with_definitions and context.definitions: | ||
schema.definitions = context.definitions | ||
return schema | ||
|
||
|
||
@dataclass | ||
class JSONSchemaDefinitions(DataClassJSONMixin): | ||
definitions: Dict[str, JSONSchema] | ||
|
||
def __post_serialize__( # type: ignore | ||
self, d: Dict[Any, Any] | ||
) -> List[Dict[str, Any]]: | ||
return d["definitions"] | ||
|
||
|
||
class JSONSchemaBuilder: | ||
def __init__( | ||
self, | ||
dialect: JSONSchemaDialect = DRAFT_2020_12, | ||
all_refs: Optional[bool] = None, | ||
): | ||
if all_refs is None: | ||
all_refs = dialect.all_refs | ||
self.context = Context(dialect=dialect, all_refs=all_refs) | ||
|
||
def build(self, instance_type: Type) -> JSONSchema: | ||
return build_json_schema( | ||
instance_type=instance_type, | ||
context=self.context, | ||
with_definitions=False, | ||
) | ||
|
||
def get_definitions(self) -> JSONSchemaDefinitions: | ||
return JSONSchemaDefinitions(self.context.definitions) | ||
|
||
|
||
__all__ = ["JSONSchemaBuilder"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass(frozen=True) | ||
class JSONSchemaDialect: | ||
uri: str | ||
definitions_root_pointer: str | ||
all_refs: bool | ||
|
||
|
||
@dataclass(frozen=True) | ||
class JSONSchemaDraft202012Dialect(JSONSchemaDialect): | ||
uri: str = "https://json-schema.org/draft/2020-12/schema" | ||
definitions_root_pointer: str = "#/defs" | ||
all_refs: bool = False | ||
|
||
|
||
@dataclass(frozen=True) | ||
class OpenAPISchema31Dialect(JSONSchemaDialect): | ||
uri: str = "https://spec.openapis.org/oas/3.1/dialect/base" | ||
definitions_root_pointer: str = "#/components/schemas" | ||
all_refs: bool = True | ||
|
||
|
||
DRAFT_2020_12 = JSONSchemaDraft202012Dialect() | ||
OPEN_API_3_1 = OpenAPISchema31Dialect() | ||
|
||
|
||
__all__ = ["JSONSchemaDialect", "DRAFT_2020_12", "OPEN_API_3_1"] |
Oops, something went wrong.