Skip to content

feat: activate JSON schema validation for all new survey responses #652

Description

@Joe-Heffer-Shef

Context

PR #577 added the JSON Schema validation infrastructure (survey/schema.py, Survey.response_schema, SurveyResponse.clean()) but intentionally did not wire it into the live submission path. The service layer still uses SurveyResponse.objects.create() which skips full_clean(), so validation is not yet enforced on new responses.

The plan is to run python manage.py validate_responses on production data first to confirm clean baseline, then activate per-submission validation in this follow-up.

Work required

  1. Activate validation on submit: Update survey/services/survey.py to delegate to the model method (which calls full_clean()):

    # survey/services/survey.py
    def accept_response(self, survey: Survey, responseValues):
        survey.accept_response(responseValues)  # delegates to Survey.accept_response() which calls full_clean()
  2. Cache the schema: Change Survey.response_schema from @property to @cached_property so the JSON Schema is built once per instance rather than on every validation call.

    from django.utils.functional import cached_property
    
    @cached_property
    def response_schema(self) -> dict:
        ...
  3. Update validate_responses output: Use ', '.join(exc.messages) instead of exc.message in the management command to handle multi-error ValidationError correctly.

Prerequisites

  • Run python manage.py validate_responses on production and confirm 0 errors
  • If errors are found, investigate and fix the data before merging this change

References

Metadata

Metadata

Labels

enhancementNew feature or requestpythonPull requests that update python code

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions