Can i create or use (from external crate validator) custom validation for fields in input objects #1296
-
For example: #[derive(juniper::GraphQLInputObject, Clone)]
pub struct UserInput {
pub id: i32,
#[validate(email)]
pub email: String,
#[validate(length(min = 5, max = 150), custom(function = "validate_username"))]
pub username: String,
pub password: String,
} Because, for me, is not comfortable to make my own implementation/macro manually |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
@WrldEngine well, you may use the However, the idiomatic way to deal with this kind of stuff is to "parse, don't validate". For that, we have scalars in GraphQL. Instead of just using For |
Beta Was this translation helpful? Give feedback.
@WrldEngine well, you may use the
validator
crate to do exactly what you've described, and then, whenever you receive such an input object in your GraphQL resolvers, just call the.validate()
and return the errors, if any.However, the idiomatic way to deal with this kind of stuff is to "parse, don't validate". For that, we have scalars in GraphQL. Instead of just using
String
for youremail
,username
andpassword
, it's better to introduce newtypesEmail
,Username
andPassword
, implementingGraphQLScalar
and, of course, containing already valid values only after parsing from an input.For
password
particularly, you especially need a customGraphQLScalar
which internally utilizes thesecrecy
…