Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constraints for validation #868

Closed
patrick-fitzgerald opened this issue Sep 18, 2019 · 2 comments
Closed

Constraints for validation #868

patrick-fitzgerald opened this issue Sep 18, 2019 · 2 comments

Comments

@patrick-fitzgerald
Copy link

Hi, Is it currently possible to use constraints for validation, or does it require a plugin? This would be a nice feature to have. e.g

type SignUpInput {
  email: String! @constraint(format: "email", maxLength: 255)
  password: String! @constraint(maxLength: 255)
  firstName: String! @constraint(pattern: "^[0-9a-zA-Z]*$", maxLength: 255)
  lastName: String! @constraint(pattern: "^[0-9a-zA-Z]*$", maxLength: 255)
}

https://blog.apollographql.com/graphql-validation-using-directives-4908fd5c1055

@vektah
Copy link
Collaborator

vektah commented Sep 19, 2019

Will be addressed via plugins - #228

@vektah vektah closed this as completed Sep 19, 2019
@ravilution
Copy link

ravilution commented Sep 21, 2019

Hi @vektah

I was able to achieve the constraint directive with the code below. However, I also want to sanitize my input values. How can I change the obj value that goes to the resolver inside the directive? For example, how can I change the input that comes into lowercase?

func ConstraintDirective(ctx context.Context, obj interface{}, next graphql.Resolver, pattern string, max int, min int, isLower bool) (res interface{}, err error) {
	v := obj.(string)
	r := regexp.MustCompile(pattern)

	if !r.MatchString(v) || len(v) < min || len(v) > max {
		return nil, gqlerror.Errorf("format")
	}

	return next(ctx)
}

Also is there a way to get the graphql input attribute name inside the directive?

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

No branches or pull requests

3 participants