Skip to content

Commit

Permalink
Add more json-schama checks + display all errors (#12805)
Browse files Browse the repository at this point in the history
  • Loading branch information
mik-laj committed Dec 4, 2020
1 parent 88aa174 commit 6878a7b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
33 changes: 33 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,39 @@ repos:
^airflow/serialization/schema.json$
require_serial: true
additional_dependencies: ['jsonschema==3.2.0', 'PyYAML==5.3.1', 'requests==2.25.0']
- id: json-schema
name: Lint JSON Schema files with JSON Schema
entry: ./scripts/ci/pre_commit/pre_commit_json_schema.py
args:
- --spec-url
- https://raw.githubusercontent.com/instrumenta/kubernetes-json-schema/master/v1.14.9/service-v1.json
language: python
pass_filenames: true
files: scripts/ci/kubernetes/nodeport.yaml
require_serial: true
additional_dependencies: ['jsonschema==3.2.0', 'PyYAML==5.3.1', 'requests==2.25.0']
- id: json-schema
name: Lint Docker compose files with JSON Schema
entry: ./scripts/ci/pre_commit/pre_commit_json_schema.py
args:
- --spec-url
- https://raw.githubusercontent.com/compose-spec/compose-spec/master/schema/compose-spec.json
language: python
pass_filenames: true
files: scripts/ci/docker-compose/.+.yml
require_serial: true
additional_dependencies: ['jsonschema==3.2.0', 'PyYAML==5.3.1', 'requests==2.25.0']
- id: json-schema
name: Lint chart/values.yaml file with JSON Schema
entry: ./scripts/ci/pre_commit/pre_commit_json_schema.py
args:
- --spec-file
- chart/values.schema.json
language: python
pass_filenames: true
files: chart/values.yaml$
require_serial: true
additional_dependencies: ['jsonschema==3.2.0', 'PyYAML==5.3.1', 'requests==2.25.0']
## ADD MOST PRE-COMMITS ABOVE THAT LINE
# The below pre-commits are those requiring CI image to be built
- id: build
Expand Down
15 changes: 6 additions & 9 deletions scripts/ci/pre_commit/pre_commit_json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ def _cache_dir():

def _gethash(string: str):
hash_object = hashlib.sha256(string.encode())
return hash_object.hexdigest()
return hash_object.hexdigest()[:8]


def fetch_and_cache(url: str, output_filename: str):
"""Fetch URL to local cache and returns path."""
cache_key = _gethash(url)
cache_dir = _cache_dir()
cache_metadata_filepath = os.path.join(cache_dir, "cache-metadata.json")
cache_filepath = os.path.join(cache_dir, f"{cache_key}-{output_filename}")
cache_filepath = os.path.join(cache_dir, f"{cache_key}-{output_filename[:64]}")
# Create cache directory
os.makedirs(cache_dir, exist_ok=True)
# Load cache metadata
Expand Down Expand Up @@ -101,7 +101,7 @@ def load_file(file_path: str):
return json.load(input_file)
elif file_path.lower().endswith('.yaml') or file_path.lower().endswith('.yml'):
with open(file_path) as input_file:
return yaml.load(input_file)
return yaml.safe_load(input_file)
raise _ValidatorError("Unknown file format. Supported extension: '.yaml', '.json'")


Expand All @@ -121,12 +121,9 @@ def _process_files(validator, file_paths: List[str]):
exit_code = 0
for input_path in file_paths:
print("Processing file: ", input_path)
try:
instance = load_file(input_path)
validator.is_valid(instance)
except _ValidatorError as ex:
print('Problem processing the file.')
print(ex)
instance = load_file(input_path)
for error in validator.iter_errors(instance):
print(error)
exit_code = 1
return exit_code

Expand Down

0 comments on commit 6878a7b

Please sign in to comment.