Git Product home page Git Product logo

conform's Introduction

Lee Benson's GitHub stats

conform's People

Contributors

andrewzuk avatar gislik avatar jamiri avatar jordanlovato avatar leebenson avatar maclav3 avatar mh-cbon avatar wilcosheh avatar zenpk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

conform's Issues

Custom sanitizers

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.

Email transform is wrong

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.

How can I "conform" URL/URI?

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!

Panic when a struct (Without any conform struct tag) has a []uint

Hello, I keep receiving a panic when a struct has a []uint.

Screenshot 2024-05-27 at 4 39 14 PM

This causes a panic.

Screenshot 2024-05-27 at 4 40 25 PM

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.

support for string array

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 ?

embedded struct pointer panic

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!

panic when input struct have a member type of []int

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:
image

Characters and numbers only?

Sometimes we need to filter only characters and numbers, example:

A1B2C3D4E5F6

Is it possible today without custom sanitizer?

Release

Any chance for a new release? glide is still fetching v1.1.0

Thank you

Problem with substructs and slice of substruct

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"`
}

strings.Conform panic when structure has []byte field

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())
	    }
	}
    }

Slice of int64 errors

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

Why not initialization?

@gislik in #17 wrote:

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...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.