Repository navigation
Replies: 5 comments 13 replies
|
Example of a problem: @strawberry.mutation
def create_mapping(
self, info: Info, input: CreateMappingInput
) -> strawberry.union(
name="CreateMappingResult", # noqa: F821
types=(
Mapping,
error.IntegrationDoesNotExistError,
error.FieldDoesNotExistError,
error.EntityDoesNotExistError,
error.DestinationFieldAlreadyMappedError,
error.MissingRequiredFieldMappingsError,
error.EntityAlreadyMappedError,
error.FilterValueInvalidForFilterTypeError,
error.FieldTypeIncorrectForFilterTypeError,
error.FilterNotFoundError,
error.FieldMappingDataTypeMismatchError,
),
):
mapping = Mapping.from_input(input=input)
is_valid, reason = mapping.validate()
if not is_valid:
return reason
...
@strawberry.type
class Mapping(Node):
def validate(
self,
) -> Tuple[
bool,
Optional[
Union[
error.FieldDoesNotExistError,
error.EntityDoesNotExistError,
error.MissingRequiredFieldMappingsError,
error.EntityAlreadyMappedError,
error.DestinationFieldAlreadyMappedError,
error.FilterValueInvalidForFilterTypeError,
error.FieldTypeIncorrectForFilterTypeError,
error.FilterNotFoundError,
error.FieldMappingDataTypeMismatchError,
]
],
]:
validators = [
validation.validate_entities,
validation.validate_match_on_fields,
validation.validate_field_mapping,
validation.validate_filters,
]
for validator in validators:
error = validator(mapping=self)
if error:
return False, error
return True, None
def validate_entities(
mapping: "mapping.Mapping",
) -> Union[error.EntityDoesNotExistError, error.EntityAlreadyMappedError]:
validators = [
_validate_source_entity_exists,
_validate_destination_entity_exists,
_validate_destination_entity_is_unique,
]
for validator in validators:
error = validator(mapping=mapping)
if error:
return error
... |
2 replies
|
I've been thinking about how the API would look for this. One possibility: import strawberry
def validate_name(name: str) -> str:
if ' ' not in name:
raise ValueError('must contain a space')
return name.title()
def validate_password(password: str) -> str:
if len(password) < 4:
raise ValueError("Password is too short")
def root_validator(input):
# This is run after all the field validators have been run
if self.password_confirm != self.password:
raise ValueError("passwords do not match")
# Also field values can be modified in this function
@strawberry.input(root_validator=root_validator)
class NewUserInput:
name: str = strawberry.field(validators=[validate_name])
email: str = strawberry.field(validators=[validate_email])
password: str = strawberry.field(validators=[validate_password])
password_confirm: str
@strawberry.mutation
def create_user(info, user_data: NewUserInput) -> Union[User, InputError]:
try:
user_data.validate(info)
except strawberry.ValidationError as error:
return InputError(errors=error.errors)This API is kind of a mix between Pydantic and Django forms. Some notes and open questions:
Thoughts @strawberry-graphql/core ? |
9 replies
|
What is the current status of this topic? |
2 replies
|
It would be wonderful if we could handle first class Pydantic support #2181 |
0 replies
|
Any movement on this by chance? I'm trying to enforce strict validation on a |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Let's collect ideas and requirements around validation in Strawberry
All reactions