Skip to content

Commit

Permalink
feat: validate limit and offset value
Browse files Browse the repository at this point in the history
  • Loading branch information
tauslim committed Feb 26, 2024
1 parent 501ab47 commit 7784096
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io"
"strconv"
"strings"
)

Expand Down Expand Up @@ -60,7 +61,7 @@ func (tb TokenBloc) String() (s string) {
return
}

func (r *RqlRootNode) ParseSpecialOps() {
func (r *RqlRootNode) parseSpecialOps() {
if parseLimit(r.Node, r) || parseSort(r.Node, r) || parseOffset(r.Node, r) || parseFields(r.Node, r) {
r.Node = nil
} else if r.Node != nil {
Expand All @@ -83,6 +84,22 @@ func (r *RqlRootNode) ParseSpecialOps() {
}
}

func (r *RqlRootNode) validateSpecialOps() error {
if r.limit != "" {
_, err := strconv.Atoi(r.limit)
if err != nil {
return fmt.Errorf("invalid format for limit: %s", err)
}
}
if r.offset != "" {
_, err := strconv.Atoi(r.offset)
if err != nil {
return fmt.Errorf("invalid format for offset: %s", err)
}
}
return nil
}

func parseLimit(n *RqlNode, root *RqlRootNode) (isLimitOp bool) {
if n == nil {
return false
Expand Down Expand Up @@ -161,7 +178,11 @@ func (p *Parser) Parse(r io.Reader) (root *RqlRootNode, err error) {
if err != nil {
return nil, err
}
root.ParseSpecialOps()
root.parseSpecialOps()
err = root.validateSpecialOps()
if err != nil {
return nil, err
}
return
}

Expand Down

0 comments on commit 7784096

Please sign in to comment.