You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is there a way to validate a search string/query for a specific search scope?
For example if I have a search_scope of the following:
# my_model.rb MyModelsearch_scope:searchdoattributes:value# value is an integer attribute type (not string)options:value,default: trueend
It would be nice to be able to call something like MyModel.validate_search("value:Foo") which would not actually make a query to the database but either raise an error (like theIncompatibleDatatype error) or return a boolean value.
This would allow us to validate/verify queries before being run and handle errors rather than silently failing when calling search("value:Foo") as it does now. The unsafe_search method is nice, but if there was a way to separate validation from querying that would be great.
After digging into the code this may seem fairly trivial to add to the SearchCop module as another ClassMethod. Unfortunately I don't think I'll have the time to make a proper PR this weekend.
# raises an error or returns the querydefvalidate_search_scope(query,scope_name)QueryBuilder.new(self,query,search_scopes[scope_name])queryend
Well, you actually can simply do MyModel.unsafe_search("query").to_sql or MyModel.unsafe_search("query"). As SearchCop returns an AR::Relation the query of course gets lazily evaluated. So i'm not sure adding validate_search_cop! provides much benefit.
Is there a way to validate a search string/query for a specific search scope?
For example if I have a search_scope of the following:
It would be nice to be able to call something like
MyModel.validate_search("value:Foo")
which would not actually make a query to the database but either raise an error (like theIncompatibleDatatype
error) or return a boolean value.This would allow us to validate/verify queries before being run and handle errors rather than silently failing when calling
search("value:Foo")
as it does now. Theunsafe_search
method is nice, but if there was a way to separate validation from querying that would be great.My primary reason for this request is to better handle errors when using custom filters in the jsonapi_resources gem which has a custom
verify
method.The text was updated successfully, but these errors were encountered: