feat: validate survey response answers against JSON Schema#577
Open
Joe-Heffer-Shef wants to merge 11 commits into
Open
feat: validate survey response answers against JSON Schema#577Joe-Heffer-Shef wants to merge 11 commits into
Joe-Heffer-Shef wants to merge 11 commits into
Conversation
Add jsonschema-based validation to SurveyResponse.clean() that verifies the structure of submitted answers matches the survey configuration, catching wrong section/field counts and invalid option values at save time. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ment command Moves validation logic into Survey.validate(), adds a management command to bulk-validate all existing responses, and fixes exception type in the command. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Validation logic belongs on the model that owns the answers. Also fixes clean() to raise ValidationError (not ValueError) for inactive surveys, makes the test helpers robust against section-order changes, and adds chunk_size to the management command iterator. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…to submit Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as resolved.
This comment was marked as resolved.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Collaborator
Author
|
I tested the management command on dev: gunicorn@sort-web-dev:/opt/sort$ python manage.py validate_responses
Survey 1 / Response 1: 'My custom value' is not one of ['Option 1', 'Option 2', 'Option 3']
Validated 2 responses — 1 error(s) |
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
survey/schema.pywithfield_schema()helpers that translate survey field configs (likert, radio, select, checkbox, text/textarea) into JSON Schema definitionsSurvey.response_schemaproperty that builds a full JSON Schema for validating the nested answers array structureSurveyResponse.answersagainst the schema inSurveyResponse.clean(), raisingValidationErroron structural mismatches or invalid option valuesjsonschema==4.*torequirements.txtUsage
Screenshots
Test plan
python manage.py test survey/tests/test_schema.py --parallel=auto --failfast— all schema tests passmake test— full test suite passesmake check— Django checks and migration verification pass🤖 Generated with Claude Code