Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions validation/test_validate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

import io
import sys
import tempfile
import unittest
from contextlib import redirect_stdout
from pathlib import Path
from unittest.mock import patch

from validation import validate


class ValidateCliTest(unittest.TestCase):
def test_schema_invalid_documents_report_errors(self) -> None:
schema_path = Path(__file__).parents[1] / "core-spec" / "osi-schema.json"

cases = (
("", "(root)"),
("[]\n", "(root)"),
("scalar\n", "(root)"),
("semantic_model: invalid\n", "semantic_model"),
)
for content, error_path in cases:
with self.subTest(content=content), tempfile.TemporaryDirectory() as tmpdir:
yaml_path = Path(tmpdir) / "model.yaml"
yaml_path.write_text(content, encoding="utf-8")
stdout = io.StringIO()

with (
patch.object(
sys,
"argv",
["validate.py", str(yaml_path), "--schema", str(schema_path)],
),
redirect_stdout(stdout),
self.assertRaises(SystemExit) as raised,
):
validate.main()

self.assertEqual(raised.exception.code, 1)
self.assertIn(f"[Schema] {error_path}:", stdout.getvalue())


if __name__ == "__main__":
unittest.main()
9 changes: 4 additions & 5 deletions validation/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
SKIP_SQL_VALIDATION = {"MDX", "TABLEAU", "MAQL"}


def validate_schema(data: dict, schema: dict) -> list[str]:
def validate_schema(data: object, schema: dict) -> list[str]:
"""Validate against JSON Schema."""
validator = Draft202012Validator(schema)
errors = []
Expand Down Expand Up @@ -255,11 +255,10 @@ def main():
sys.exit(1)

# Run validations
errors = []
errors.extend(validate_schema(data, schema))
errors = validate_schema(data, schema)

# Run semantic-model-specific checks only for semantic model payloads.
if data.get("semantic_model"):
# Semantic checks assume the schema has established the nested data shapes.
if not errors and isinstance(data, dict) and data.get("semantic_model"):
errors.extend(validate_unique_names(data))
errors.extend(validate_references(data))
errors.extend(validate_sql(data))
Expand Down