From f2e6db78e820d7fdbb316e4940c8a020bc675489 Mon Sep 17 00:00:00 2001 From: slugb0t Date: Fri, 3 Jan 2025 16:01:24 -0800 Subject: [PATCH] refacor: :recycle: switch between the two validation schemas depending on codemeta version --- validator/apis/__init__.py | 61 ++++++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 19 deletions(-) diff --git a/validator/apis/__init__.py b/validator/apis/__init__.py index cc18d36..321ab91 100644 --- a/validator/apis/__init__.py +++ b/validator/apis/__init__.py @@ -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