leebenson / conform Goto Github PK
View Code? Open in Web Editor NEWTrims, sanitizes & scrubs data based on struct tags (go, golang)
License: MIT License
Trims, sanitizes & scrubs data based on struct tags (go, golang)
License: MIT License
Have you any intention of providing the user to register a custom sanitizers with a tag name?
The sanitizers are simple functions with the signature:
func(string) string
Users could instantiate an object of Conform, with setters for an object which could be referred to in transformString.
If this does not go against the ethos of conform, I might be able to send a PR.
The conformatiy for email is wrong as just does an lower(). Because the email addresses [email protected], [email protected], and [email protected] are three different email address according to the RFC and as implemented by many email servers. While case in the domain part does not matter, case in the name part does matter. Also spaces and all weired stuff is allowed if it is escaped or quoted.
Hi! I'm a newbie using your helper and I didn't see any URI/URL sanitizer/normalizer in the package. So, how can I do to check if the URI/URL is correct?
Thanks!
Hello, I keep receiving a panic when a struct has a []uint.
This causes a panic.
Reason why a struct without any conform struct tag is passed to conform.Strings:
That is because I use a general decoder to decode incoming json payloads, and depending on the struct payload is decoded to, some might or might not have conform tag. But this should not cause the panic I encounter.
Thanks.
When using alpha
tag, it removes -
from the string. Is there any way to allow a -
within alpha
sanitization. And the -
can not be at the start or the end of the string.
I thing this can be done with a new tag - username
Hi There,
I've noticed that when conforming names with apostrophes, not only will the apostrophe be stripped, but the rest of the name.
Expected:
D'Addario
Got:
D
Hi,
Look likes conform won t apply on array of strings, unless i did wrong.
type NewInput struct {
Tags []string `form:"tags[]" validate:"dive,min=1" conform:"trim"`
}
func (e EndPoints) HandleForm(c *gin.Context) {
var input NewInput
c.Bind(&input)
conform.Strings(&input)
fmt.Printf("%q\n", input)
fmt.Printf("%q\n", input.Tags)
}
curl -D - -d "tags[]=1&tags[]= " http://localhost:8080/form
yields
{["1" " "]}
["1" " "]
Anyways to make that happen ?
I always get a panic when my struct has the similar structure as below, here's a minimal example
package main
import (
"github.com/leebenson/conform"
"log"
)
type Child struct {
ChildName string `conform:"trim"`
Numbers []uint64
}
type Parent struct {
Child *Child
}
func main() {
child := Child{
ChildName: " child ",
Numbers: []uint64{1, 2, 3},
}
parent := Parent{
Child: &child,
}
if err := conform.Strings(&parent); err != nil {
log.Println(err)
} else {
log.Println(*parent.Child)
}
}
output:
panic: reflect.Value.Convert: value of type string cannot be converted to type uint64
possible fix
conform.go:243
if elType.ConvertibleTo(reflect.TypeOf(str)) || elType.ConvertibleTo(reflect.TypeOf(&str)) {
change to
if elType == reflect.TypeOf(str) || elType == reflect.TypeOf(&str) {
output:
2022/12/06 22:11:56 {child [1 2 3]}
I might try to make a pr to fix it later, any help or explanation is appreciated, thanks in advance!
hi, i got a problem when pass a struct which have an slice of int member, here is the test code below:
`package main
import (
"github.com/leebenson/conform"
)
type UserForm struct {
Age []int
}
func main() {
input := UserForm{
Age: []int{1},
}
conform.Strings(&input) // panic
}
`
and below is panic info:
Sometimes we need to filter only characters and numbers, example:
A1B2C3D4E5F6
Is it possible today without custom sanitizer?
Any chance for a new release? glide is still fetching v1.1.0
Thank you
Hello guys,
Im trying to implement your code but I have some issues with it.
In the case I have substruct or slice of substruct, the lib is not working.
type Request struct {
SeveralSubRequests []*struct {
Foo string `json:"foo" conform:"trim"`
Bar string `json:"bar" conform:"trim"`
} `json:"several_sub_requests"`
}
type Request struct {
SubRequest *struct {
Foo string `json:"foo" conform:"trim"`
Bar string `json:"bar" conform:"trim"`
} `json:"sub_request"`
}
Hello,
I have found the issue in your lib - if structure has []byte or *[]byte field it will crash in string.Conform with the following message:
panic: reflect: NumField of non-struct type
goroutine 1 [running]:
reflect.(*rtype).NumField(0x1143c40, 0xc420016320)
/usr/local/Cellar/go/1.10/libexec/src/reflect/type.go:1019 +0x5e
github.com/leebenson/conform.Strings(0x113f5e0, 0xc420016320, 0x16, 0x113f5e0)
/Users/pvaladzko/dev/go/src/github.com/leebenson/conform/conform.go:185 +0x10c
github.com/leebenson/conform.Strings(0x113bf20, 0xc42007c960, 0x60, 0x113bf20)
/Users/pvaladzko/dev/go/src/github.com/leebenson/conform/conform.go:199 +0x682
It will fail even if []byte field does not have any tags for conform
:
type Out struct {
Bytes []byte `json:"bytes"`
}
The problem in this part of code (conform.go file):
case reflect.Slice:
if el.CanInterface() {
if slice, ok := el.Interface().([]string); ok {
for i, input := range slice {
tags := v.Tag.Get("conform")
slice[i] = transformString(input, tags)
}
} else {
val := reflect.ValueOf(el.Interface())
for i := 0; i < val.Len(); i++ {
// HERE it takes single byte, which fails later with the reported message..
// I guess we can just skip bytes array at the start of this else-block
Strings(val.Index(i).Addr().Interface())
}
}
}
The README stated:
Trims and lowercases the string. Example: "[email protected] " -> "[email protected]"
But it actually only lowercase the domain part.. which means it actually become [email protected] instead of [email protected]
I wonder what is the correct behaviour...
It seems like non-string slices are trying to be conformed. I am on version 1.2.2
type Test struct {
Title string `json:"test" conform:"trim"`
Statuses []int64 `json:"statuses"`
}
I am getting:
reflect.Value.Convert: value of type string cannot be converted to int64
Since no object is instantiated the code is not type-safe, i.e. two thread may try to add sanitizers at the same time resulting in a corrupt state.
Can we instantiate an object?
Can we use it like:
func main() {
sanitizer := conform.New()
conform.AddSanitizer(customOne)
sanitizer.String(...)
}
Why not?
We're using dependency injection everywhere...
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.