Skip to content

Commit

Permalink
refacor: ♻️ switch between the two validation schemas depending on co…
Browse files Browse the repository at this point in the history
…demeta version
  • Loading branch information
slugb0t committed Jan 4, 2025
1 parent 92c01c4 commit f2e6db7
Showing 1 changed file with 42 additions and 19 deletions.
61 changes: 42 additions & 19 deletions validator/apis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,23 +218,46 @@ def post(self):
"message": "Validation Error",
"error": "Unsupported codemeta version",
}, 400
try:
with open("./codemeta-schema.json", "r", encoding="utf-8") as f:
schema = json.load(f)
jsonschema.validate(file_content, schema)

return {
"message": "valid",
"version": codemeta_version,
}, 200
except jsonschema.exceptions.ValidationError as e:
return {
"message": "invalid",
"error": str(e.message + " at " + str(e.validator_value)),
"version": codemeta_version,
}, 200
except Exception as e:
return {
"message": "Validation Error",
"error": f"Unexpected error: {str(e)}",
}, 400
if codemeta_version == "2.0":
try:
with open("./codemeta-schema2.0.json", "r", encoding="utf-8") as f:
schema = json.load(f)
jsonschema.validate(file_content, schema)

return {
"message": "valid",
"version": codemeta_version,
}, 200
except jsonschema.exceptions.ValidationError as e:
return {
"message": "invalid",
"error": str(e.message + " at " + str(e.validator_value)),
"version": codemeta_version,
}, 200
except Exception as e:
return {
"message": "Validation Error",
"error": f"Unexpected error: {str(e)}",
}, 400
elif codemeta_version == "3.0":
try:
with open("./codemeta-schema.json", "r", encoding="utf-8") as f:
schema = json.load(f)
jsonschema.validate(file_content, schema)

return {
"message": "valid",
"version": codemeta_version,
}, 200
except jsonschema.exceptions.ValidationError as e:
return {
"message": "invalid",
"error": str(e.message + " at " + str(e.validator_value)),
"version": codemeta_version,
}, 200
except Exception as e:
return {
"message": "Validation Error",
"error": f"Unexpected error: {str(e)}",
}, 400

0 comments on commit f2e6db7

Please sign in to comment.