Skip to content

Conversation

@edbo
Copy link
Contributor

@edbo edbo commented Sep 7, 2024

Add a validator that accepts ID or String Input Values that are UUIDs and validates them for correctness and optionally checks the version number is correct.

Example:

#[Object]
impl MutationRoot {
    async fn add_user_to_team(
        &self,
        ctx: &Context<'_>,
        #[graphql(validator(uuid))] team_id: ID,
        #[graphql(validator(uuid = 4))] user_id: ID,
    ) -> ID {
        let pool = ctx.data::<Pool<Postgres>>().unwrap();
        team_repository::add_user_to_team(
            &pool,
            Uuid::parse_str(&team_id).unwrap(),
            Uuid::parse_str(&user_id).unwrap(),
        )
        .await
        .unwrap();
        team_id
    }
}

@sunli829
Copy link
Collaborator

sunli829 commented Sep 8, 2024

thanks 🙂

Is it possible to make the version optional?

#[graphql(validator(uuid))] team_id: ID,
#[graphql(validator(uuid = 4))] user_id: ID,

@edbo
Copy link
Contributor Author

edbo commented Sep 9, 2024

I went with

#[graphql(validator(uuid))] team_id: ID,
#[graphql(validator(uuid_version = 4))] user_id: ID,

I couldn't find a simple way to have a proc arg accept a bool or an int but open to suggestions if there is a way.

@sunli829
Copy link
Collaborator

Do it like this

impl FromMeta for DefaultValue {

* Add a UUID Validator
* Also add AsRef trait to ID to allow using the ID type with the UUID validator.

Add description to docs

Add a line to the docs describing the validator.

Add a variant that accepts any UUID

Change to uuid format with default

Change to uuid function which defaults to allow any valid UUID value.
@edbo
Copy link
Contributor Author

edbo commented Oct 15, 2024

@sunli829 Sorry for the delay and thanks so much for the pointer. Not sure why I didn't try just putting an enum in the option.

I've rebased to master so hopefully this is good to go now.

@sunli829 sunli829 merged commit a244af4 into async-graphql:master Nov 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants