timsolov / rest-query-parser Goto Github PK
View Code? Open in Web Editor NEWQuery Parser for REST
License: Apache License 2.0
Query Parser for REST
License: Apache License 2.0
In Postgres substitutions params look like $1. In this library only "?"
All cases in single test case set
func Test_RemoveOrEntries(t *testing.T) {
type testCase struct {
name string
urlQuery string
filterToRemove string
wantWhere string
}
tests := []testCase{
{
name: "should fix OR statements after removing EndOR filter with 2 items",
urlQuery: "?test1[eq]=test10|test2[eq]=test10",
filterToRemove: "test2",
wantWhere: "test1 = ?",
},
{
name: "should fix OR statements after removing StartOR filter with 2 items",
urlQuery: "?test1[eq]=test10|test2[eq]=test10",
filterToRemove: "test1",
wantWhere: "test2 = ?",
},
{
name: "should fix OR statements after removing StartOR filter with 3 items",
urlQuery: "?test1[eq]=test10|test2[eq]=test10|test3[eq]=test10",
filterToRemove: "test1",
wantWhere: "(test2 = ? OR test3 = ?)",
},
{
name: "should fix OR statements after removing EndOR filter with 3 items",
urlQuery: "?test1[eq]=test10|test2[eq]=test10|test3[eq]=test10",
filterToRemove: "test3",
wantWhere: "(test1 = ? OR test2 = ?)",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Arrange
URL, _ := url.Parse(tt.urlQuery)
q := rqp.NewQV(nil, rqp.Validations{
"test1": nil,
"test2": nil,
"test3": nil,
})
_ = q.SetUrlQuery(URL.Query()).Parse()
// Act
_ = q.RemoveFilter(tt.filterToRemove)
// Assert
assert.Equal(t, tt.wantWhere, q.WHERE())
})
}
}
Hello! NIce library, very comprehensive functions. I've tried but i got an error from example in Readme.
q2, _ := rqp.NewParse(r.URL.Query(), rqp.Validations{
"limit:required": rqp.MinMax(10, 100), // limit must present in the Query part and must be between 10 and 100 (default: Min(1))
"sort": rqp.In("id", "name"), // sort could be or not in the query but if it is present it must be equal to "in" or "name"
"s": rqp.In("one", "two"), // filter: s - string and equal
"id:int": nil, // filter: id is integer without additional validation
"i:int": func(value interface{}) error { // filter: custom func for validating
if value.(int) > 1 && value.(int) < 10 {
return nil
}
return errors.New("i: must be greater then 1 and lower then 10")
},
"email": nil,
"name": nil,
})
URL looks like: ?sort=+name,-id&limit=10&id=1&i[eq]=5&s[eq]=one&email[like]=*tim*|name[like]=*tim*
Error: s[eq]: filter not found
If i remove s[eq]
parameter from url query, then i got an error: sort: validation not found
If i remove sort
parameter from url query, then i got an error: id: filter not found
Can you tell what i'm doing wrong?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.