<commit_before># binding [![wercker status](https://app.wercker.com/status/f00480949f8a4e4130557f802c5b5b6b "wercker status")](https://app.wercker.com/project/bykey/f00480949f8a4e4130557f802c5b5b6b)

Request data binding and validation for Martini.

[![GoDoc](https://godoc.org/github.com/martini-contrib/binding?status.svg)](https://godoc.org/github.com/martini-contrib/binding)

## Features

 - Automatically converts data from a request into a struct ready for your application
 - Supports form, JSON, and multipart form data (including file uploads)
 - Can use interfaces
 - Provides data validation facilities
 	- Enforces required fields
 	- Invoke your own data validator
 - Built-in error handling (or use your own)
 - 99% test coverage


## Usage

#### Getting form data from a request

Suppose you have a contact form on your site where at least name and message are required. We'll need a struct to receive the data:

```go
type ContactForm struct {
	Name    string `form:"name" binding:"required"`
	Email   string `form:"email"`
	Message string `form:"message" binding:"required"`
}
```

Then we simply add our route in Martini:

```go
m.Post("/contact/submit", binding.Bind(ContactForm{}), func(contact ContactForm) string {
	return fmt.Sprintf("Name: %s\nEmail: %s\nMessage: %s",
		contact.Name, contact.Email, contact.Message)
})
```

That's it! The `binding.Bind` function takes care of validating required fields. If there are any errors (like a required field is empty), `binding` will return an error to the client and your app won't even see the request.

(Caveat: Don't try to bind to embedded struct pointers; it won't work. See [issue 30](https://github.com/martini-contrib/binding/issues/30) if you want to help with this.)


#### Getting JSON data from a request

To get data from JSON payloads, simply use the `json:` struct tags instead of `form:`. Pro Tip: Use [JSON-to-Go](http://mholt.github.io/json-to-go/) to correctly convert JSON to a Go type definition. It's useful if you're new to this or the structure is large/complex.

<commit_msg>Added missing newline to example Post route<commit_after># binding [![wercker status](https://app.wercker.com/status/f00480949f8a4e4130557f802c5b5b6b "wercker status")](https://app.wercker.com/project/bykey/<KEY>)

Request data binding and validation for Martini.

[![GoDoc](https://godoc.org/github.com/martini-contrib/binding?status.svg)](https://godoc.org/github.com/martini-contrib/binding)

## Features

 - Automatically converts data from a request into a struct ready for your application
 - Supports form, JSON, and multipart form data (including file uploads)
 - Can use interfaces
 - Provides data validation facilities
 	- Enforces required fields
 	- Invoke your own data validator
 - Built-in error handling (or use your own)
 - 99% test coverage


## Usage

#### Getting form data from a request

Suppose you have a contact form on your site where at least name and message are required. We'll need a struct to receive the data:

```go
type ContactForm struct {
	Name    string `form:"name" binding:"required"`
	Email   string `form:"email"`
	Message string `form:"message" binding:"required"`
}
```

Then we simply add our route in Martini:

```go
m.Post("/contact/submit", binding.Bind(ContactForm{}), func(contact ContactForm) string {
	return fmt.Sprintf("Name: %s\nEmail: %s\nMessage: %s\n",
		contact.Name, contact.Email, contact.Message)
})
```

That's it! The `binding.Bind` function takes care of validating required fields. If there are any errors (like a required field is empty), `binding` will return an error to the client and your app won't even see the request.

(Caveat: Don't try to bind to embedded struct pointers; it won't work. See [issue 30](https://github.com/martini-contrib/binding/issues/30) if you want to help with this.)


#### Getting JSON data from a request

To get data from JSON payloads, simply use the `json:` struct tags instead of `form:`. Pro Tip: Use [JSON-to-Go](http://mholt.github.io/json-to-go/) to correctly convert JSON to a Go type definition. It's useful if you're new to this or the structure is large/complex.


{'fraction_non_alphanumeric': 0.1102001446829033, 'fraction_numerical': 0.019532191945985048, 'mean_word_length': 4.51595744680851, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2509716', 'n_tokens_mistral': 1296, 'n_tokens_neox': 1201, 'n_words': 482}