ProductPromotion
Logo

Go.Lang

made by https://0x3d.site

GitHub - leebenson/conform: Trims, sanitizes & scrubs data based on struct tags (go, golang)
Trims, sanitizes & scrubs data based on struct tags (go, golang) - leebenson/conform
Visit Site

GitHub - leebenson/conform: Trims, sanitizes & scrubs data based on struct tags (go, golang)

GitHub - leebenson/conform: Trims, sanitizes & scrubs data based on struct tags (go, golang)

Conform- keep user input in check (go, golang)

Trim, sanitize, and modify struct string fields in place, based on tags.

Now also works with embedded structs

Turns this...

type Person struct {
	FirstName string `conform:"name"`
	LastName  string `conform:"ucfirst,trim"`
	Email     string `conform:"email"`
	CamelCase string `conform:"camel"`
	UserName  string `conform:"snake"`
	Slug      string `conform:"slug"`
	Blurb     string `conform:"title"`
	Left      string `conform:"ltrim"`
	Right     string `conform:"rtrim"`

 	Skills   []string          `conform:"upper"`
	Examples map[string]string `conform:"!html"`
}

p1 := Person{
	" LEE ",
	"     Benson",
	"   [email protected]  ",
	"I love new york city",
	"lee benson",
	"LeeBensonWasHere",
	"this is a little bit about me...",
	"    Left trim   ",
	"    Right trim  ",
	[]string{"HtmL", "Yaml"},
	map[string]string{"<best>":"<body><p>I know this & that.</p></body>"},
}

Into this...

p2 := p1 // <-- copy the Person struct into a new one, to see the difference
conform.Strings(&p2) // <-- this does the work

/*
	p1 (left) vs. p2 (right)

	FirstName: ' LEE ' -> 'Lee'
	LastName: '     Benson' -> 'Benson'
	Email: '   [email protected]  ' -> '[email protected]'
	CamelCase: 'I love new york city' -> 'ILoveNewYorkCity'
	UserName: 'lee benson' -> 'lee_benson'
	Slug: 'LeeBensonWasHere' -> 'lee-benson-was-here'
	Blurb: 'this is a little bit about me...' -> 'This Is A Little Bit About Me...'
	Left: '    Left trim   ' -> 'Left trim   '
	Right: '    Right trim  ' -> '    Right trim',
	Skills: { 'HtmL', 'Yaml' } -> { 'HTML', 'YAML' },
	Examples: { '<best>': '<body><p>I know this & that.</p></body>' } -> { '<best>': '&lt;body&gt;&lt;p&gt;I know this &amp; that.&lt;/p&gt;&lt;/body&gt;' }
*/

Note: No map keys are changed.

Why?

Conform helps you fix and format user strings quickly, without writing functions.

If you do form processing with Gorilla Schema or similar, you probably shuttle user data into structs using tags. Adding a conform tag to your string field gives you "first pass" clean up against user input.

Use it for names, e-mail addresses, URL slugs, or any other form field where formatting matters.

Conform doesn't attempt any kind of validation on your fields. Check out govalidator for a slew of common validation funcs, or validator which is an uber-flexible Swiss Army knife for validating pretty much any kind of data you can imagine. Both have struct tag syntax and can be used with conform.

How to use

Grab the package from the command line with:

go get github.com/leebenson/conform

And import in the usual way in your Go app:

import "github.com/leebenson/conform"

Add a conform tag to your structs, for all of the string fields that you want Conform to transform. Add the name of the transform (known as the "tag") in double quotes, and separate multiple tags with commas. Example: conform:"trim,lowercase"

To format in place, pass your struct pointer to conform.Strings.

Note: your struct will be edited in place. This will OVERWRITE any data that is already stored in your string fields.

Here's an example that formats e-mail addresses:

package main

import (
		"fmt"
		"github.com/leebenson/conform"
)

type UserForm struct {
	Email string `conform:"email"`
}

func main() {
	input := UserForm{
		Email: "   [email protected]  ",
	}
	conform.Strings(&input) // <-- pass in a pointer to your struct
	fmt.Println(input.Email) // prints "[email protected]"
}

Using with Gorilla Schema

Just add a conform tag along with your Gorilla schema tags:

// ...

import (
	"net/http"

	"github.com/gorilla/schema"
	"github.com/leebenson/conform"
)

// the struct that will be filled from the post request...
type newUserForm struct {
	FirstName string    `schema:"firstName" conform:"name"`
	Email     string    `schema:"emailAddress" conform:"email"`
	Password  string    `schema:"password"`    // <-- no tag? no change
	Dob       time.Time `schema:"dateOfBirth"` // <-- non-strings ignored by conform
}

// ProcessNewUser attempts to register a new user
func ProcessNewUser(r *http.Request) error {
	form := new(newUserForm)
	schema.NewDecoder().Decode(form, r.PostForm) // <-- Gorilla Schema
	conform.Strings(form)                       // <-- Conform.  Pass in the same pointer that Schema used
	// ...
}

// HTTP handlers, etc...

Godoc

See the public API / exported methods on Godoc.

Tags

You can use multiple tags in the format of conform:"tag1,tag2"

trim


Trims leading and trailing spaces. Example: " string " -> "string"

ltrim


Trims leading spaces only. Example: " string " -> "string "

rtrim


Trims trailing spaces only. Example: " string " -> " string"

lower


Converts string to lowercase. Example: "STRING" -> "string"

upper


Converts string to uppercase. Example: "string" -> "STRING"

title


Converts string to Title Case, e.g. "this is a sentence" -> "This Is A Sentence"

camel


Converts to camel case via stringUp, Example provided by library: this is it => thisIsIt, this\_is\_it => thisIsIt, this-is-it => thisIsIt

snake


Converts to snake_case. Example: "CamelCase" -> "camel_case", "regular string" -> "regular_string" Special thanks to snaker for inspiration (credited in license)

slug


Turns strings into slugs. Example: "CamelCase" -> "camel-case", "blog title here" -> "blog-title-here"

ucfirst


Uppercases first character. Example: "all lower" -> "All lower"

name


Trims, strips numbers and special characters (except dashes and spaces separating names), converts multiple spaces and dashes to single characters, title cases multiple names. Example: "3493€848Jo-s$%£@Ann " -> "Jo-Ann", " ~~ The Dude ~~" -> "The Dude", "**susan**" -> "Susan", " hugh fearnley-whittingstall" -> "Hugh Fearnley-Whittingstall"

email


Trims and lowercases the domain portion of the string. Example: "[email protected] " -> "[email protected]"

num


Removes all non-numeric characters. Example: "the price is €30,38" -> "3038"

Note: The struct field will remain a string. No type conversion takes place.

!num


Removes all numbers. Example "39472349D34a34v69e8932747" -> "Dave"

alpha


Removes non-alpha unicode characters. Example: "!@£$%^&'()Hello 1234567890 World+[];\" -> "HelloWorld"

!alpha


Removes alpha unicode characters. Example: "Everything's here but the letters!" -> "' !"

!html


Escapes HTML so that it is safe for display. Characters are substituted by their respective HTML codes. Internally uses template.HTMLEscapeString. Example: "' " & < > \000" -> "&#39; &#34; &amp; &lt; &gt; \uFFFD"

!js


Escapes JavaScript. Internally uses template.JSEscapeString. Example: "\ ' " < > & =" -> "\\ \' \u003C \u003E \u0026 \u003D"

LICENSE

MIT

Articles
to learn more about the golang concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about GoLang.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory