-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…#917) * Overwrite schema to allow required+enum keys * Fixing missing import * isrot * Address comments * Update singer_sdk/helpers/_schema.py Co-authored-by: Edgar R. M. <[email protected]> * Update singer_sdk/helpers/_schema.py Co-authored-by: Edgar R. M. <[email protected]> * Update singer_sdk/helpers/_schema.py Co-authored-by: Edgar R. M. <[email protected]> * Update singer_sdk/helpers/_schema.py Co-authored-by: Edgar R. M. <[email protected]> * black Co-authored-by: Edgar R. M. <[email protected]> Closes #901
- Loading branch information
1 parent
52db6ec
commit a3f570c
Showing
6 changed files
with
177 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
"""Provides an object model for JSON Schema.""" | ||
|
||
from dataclasses import dataclass | ||
from typing import Any, List, Optional, Union | ||
|
||
from singer import Schema | ||
|
||
# These are keys defined in the JSON Schema spec that do not themselves contain | ||
# schemas (or lists of schemas) | ||
STANDARD_KEYS = [ | ||
"title", | ||
"description", | ||
"minimum", | ||
"maximum", | ||
"exclusiveMinimum", | ||
"exclusiveMaximum", | ||
"multipleOf", | ||
"maxLength", | ||
"minLength", | ||
"format", | ||
"type", | ||
"required", | ||
"enum", | ||
# These are NOT simple keys (they can contain schemas themselves). We could | ||
# consider adding extra handling to them. | ||
"additionalProperties", | ||
"anyOf", | ||
"patternProperties", | ||
] | ||
|
||
|
||
@dataclass | ||
class SchemaPlus(Schema): | ||
"""Object model for JSON Schema. | ||
Tap and Target authors may find this to be more convenient than | ||
working directly with JSON Schema data structures. | ||
This is based on, and overwrites | ||
https://github.com/transferwise/pipelinewise-singer-python/blob/master/singer/schema.py. | ||
This is because we wanted to expand it with extra STANDARD_KEYS. | ||
""" | ||
|
||
type: Optional[Union[str, List[str]]] = None | ||
properties: Optional[dict] = None | ||
items: Optional[Any] = None | ||
description: Optional[str] = None | ||
minimum: Optional[float] = None | ||
maximum: Optional[float] = None | ||
exclusiveMinimum: Optional[float] = None | ||
exclusiveMaximum: Optional[float] = None | ||
multipleOf: Optional[float] = None | ||
maxLength: Optional[int] = None | ||
minLength: Optional[int] = None | ||
anyOf: Optional[Any] = None | ||
format: Optional[str] = None | ||
additionalProperties: Optional[Any] = None | ||
patternProperties: Optional[Any] = None | ||
required: Optional[List[str]] = None | ||
enum: Optional[List[Any]] = None | ||
title: Optional[str] = None | ||
|
||
def to_dict(self): | ||
"""Return the raw JSON Schema as a (possibly nested) dict.""" | ||
result = {} | ||
|
||
if self.properties is not None: | ||
result["properties"] = {k: v.to_dict() for k, v in self.properties.items()} | ||
|
||
if self.items is not None: | ||
result["items"] = self.items.to_dict() | ||
|
||
for key in STANDARD_KEYS: | ||
if self.__dict__.get(key) is not None: | ||
result[key] = self.__dict__[key] | ||
|
||
return result | ||
|
||
@classmethod | ||
def from_dict(cls, data, **schema_defaults): | ||
"""Initialize a Schema object based on the JSON Schema structure. | ||
:param schema_defaults: The default values to the Schema constructor. | ||
""" | ||
kwargs = schema_defaults.copy() | ||
properties = data.get("properties") | ||
items = data.get("items") | ||
|
||
if properties is not None: | ||
kwargs["properties"] = { | ||
k: SchemaPlus.from_dict(v, **schema_defaults) | ||
for k, v in properties.items() | ||
} | ||
if items is not None: | ||
kwargs["items"] = SchemaPlus.from_dict(items, **schema_defaults) | ||
for key in STANDARD_KEYS: | ||
if key in data: | ||
kwargs[key] = data[key] | ||
return SchemaPlus(**kwargs) |
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
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,66 @@ | ||
""" | ||
Testing that SchemaPlus can convert schemas lossless from and to dicts. | ||
Schemas are taken from these examples; https://json-schema.org/learn/miscellaneous-examples.html | ||
NOTE: The following properties are not currently supported; | ||
pattern | ||
unevaluatedProperties | ||
propertyNames | ||
minProperties | ||
maxProperties | ||
prefixItems | ||
contains | ||
minContains | ||
maxContains | ||
minItems | ||
maxItems | ||
uniqueItems | ||
enum | ||
const | ||
contentMediaType | ||
contentEncoding | ||
allOf | ||
oneOf | ||
not | ||
Some of these could be trivially added (if they are SIMPLE_PROPERTIES. | ||
Some might need more thinking if they can contain schemas (though, note that we also treat 'additionalProperties', | ||
'anyOf' and' patternProperties' as SIMPLE even though they can contain schemas. | ||
""" | ||
|
||
from singer_sdk.helpers._schema import SchemaPlus | ||
|
||
|
||
def test_simple_schema(): | ||
simple_schema = { | ||
"title": "Longitude and Latitude Values", | ||
"description": "A geographical coordinate.", | ||
"required": ["latitude", "longitude"], | ||
"type": "object", | ||
"properties": { | ||
"latitude": {"type": "number", "minimum": -90, "maximum": 90}, | ||
"longitude": {"type": "number", "minimum": -180, "maximum": 180}, | ||
}, | ||
} | ||
|
||
schema_plus = SchemaPlus.from_dict(simple_schema) | ||
assert schema_plus.to_dict() == simple_schema | ||
assert schema_plus.required == ["latitude", "longitude"] | ||
assert isinstance(schema_plus.properties["latitude"], SchemaPlus) | ||
latitude = schema_plus.properties["latitude"] | ||
assert latitude.type == "number" | ||
|
||
|
||
def test_schema_with_items(): | ||
schema = { | ||
"description": "A representation of a person, company, organization, or place", | ||
"type": "object", | ||
"properties": {"fruits": {"type": "array", "items": {"type": "string"}}}, | ||
} | ||
schema_plus = SchemaPlus.from_dict(schema) | ||
assert schema_plus.to_dict() == schema | ||
assert isinstance(schema_plus.properties["fruits"], SchemaPlus) | ||
fruits = schema_plus.properties["fruits"] | ||
assert isinstance(fruits.items, SchemaPlus) | ||
assert fruits.items.type == "string" |