Skip to content

Commit

Permalink
convert some oneOf checks into if-else chains to get better errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Beck committed Jul 9, 2019
1 parent 5f1ea26 commit 3dc1aff
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion core/dbt/contracts/graph/parsed.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dataclasses import dataclass, field
from typing import Optional, Union, List, Dict, Any
from typing import Optional, Union, List, Dict, Any, Type, Tuple

from hologram import JsonSchemaMixin
from hologram.helpers import StrEnum, NewPatternType
Expand Down Expand Up @@ -302,11 +302,48 @@ class IntermediateSnapshotNode(ParsedNode):
config: NodeConfig


def _create_if_else_chain(
key: str, criteria: List[Tuple[str, Type[JsonSchemaMixin]]]
) -> dict:
"""Mutate a given schema key that contains a 'oneOf' to instead be an
'if-then-else' chain. This results is much better/more consistent errors
from jsonschema.
"""
result = schema = {}
criteria = criteria[:]
while criteria:
if_clause, then_clause = criteria.pop()
schema['if'] = {'properties': {
key: {'enum': [if_clause]}
}}
schema['then'] = then_clause.json_schema()
schema['else'] = {}
schema = schema['else']
schema['additionalProperties'] = False
schema['required'] = ['invalid']
return result


@dataclass
class ParsedSnapshotNode(ParsedNode):
resource_type: SnapshotType
config: Union[CheckSnapshotConfig, TimestampSnapshotConfig]

@classmethod
def json_schema(cls):
schema = super().json_schema()

# mess with config
configs = [
(CheckStrategy.Check, CheckSnapshotConfig),
(TimestampStrategy.Timestamp, TimestampSnapshotConfig),
]

schema['properties']['config'] = _create_if_else_chain(
'strategy', configs
)
return schema


# The parsed node update is only the 'patch', not the test. The test became a
# regular parsed node. Note that description and columns must be present, but
Expand Down

0 comments on commit 3dc1aff

Please sign in to comment.