Skip to content
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

Add URL escape normalizer and tests to v2. #142

Merged
merged 1 commit into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions v2/maker.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var makers = map[string]MakeCheckFunc{
nameRequired: makeRequired,
nameTrimSpace: makeTrimSpace,
nameURL: makeURL,
nameURLEscape: makeURLEscape,
}

// makeChecks take a checker config and returns the check functions.
Expand Down
37 changes: 37 additions & 0 deletions v2/url_escape.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2023-2024 Onur Cinar.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// https://github.com/cinar/checker

package v2

import (
"net/url"
"reflect"
)

const (
// nameURLEscape is the name of the URL escape normalizer.
nameURLEscape = "url-escape"
)

// normalizeURLEscape applies URL escaping to special characters.
// Uses net.url.QueryEscape for the actual escape operation.
func normalizeURLEscape(value string) (string, error) {
return url.QueryEscape(value), nil
}

// checkURLEscape checks if the value is a valid URL escape string.
func checkURLEscape(value reflect.Value) (reflect.Value, error) {
escaped, err := normalizeURLEscape(value.Interface().(string))
if err != nil {
return value, err
}
value.SetString(escaped)
return value, nil
}

// makeURLEscape makes a normalizer function for the URL escape normalizer.
func makeURLEscape(_ string) CheckFunc[reflect.Value] {
return checkURLEscape
}
43 changes: 43 additions & 0 deletions v2/url_escape_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) 2023-2024 Onur Cinar.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// https://github.com/cinar/checker

package v2_test

import (
"testing"

v2 "github.com/cinar/checker/v2"
)

func TestNormalizeURLEscapeNonString(t *testing.T) {
defer FailIfNoPanic(t, "expected panic")

type Request struct {
Query int `checkers:"url-escape"`
}

request := &Request{}

v2.CheckStruct(request)
}

func TestNormalizeURLEscape(t *testing.T) {
type Request struct {
Query string `checkers:"url-escape"`
}

request := &Request{
Query: "param1/param2 = 1 + 2 & 3 + 4",
}

_, valid := v2.CheckStruct(request)
if !valid {
t.Fail()
}

if request.Query != "param1%2Fparam2+%3D+1+%2B+2+%26+3+%2B+4" {
t.Fail()
}
}
Loading