Skip to content

feat: validate survey response answers against JSON Schema#577

Open
Joe-Heffer-Shef wants to merge 11 commits into
mainfrom
feat/survey-validate
Open

feat: validate survey response answers against JSON Schema#577
Joe-Heffer-Shef wants to merge 11 commits into
mainfrom
feat/survey-validate

Conversation

@Joe-Heffer-Shef

@Joe-Heffer-Shef Joe-Heffer-Shef commented Apr 21, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Add survey/schema.py with field_schema() helpers that translate survey field configs (likert, radio, select, checkbox, text/textarea) into JSON Schema definitions
  • Add Survey.response_schema property that builds a full JSON Schema for validating the nested answers array structure
  • Validate SurveyResponse.answers against the schema in SurveyResponse.clean(), raising ValidationError on structural mismatches or invalid option values
  • Add jsonschema==4.* to requirements.txt
  • Add tests covering schema generation, valid responses, wrong section/field counts, extra sections, invalid likert values, and empty required checkboxes

Usage

$ python manage.py validate_responses
Validated 11 responses — 0 error(s)
$ 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)

Screenshots

image

Test plan

  • python manage.py test survey/tests/test_schema.py --parallel=auto --failfast — all schema tests pass
  • make test — full test suite passes
  • make check — Django checks and migration verification pass
  • Test in dev environment

🤖 Generated with Claude Code

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>
@Joe-Heffer-Shef Joe-Heffer-Shef linked an issue Apr 21, 2026 that may be closed by this pull request
Joe-Heffer-Shef and others added 3 commits April 21, 2026 09:20
…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>
@Joe-Heffer-Shef Joe-Heffer-Shef self-assigned this Apr 21, 2026
…to submit

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@Joe-Heffer-Shef

This comment was marked as outdated.

@Joe-Heffer-Shef

This comment was marked as resolved.

@Joe-Heffer-Shef

Copy link
Copy Markdown
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)

@Joe-Heffer-Shef Joe-Heffer-Shef added enhancement New feature or request python Pull requests that update python code labels Apr 22, 2026
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request python Pull requests that update python code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Validate JSON data using a schema

1 participant