Skip to content

Commit

Permalink
use python for cuteness overload
Browse files Browse the repository at this point in the history
  • Loading branch information
M. Fatih Cırıt committed Feb 20, 2024
1 parent c8ecf5f commit 502878a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 44 deletions.
47 changes: 3 additions & 44 deletions json-schema-check/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,10 @@ runs:
steps:
- name: Install JSON Schema packages
run: |
pip3 install -U check-jsonschema
pip3 install -U check-jsonschema colorama

Check warning on line 9 in json-schema-check/action.yaml

View workflow job for this annotation

GitHub Actions / spell-check-differential

Unknown word (colorama)
shell: bash

- name: Check configuration files
- name: Validate JSON Schemas
run: |
# ANSI color codes
export RED='\033[0;31m'
export GREEN='\033[0;32m'
export NO_COLOR='\033[0m'
export CYAN='\033[0;36m'
export LIGHT_CYAN='\033[1;36m'
validation_failed=0 # Initialize as false
find . -path '*/schema/*.schema.json' -exec bash -c '
schemaFile="${1#./}" # Normalize schema file path to remove leading ./
baseName="${schemaFile##*/}" # Extract just the file name
baseName="${baseName%.schema.json}" # Remove the schema.json extension
# Determine the directory of the schema and switch to corresponding config directory
configDir="${schemaFile%/*}" # Get the directory path for schema, without leading ./
configDir="${configDir/schema/config}" # Switch to config directory
shopt -s nullglob # Enable nullglob to handle no matches as an empty array
configFiles=("${configDir}/${baseName}"*.param.yaml) # Pattern match for config files
local_failed=0 # Track failures for this schema
for configFile in "${configFiles[@]}"; do
echo -ne "🔍 Validating: ${CYAN}${configFile}${NO_COLOR} 🆚 ${LIGHT_CYAN}${schemaFile}${NO_COLOR} ▶️ "
if ! check-jsonschema --schemafile "$schemaFile" "$configFile"; then
# If validation fails, print failure message
echo -e "${RED}❌ Failed${NO_COLOR}"
local_failed=1 # Mark as failed
fi
done
exit $local_failed # Exit with the local failure status
' bash {} \; || validation_failed=1 # Catch any failure and mark validation as failed
if [ "$validation_failed" -eq 1 ]; then
echo -e "${RED}❗ Validation failed for one or more files.${NO_COLOR}"
exit 1
else
echo -e "${GREEN}✔️ All validations passed successfully.${NO_COLOR}"
fi
python validate_json_schemas.py
shell: bash
48 changes: 48 additions & 0 deletions json-schema-check/validate_json_schemas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import os
import subprocess
import glob
import colorama

# Initialize colorama to auto-reset colors
colorama.init(autoreset=True)

def main():
validation_failed = False

for schema_path in glob.glob('./**/schema/*.schema.json', recursive=True):
schema_file = os.path.relpath(schema_path, './')
base_name = os.path.basename(schema_file).replace('.schema.json', '')
config_dir = os.path.dirname(schema_file).replace('schema', 'config')

config_files = glob.glob(f'{config_dir}/{base_name}*.param.yaml')
if not config_files:
print(colorama.Fore.YELLOW + f'No configuration files found for schema {schema_file}.')
continue

for config_file in config_files:
# Customized print statement with different colors
print(
colorama.Style.BRIGHT + '🔍 Validating: ' +
colorama.Style.RESET_ALL +
colorama.Fore.CYAN + f'{config_file} ' +
colorama.Style.RESET_ALL + '🆚 ' +
colorama.Fore.BLUE + f'{schema_file}' +
colorama.Style.RESET_ALL,
end=' '
)
result = subprocess.run(['check-jsonschema', '--schemafile', schema_file, config_file], capture_output=True)
if result.returncode != 0:
print(colorama.Fore.RED + '❌ Failed')
print(colorama.Fore.RED + result.stdout.decode('utf-8'))
validation_failed = True
else:
print(colorama.Fore.GREEN + '✅ Passed')

if validation_failed:
print(colorama.Style.BRIGHT + colorama.Fore.RED + '❗ Validation failed for one or more files.')
# exit(1)
else:
print(colorama.Style.BRIGHT + colorama.Fore.GREEN + '✔️ All validations passed successfully.')

if __name__ == "__main__":
main()

0 comments on commit 502878a

Please sign in to comment.