-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Conversation
lib/graphql/name_validator.rb
Outdated
end | ||
|
||
def self.valid?(name) | ||
name =~ /^[_a-zA-Z][_a-zA-Z0-9]*$/ |
There was a problem hiding this comment.
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"
spec/graphql/object_type_spec.rb
Outdated
# 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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
👍 This looks great, thanks for pushing a fix! I totally agree that we should enforce spec-compliant behavior. Here are my thoughts:
|
thanks @rmosolgo! both of your points make total sense, I've already done the Going to replace |
Very nice, thanks again for making it happen! |
Problem
We should validate
name
ofQuery
. 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 canvalidate!
a name. UpdatedObjectType
to overridename=
method which first validates usingNameValidator
and then sets@name
.Switched
EnumType
to also use the same validator, if you think it's ok forEnumType
to raise newly addedGraphQL::InvalidNameError
then we can switch it to useNameValidator.validate!
instead ofNameValidator.valid?
.