Skip to content

Commit

Permalink
add JSON schema
Browse files Browse the repository at this point in the history
  • Loading branch information
kisvegabor committed Sep 17, 2024
1 parent 53945b7 commit f71ba6b
Showing 1 changed file with 123 additions and 6 deletions.
129 changes: 123 additions & 6 deletions build_manifest_all.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,112 @@

import json
import requests
from jsonschema import validate, ValidationError

schema_individual = {
"type": "object",
"properties": {
"name": {"type": "string"},
"maintainer": {"type": "string"},
"hostOperatingsystem": {
"type": "array",
"items": {"type": "string"}
},
"environment": {
"type": "array",
"items": {"type": "string"}
},
"hardware": {
"type": "object",
"properties": {
"chipVendor": {"type": "string"},
"manufacturer": {"type": "string"},
"specs": {
"type": "object",
"properties": {
"MCU": {"type": "string"},
"RAM": {"type": "string"},
"Flash": {"type": "string"},
"GPU": {"type": ["string", "null"]},
"Resolution": {"type": "string"},
"Display Size": {"type": "string"},
"Interface": {"type": "string"},
"Color Depth": {"type": "string"},
"Technology": {"type": "string"},
"DPI": {"type": "string"},
"Touch Pad": {"type": "string"}
},
"required": ["MCU", "RAM", "Flash"]
}
},
"required": ["chipVendor", "manufacturer", "specs"]
},
"description": {"type": "string"},
"shortDescription": {"type": "string"},
"urlToClone": {"type": "string"},
"logo": {"type": "string"},
"branches": {
"type": "array",
"items": {"type": "string"}
},
"settings": {
"type": "array",
"items": {
"type": "object",
"properties": {
"type": {"type": "string"},
"label": {"type": "string"},
"options": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {"type": "string"},
"value": {"type": "string"},
"default": {"type": "string", "enum": ["true", "false"], "default": "false"}
},
"required": ["name", "value"]
}
},
"actions": {
"type": "array",
"items": {
"type": "object",
"properties": {
"ifValue": {"type": "string"},
"toAppend": {"type": "string"},
"toReplace": {"type": "string"},
"newContent": {"type": "string"},
"filePath": {"type": "string"}
}
}
}
},
"required": ["type", "label", "options"]
}
}
},
"required": ["name", "maintainer", "hostOperatingsystem", "environment", "description", "shortDescription", "urlToClone", "logo", "branches", "settings"]
}

schema_whole = {
"type": "array",
"items": schema_individual
}

# Function to validate JSON against the schema
def validate_json(json_data, schema):
try:
validate(instance=json_data, schema=schema)
print("JSON is valid")
return True
except ValidationError as e:
print(f"JSON validation error: {e.message}")
if e.path:
print(f"Error location: {' -> '.join(map(str, e.path))}")
else:
print("Error location: Root of the document")
return False

# Function to fetch JSON content from a URL
def fetch_json(url):
Expand All @@ -21,12 +127,23 @@ def fetch_json(url):
url = url.strip() # Remove any extra whitespace or newlines
if url: # Ensure the URL is not empty
print(f"Fetching {url}")
json_data = fetch_json(url)
all_json_data.append(json_data)
try:
json_data = fetch_json(url)
validate_json(json_data, schema_individual) # Validate each JSON fetched
all_json_data.append(json_data) # Append if valid
except requests.exceptions.RequestException as e:
print(f"Error fetching {url}: {e}")
except ValidationError as e:
print(f"Validation failed for {url}: {e.message}")

# Save the concatenated JSON data to a new file
with open('manifest_all.json', 'w') as outfile:
json.dump(all_json_data, outfile, indent=4)

print("All JSON data has been concatenated and saved to 'manifest_all.json'.")
print("Validating the concatenated JSON")
valid = validate_json(all_json_data, schema_whole)
if valid:
# Save the concatenated JSON data to a new file
with open('manifest_all.json', 'w') as outfile:
json.dump(all_json_data, outfile, indent=4)

print("All JSON data has been concatenated and saved to 'manifest_all.json'.")
else:
print("Error: the concatenated JSON is invalid")

0 comments on commit f71ba6b

Please sign in to comment.