Skip to content

[BUG] choices parameter for TextFields on Spell model have no effect #869

@calumbell

Description

@calumbell

Bug description

I encountered this bug while working with the display of Spells on the FE. I notice that the "casting_time" was returning values outside of the enumerated types associated with this field (the TS types were generated directly from the Swagger spec).

Investigating, I first took a look at Spell model field for casting_time:

    casting_time = models.TextField(
        choices = SPELL_CASTING_TIME_CHOICES,
        help_text = "Casting time key, such as 'action'")

From api_v2/models/enums.py

SPELL_CASTING_TIME_CHOICES = [
    ('reaction',"Reaction"),
    ('bonus-action',"1 Bonus Action"),
    ('action',"1 Action"),
    ('turn',"1 Turn"),
    ('round',"1 Round"),
    ('1minute',"1 Minute"),
    ('5minutes',"5 Minutes"),
    ('10minutes',"10 Minutes"),
    ('1hour',"1 Hour"),
    ('4hours',"4 Hours"),
    ('7hours',"7 Hours"),
    ('8hours',"8 Hours"),
    ('9hours',"9 Hours"),
    ('12hours',"12 Hours"),
    ('24hours',"24 Hours"),
    ('1week',"1 Week")
]

This feels like it ought to handle the validation to stop a value being assigned to casting_time that is outside of the SPELL_CASTING_TIME_CHOICES enum, but it doesn't.

[
  {
    "model": "api_v2.spell",
    "pk": "srd-2024_healing-word",
    "fields": {
      "name": "Healing Word",
      ...
      "casting_time": "bonus_action",
      ....

The value "bonus_action" is not a valid option in the enum, it should be "bonus-action", but Django doesn't seem to flag this as an issue when the DB is built.

Desired Behaviour

That Django validates TextFields with choice properties to ensure that only values specified in the choices enum can be assigned to the field.

Steps to Reproduce

  1. Fork the staging branch of the Open5e API
  2. Change the "casting_time" of any spell to something outside of the SPELL_CASTING_TIME_CHOICES. ie. "TEST".
  3. When we run pipenv run python manage.py quicksetup, no validation error is thrown. DB is built as normal.
  4. Spin up a local Django dev server (pipenv run python manage.py runserver) and visit the endpoint for the spell you just changed. Notice that the "casting_time" has been updated to a value disallowed by the SPELL_CASTING_TIME_CHOICES enum.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions