Open la4de opened 3 years ago
I still think that maybe it's possible do add this functionality without first-classing support for validation, through other techniques such as user-defined function decorators (this gets a bit tricky with input
types w/o resolvers, so maybe not?).
However, if we do want to first-class validation, I wonder if we should take a similar approach that Python's @property
decorator does the getter
and setter
functions have the same symbol name:
####### Property
class SomeClass:
@property # implicitly my_field.getter
def my_field() -> bool:
return True
@my_field.setter
def my_field(value: bool):
print(f"Set my_field={value}"
####### Strawberry
@strawberry.type
class User:
@strawberry.field # implicitly name.resolver
def name() -> str:
return "First Last"
@name.validator
def name(value):
if 'bad' in value:
raise ValueError('name contains bad word')
return value.title()
Here, the same function symbol name is used for both the resolver
and validator
functions for the name
field.
I think this might actually just be the standard practice in Python, and not actually a strict requirement with the property system; but it could be something for us to follow precedent
I added 3rd option. I think I personally would like implement both 2nd and 3rd. Any opinions?
Unfortunately, I don't think you can use decorators around assignments/expressions, only function/class definitions.
Unfortunately, I don't think you can use decorators around assignments/expressions, only function/class definitions.
~Decorator can add validator functions to the internal field definition structure. Generic field resolver can then iterate validators and execute them later. See: https://github.com/la4de/strawberry/commit/04b0ca2a0fe3e6b0a2dc65cd835846bc743ade67. I think this should be doable.~
You are right, it is not possible to do that. :)
don't know if this helps, but attrs has a concept of validators: https://www.attrs.org/en/stable/init.html#validators
don't know if this helps, but attrs has a concept of validators: https://www.attrs.org/en/stable/init.html#validators
I like this API. I think it would be a nice feature if Strawberry implemented it as well. (It's option 1 and 2 in the examples)
I implemented prototype code and made draft pull request #809.
It would be very useful to have possibility to define field level validators especially for those fields which do not have resolvers. My proposal is to add validators parameter to field to
strawberry.field
and validator decorator. This idea came to my mind when I was designing API forstrawberry-graphql-django
package (https://github.com/strawberry-graphql/strawberry-graphql-django/issues/10). Soon we realized that this could be core feature ofstrawberry
library.I implemented prototype code and made draft pull request #809.
Validators could be used for following purposes
Option 1
I'd propose following syntax for validators
Option 2
I'd also propose decorator syntax, which is quite similar with existing field resolver syntax. Strawberry field would provide validator decorator which could be then used to tie the field together with validator function.
Option 3 (not doable)
Upvote & Fund