json_schema.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #
  2. # Licensed to the Apache Software Foundation (ASF) under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. The ASF licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing,
  13. # software distributed under the License is distributed on an
  14. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. # KIND, either express or implied. See the License for the
  16. # specific language governing permissions and limitations
  17. # under the License.
  18. """jsonschema for validating serialized DAG and operator."""
  19. from __future__ import annotations
  20. import pkgutil
  21. from typing import TYPE_CHECKING, Iterable
  22. from airflow.exceptions import AirflowException
  23. from airflow.settings import json
  24. from airflow.typing_compat import Protocol
  25. if TYPE_CHECKING:
  26. import jsonschema
  27. class Validator(Protocol):
  28. """
  29. This class is only used for type checking.
  30. A workaround for IDEs, mypy, etc. due to the way ``Draft7Validator`` is created.
  31. They are created or do not inherit from proper classes.
  32. Hence, you can not have ``type: Draft7Validator``.
  33. """
  34. def is_valid(self, instance) -> bool:
  35. """Check if the instance is valid under the current schema."""
  36. ...
  37. def validate(self, instance) -> None:
  38. """Check if the instance is valid under the current schema, raising validation error if not."""
  39. ...
  40. def iter_errors(self, instance) -> Iterable[jsonschema.exceptions.ValidationError]:
  41. """Lazily yield each of the validation errors in the given instance."""
  42. ...
  43. def load_dag_schema_dict() -> dict:
  44. """Load & return Json Schema for DAG as Python dict."""
  45. schema_file_name = "schema.json"
  46. schema_file = pkgutil.get_data(__name__, schema_file_name)
  47. if schema_file is None:
  48. raise AirflowException(f"Schema file {schema_file_name} does not exists")
  49. schema = json.loads(schema_file.decode())
  50. return schema
  51. def load_dag_schema() -> Validator:
  52. """Load & Validate Json Schema for DAG."""
  53. import jsonschema
  54. schema = load_dag_schema_dict()
  55. return jsonschema.Draft7Validator(schema)