Is there any plan for adding validation for mutation Argument? Like wt-form's validation.
@keyeMyria no plans that I'm aware of. What do you think the validation could look like?
Just like WTForm's validation.
I'm not familiar with WTForm so you're going to have to help me out with what you're looking for. Do you have an example api that you think would work?
I would think that would look like the from validators in django. You would specify a validator for the input and its checked once the endpoint is called.
Here is an example of what validation could look like if modelled after REST Serializers (why do i need to inlclude serializers to get validation currently?)
validation logic for rest here:
https://github.com/encode/django-rest-framework/blob/master/rest_framework/serializers.py
class ValidationMutation(graphene.Mutation):
class Arguments:
field1 = graphene.String()
field2 = graphene.String()
def validate_field1(self, value):
if value != 'BAD':
return value
raise graphene.FieldError('field1 is BAD')
def validate_field2(self, value):
if value != 'OOPS':
return value
raise graphene.FieldError('field1 is OOPS')
def validate(self, fields):
field1 = fields.get('field1', None)
field2 = fields.get('field2', None)
if field1 == field2:
raise graphene.FieldError('fields are the same value')
return fields
def mutate(self, field1, field2):
# This should be done with a loop through arguments
self.validate_field1(field1)
self.validate_field2(field2)
self.validate({
'field1':field1,
'field2':field2
})
return ValidationMutation()
This feature would also be useful to us, any news on its status ? Is it accepted as a sensible approach to implement someday ?
@ArnaudPel there are currently no plans to build validation into Graphene but that doesn't mean that people can't experiment with building validation frameworks on top of Graphene. For example this is how graphene-django does it: http://docs.graphene-python.org/projects/django/en/latest/form-mutations/
If anyone thinking of implementing a validation framework on top of Graphene I would look into Cerberus:
https://docs.python-cerberus.org/
It's a great validation library.
There is someone who started working on this kind of library although it doesn't seem very active and it's still WIP -
https://github.com/gillgamesh/graphene-cerberus
Currently, I'm looking of using the Cerberus library as it is with Graphene
Cerberus is very good. Waiting for this feature.
You might be interested in taking a look at a PoC I've been working on: https://github.com/chpmrc/graphene-validator
Most helpful comment
Here is an example of what validation could look like if modelled after REST Serializers (why do i need to inlclude serializers to get validation currently?)
validation logic for rest here:
https://github.com/encode/django-rest-framework/blob/master/rest_framework/serializers.py