Skip to content

Commit

Permalink
implement bulk schema validateion for input directories
Browse files Browse the repository at this point in the history
  • Loading branch information
Eiko Thomas committed Nov 1, 2024
1 parent 6d43182 commit 4d8df78
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
11 changes: 11 additions & 0 deletions bin/runGithubActions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,17 @@ if python validateSchemas.py --schema tests/resources/evil/evil2.json --noEmpty
exit 1
fi

if ! python validateSchemas.py --inputDir tests/resources/models --noEmptySchemas > /dev/null; then
echo "didn't validate inputDir: tests/resources/models"
popd &> /dev/null
exit 1
fi

if python validateSchemas.py --inputDir tests/resources/evil --noEmptySchemas > /dev/null; then
echo "didn't fail on inputDir: tests/resources/evil"
popd &> /dev/null
exit 1
fi

echo "all good :)"
popd > /dev/null
28 changes: 27 additions & 1 deletion validateSchemas.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import argparse
import sys
import os
import json
import yacg.builder.impl.dictionaryBuilder as builder
from yacg.util.outputUtils import printError
Expand All @@ -14,6 +15,7 @@
parser.add_argument('--schema', help='path to model schema')
parser.add_argument('--inputDir', help='path to start to search for JSON schemas')
parser.add_argument('--noEmptySchemas', help='Checks that at least one type is found in the schema', action='store_true') # noqa: E501
parser.add_argument('--blackListed', nargs='+', help='schemas that should not be handled in the template')


def main():
Expand All @@ -33,7 +35,31 @@ def main():
printError(f"Error while validating schema: ${args.schema}")
sys.exit(1)
if args.inputDir is not None:
pass
violatedFiles = []
for root, _, files in os.walk(args.inputDir):
for file in files:
if file.endswith(".json"):
if (args.blackListed is not None) and (file in args.blackListed):
continue
file_path = os.path.join(root, file)
try:
with open(file_path, "r") as f:
json_data = json.load(f)
if not isinstance(json_data, dict):
continue
if json_data.get("$schema", None) is not None:
schemaAsDict = builder.getParsedSchemaFromJson(file_path)
if args.noEmptySchemas:
extractedTypes = builder.extractTypes(schemaAsDict, file_path, [], False)
if len(extractedTypes) == 0:
violatedFiles.append(file_path)
except (json.JSONDecodeError, FileNotFoundError, IndexError) as e:
# Handle errors: skip files with bad JSON or that can't be read
violatedFiles.append(file_path)
if len(violatedFiles) > 0:
print(f"Error while validating schemas in dir: ${args.inputDir}")
print(f"corrupted files are: ${violatedFiles}")
sys.exit(1)


if __name__ == '__main__':
Expand Down
3 changes: 3 additions & 0 deletions yacg/util/stringUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ def toUpperCamelCase(text, separator=None):
"""converts a given Text to a upper camel case text
this is an example -> ThisIsAnExample"""

if text is None:
return ""

splittedText = text.split() if separator is None else text.split(separator)
upperCamelCase = ''
for t in splittedText:
Expand Down

0 comments on commit 4d8df78

Please sign in to comment.