Skip to content

Commit

Permalink
ISSUE #11: Ignore empty file option added
Browse files Browse the repository at this point in the history
ISSUE #7: All files will be checked before crashing with error
  • Loading branch information
lyubick committed Nov 15, 2023
1 parent faed376 commit cad8de7
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/self-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,8 @@ jobs:
with:
json-schema-file: test/schema/json_schema.json
yaml-json-file-dir: test/YAMLs/valid.json,test/JSONs/valid.json
- name: Run Action (empty files valid)
uses: ./
with:
json-schema-file: test/schema/json_schema.json
yaml-json-file-dir: test/emptyJSONs,test/emptyYAMLSs,
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,6 @@ Failed validating 'additionalProperties' in schema:
On instance:
{'field2': 'Value2'}
```

## Supported & Used by
* [Printify](https://github.com/printify)
7 changes: 7 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@ inputs:
in directory required.
required: false
default: 'false'
ignore-empty:
description: |
True/False, provide False if error should be raised on empty files,
otherwise empty files will be ignored.
required: false
default: 'true'
runs:
using: 'docker'
image: 'Dockerfile'
args:
- ${{ inputs.json-schema-file }}
- ${{ inputs.yaml-json-file-dir }}
- ${{ inputs.recursive }}
- ${{ inputs.ignore-empty }}
3 changes: 3 additions & 0 deletions test/JSONs/invalid_2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"field2": "Value2_2"
}
Empty file added test/emptyJSONs/empty.json
Empty file.
Empty file added test/emptyYAMLs/empty.yaml
Empty file.
40 changes: 32 additions & 8 deletions test/test_sanity.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from pathlib import Path

import validator
Expand Down Expand Up @@ -27,8 +28,10 @@ def test4_validate_invalid_file(self):
try:
validator.validate_files(files, schema)
assert False
except jsonschema.exceptions.ValidationError as exc:
assert exc.instance == {'field2': 'Value2'}
except Exception as exc:
assert len(exc.args) == 1
assert len(exc.args[0]) == 1
assert exc.args[0][0][1] == {'field2': 'Value2'}

def test5_validate_valid_file_json(self):
schema = validator.load_schema(f'{self.abs_path}/schema/json_schema.json')
Expand All @@ -41,23 +44,44 @@ def test6_validate_invalid_file_json(self):
try:
validator.validate_files(files, schema)
assert False
except jsonschema.exceptions.ValidationError as exc:
assert exc.instance == {'field2': 'Value2'}
except Exception as exc:
assert len(exc.args) == 1
assert len(exc.args[0]) == 1
assert exc.args[0][0][1] == {'field2': 'Value2'}

def test7_validate_folder_yaml(self):
schema = validator.load_schema(f'{self.abs_path}/schema/json_schema.json')
files = validator.get_yaml_json_files_list(f'{self.abs_path}/YAMLs/', False)
try:
validator.validate_files(files, schema)
assert False
except jsonschema.exceptions.ValidationError as exc:
assert exc.instance == {'field2': 'Value2'}
except Exception as exc:
assert len(exc.args) == 1
assert len(exc.args[0]) == 1
assert exc.args[0][0][1] == {'field2': 'Value2'}

def test8_validate_folder_json(self):
schema = validator.load_schema(f'{self.abs_path}/schema/json_schema.json')
files = validator.get_yaml_json_files_list(f'{self.abs_path}/JSONs/', False)
try:
validator.validate_files(files, schema)
assert False
except jsonschema.exceptions.ValidationError as exc:
assert exc.instance == {'field2': 'Value2'}
except Exception as exc:
assert len(exc.args) == 1
assert len(exc.args[0]) == 2
assert exc.args[0][1][1] == {'field2': 'Value2'}
assert exc.args[0][0][1] == {'field2': 'Value2_2'}

def test9_validate_empty_json(self):
schema = validator.load_schema(f'{self.abs_path}/schema/json_schema.json')
files = validator.get_yaml_json_files_list(f'{self.abs_path}/emptyJSONs/empty.json', False, True)
validator.validate_files(files, schema)

def test9_validate_empty_json_fail(self):
schema = validator.load_schema(f'{self.abs_path}/schema/json_schema.json')
files = validator.get_yaml_json_files_list(f'{self.abs_path}/emptyJSONs/empty.json', False, False)
try:
validator.validate_files(files, schema)
assert False
except json.JSONDecodeError as exc:
assert True
12 changes: 10 additions & 2 deletions validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def load_schema(schema_file_path: str) -> json:
raise f'Provided JSON Schema file does not exist!'


def get_yaml_json_files_list(files_paths_list: str, is_recursive: bool) -> list[str]:
def get_yaml_json_files_list(files_paths_list: str, is_recursive: bool, ignore_empty_files: bool = False) -> list[str]:
yaml_input = list(files_paths_list.split(','))

yaml_files = []
Expand All @@ -39,10 +39,14 @@ def get_yaml_json_files_list(files_paths_list: str, is_recursive: bool) -> list[
elif os.path.isfile(yaml_object):
yaml_files.append(yaml_object)

if ignore_empty_files:
yaml_files = list(filter(lambda f: os.path.getsize(f) > 0, yaml_files))

return yaml_files


def validate_files(yaml_files: list, json_schema: json):
failed = []
for yaml_file in yaml_files:
if os.path.exists(yaml_file):
if os.path.isfile(yaml_file):
Expand All @@ -55,11 +59,15 @@ def validate_files(yaml_files: list, json_schema: json):
validate(instance=yaml_json, schema=json_schema)
except ValidationError as exc:
print(f'File `{yaml_file}` failed validation with >>>`{exc}`<<<', file=sys.stderr)
raise exc
failed.append((yaml_file, exc.instance))
else:
raise f'Provided YAML file is not a file! Please provide legit YAML file.'
else:
raise f'Provided YAML file does not exist!'

if failed:
raise Exception(failed)

return True


Expand Down

0 comments on commit cad8de7

Please sign in to comment.