Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: ♻️ switch between schemas for codemeta.json version #126

Merged
merged 11 commits into from
Jan 4, 2025
11 changes: 4 additions & 7 deletions ui/pages/dashboard/[owner]/[repo]/release/zenodo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -1226,16 +1226,13 @@ onBeforeUnmount(() => {
</n-button>
</NuxtLink>

<n-button
<n-button
v-if="zenodoPublishStatus === 'published' || zenodoPublishStatus === 'error'"
type="success"
:disabled="
zenodoPublishStatus !== 'published' &&
zenodoPublishStatus !== 'error'
"
@click="navigateToDashboard"
>
>
Okay
</n-button>
</n-button>
</n-flex>
</template>
</n-modal>
Expand Down
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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Consider extracting the validation logic into a separate function to avoid code duplication

The validation logic for both version 2.0 and 3.0 is identical except for the schema file name. Consider creating a helper function that takes the schema file name as a parameter to reduce duplication and improve maintainability.

Suggested implementation:

        def validate_schema(schema_filename: str, version: str) -> tuple[dict, int]:
            """Validate file content against the specified schema file.

            Args:
                schema_filename: Path to the schema file
                version: Version string to include in the response

            Returns:
                Tuple of (response dict, status code)
            """
            try:
                with open(schema_filename, "r", encoding="utf-8") as f:
                    schema = json.load(f)
                    jsonschema.validate(file_content, schema)

                return {
                    "message": "valid", 
                    "version": version,
                }, 200
            except jsonschema.exceptions.ValidationError as e:
                raise e

        if codemeta_version == "2.0":
            return validate_schema("./codemeta-schema2.0.json", codemeta_version)

The developer will need to:

  1. Update the code handling version 3.0 to use the new validate_schema() function in the same way
  2. Ensure the error handling for ValidationError is properly implemented in the calling code since we're re-raising the exception

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
Comment on lines +240 to +242
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): Consider using a 500 status code for unexpected errors instead of 400

400 Bad Request implies client error, but unexpected exceptions might be server-side issues. Using 500 Internal Server Error would be more appropriate for unexpected exceptions.

Suggested change
"message": "Validation Error",
"error": f"Unexpected error: {str(e)}",
}, 400
"message": "Internal Server Error",
"error": f"Unexpected error: {str(e)}",
}, 500

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
Loading