Skip to content

Add validation for Query Name #947

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

Merged
merged 3 commits into from
Sep 14, 2017
Merged

Conversation

ashkan18
Copy link
Contributor

Problem

We should validate name of Query. Having space and other invalid characters causes issues when using schema with GraphiQL. Doing this we'll also follow GraphQL reference implementation here.

Solution

Added new GraphQL::NameVlidator class which can validate! a name. Updated ObjectType to override name= method which first validates using NameValidator and then sets @name.

Switched EnumType to also use the same validator, if you think it's ok for EnumType to raise newly added GraphQL::InvalidNameError then we can switch it to use NameValidator.validate! instead of NameValidator.valid?.

end

def self.valid?(name)
name =~ /^[_a-zA-Z][_a-zA-Z0-9]*$/
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can make this a const and then use it in the error message.

irb(main):004:0> regexp = /^[_a-zA-Z][_a-zA-Z0-9]*$/
=> /^[_a-zA-Z][_a-zA-Z0-9]*$/
irb(main):005:0> "Names must match #{regexp.inspect} but 'Root Query' does not"
=> "Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but 'Root Query' does not"

# Force evaluation
InvalidNameObject.name
}
assert_equal("Names must match (?-mix:^[_a-zA-Z][_a-zA-Z0-9]*$) but 'Three Word Query' does not", exception.message)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you call #inspect on the regexp and interpolate that instead then you won’t get the (?-mix:…) part.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I thought maybe its useful to have it there, but i agree it's cleaner with inspect! thanks.

@rmosolgo
Copy link
Owner

👍 This looks great, thanks for pushing a fix! I totally agree that we should enforce spec-compliant behavior.

Here are my thoughts:

  • I agree that #inspect is a more user-friendly output for the error message.
  • What do you think about replacing EnumNameInvalidError with your new InvalidNameError? I think it makes sense to use the same error here and it's ok with me to make this "breaking" change. Also, you could refactor the error message by putting inside InvalidNameError#initialize. What do you think?

@ashkan18
Copy link
Contributor Author

thanks @rmosolgo! both of your points make total sense, I've already done the inspect change in last commit.

Going to replace EnumNameInvalidError to use new generic InvalidNameError, wasn't sure if we should do it since it was breaking but it would be a nice cleanup 👍

@rmosolgo rmosolgo added this to the 1.7.0 milestone Sep 14, 2017
@rmosolgo
Copy link
Owner

Very nice, thanks again for making it happen!

@rmosolgo rmosolgo merged commit dd22339 into rmosolgo:master Sep 14, 2017
@ashkan18
Copy link
Contributor Author

Thank you @rmosolgo and @alloy for taking time and reviewing it 👍

@ashkan18 ashkan18 deleted the validate-query-name branch September 14, 2017 21:47
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.

3 participants