jazzband / jsonmodels

jsonmodels is library to make it easier for you to deal with structures that are converted to, or read from JSON.
http://jsonmodels.readthedocs.org/en/latest/
BSD 3-Clause "New" or "Revised" License
334 stars 51 forks source link

Enum field example #131

Closed mikepc closed 4 years ago

mikepc commented 4 years ago

Hey there! Does anyone have an example of using an enum for a field?

I see that there was validation added for enums, but how do I go about defining the field on the model?

mikepc commented 4 years ago

    scope = fields.StringField(required=True,
            validators=[
                    validators.Enum([s.value for s in Scope ])
                ])

When I assign the value of scope = Scope.ALL.value (this == 'all') the validation is failing with:

"Value 'all' is not a valid choice."

mikepc commented 4 years ago

    scope = fields.StringField(required=True,
            validators=[
                    validators.Enum(*[s.value for s in Scope ])
                ])

Fixed by de-referencing the array into an arg list.