Skip to content

Add JSON validation for project criteria submission#144

Merged
alexeygrigorev merged 3 commits into
mainfrom
copilot/add-json-validation-criteria
Jan 21, 2026
Merged

Add JSON validation for project criteria submission#144
alexeygrigorev merged 3 commits into
mainfrom
copilot/add-json-validation-criteria

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 21, 2026

This PR adds comprehensive JSON field validation for the ReviewCriteria.options field to prevent 500 server errors when submitting incorrect JSON through the admin panel.

Problem

Users could submit incorrect JSON structure via the admin panel (e.g., a dict with nested "options" key instead of a direct list), which would cause 500 errors when the code tried to access the data expecting a specific structure.

Solution

1. Created Validator Function

File: courses/validators/criteria_validators.py

The validate_review_criteria_options() function validates:

  • Options must be a list (not a dict or other type)
  • List cannot be empty
  • Each item must be a dictionary
  • Each dict must have 'criteria' (string) and 'score' (integer) keys
  • Criteria text cannot be empty

2. Updated Model

File: courses/models/project.py

Added the validator to the ReviewCriteria model's options field.

3. Enhanced Admin Form

File: courses/admin/course.py

Updated CriteriaForm to:

  • Run validation when data is submitted
  • Provide helpful error messages with expected format
  • Prevent saving invalid data

4. Comprehensive Tests

Added 16 tests covering:

  • Valid JSON acceptance
  • Invalid dict instead of list (the reported issue)
  • Empty lists
  • Missing required keys
  • Wrong data types
  • Empty criteria text

Expected Format

[
  {"criteria": "Poor", "score": 0},
  {"criteria": "Good", "score": 1},
  {"criteria": "Excellent", "score": 2}
]

Testing

  • ✅ All 16 new tests pass
  • ✅ No regression in existing tests
  • ✅ Code review completed
  • ✅ Security scan passed (no vulnerabilities)

Users attempting to submit invalid JSON will now see helpful error messages in the admin panel instead of getting 500 errors.

Original prompt

This section details on the original issue you should resolve

<issue_title>Criteria json validation</issue_title>
<issue_description>It's possible to submit an incorrect JSON for the project criteria via the admin panel

I submitted this:

{
  "type": "radio",
  "options": [
    { "score": 0, "criteria": "Project cannot be run with the provided instructions" },
    { "score": 1, "criteria": "Project can be run, but setup or run instructions are incomplete" },
    { "score": 2, "criteria": "Clear instructions exist to set up, run, test, and deploy the system end-to-end" }
  ]
}

And it resulted in 500 Server error. Let's have JSON field validation for this and other JSON fields, so if the submitted data is not correct, admin panel won't let me save it
</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@alexeygrigorev
Copy link
Copy Markdown
Member

@copilot you got stuck. continue

Copilot AI and others added 2 commits January 21, 2026 08:56
Co-authored-by: alexeygrigorev <875246+alexeygrigorev@users.noreply.github.com>
Co-authored-by: alexeygrigorev <875246+alexeygrigorev@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Jan 21, 2026

@copilot you got stuck. continue

Completed the JSON validation implementation!

I added comprehensive validation for the ReviewCriteria.options field at both the model and form levels. The solution:

  1. Created a validator function that ensures options is a list of dicts with 'criteria' (string) and 'score' (integer) keys
  2. Added the validator to the model field
  3. Enhanced the admin form with better error messages
  4. Added 16 tests - all passing ✅

The exact JSON from the issue (dict with nested "options" key) will now be properly rejected with a clear error message instead of causing a 500 error.

Commits: 6c38c15, 70ba379

Copilot AI changed the title [WIP] Add JSON validation for project criteria submission Add JSON validation for project criteria submission Jan 21, 2026
@alexeygrigorev alexeygrigorev marked this pull request as ready for review January 21, 2026 09:30
@alexeygrigorev alexeygrigorev merged commit 91ff448 into main Jan 21, 2026
2 checks passed
@alexeygrigorev alexeygrigorev deleted the copilot/add-json-validation-criteria branch January 21, 2026 09:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Criteria json validation

2 participants