{"id":20214193,"url":"https://github.com/masterminds/formenc","last_synced_at":"2025-04-10T14:08:11.060Z","repository":{"id":57517425,"uuid":"74097248","full_name":"Masterminds/formenc","owner":"Masterminds","description":"Decode form values using a Go unmarshal","archived":false,"fork":false,"pushed_at":"2016-12-08T15:28:42.000Z","size":15,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-03-24T12:48:02.599Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Masterminds.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-11-18T05:10:29.000Z","updated_at":"2022-02-18T20:43:37.000Z","dependencies_parsed_at":"2022-09-15T21:52:26.334Z","dependency_job_id":null,"html_url":"https://github.com/Masterminds/formenc","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Masterminds%2Fformenc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Masterminds%2Fformenc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Masterminds%2Fformenc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Masterminds%2Fformenc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Masterminds","download_url":"https://codeload.github.com/Masterminds/formenc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248232168,"owners_count":21069475,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-14T06:14:30.973Z","updated_at":"2025-04-10T14:08:11.016Z","avatar_url":"https://github.com/Masterminds.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# formenc: Easily unmarshal forms\n\nThis library provides unmarshaling support for HTTP form\ndata. It is loosely modeled after the Go encoders.\n\n**This is pre-release software. Not all of its features are currently\nimplemented.**\n\nRight now, this library can unmarshal forms into flat structs.\nAdditionally, it supports `[]string` as a value on a struct. More will\nbe added as we get time. If you'd like to quickly support another type,\nuse the `FormSetFIELD` pattern described below.\n\n## Usage\n\nThis library provides unmarshal functions for form data.\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"net/url\"\n\n\t\"github.com/Masterminds/formenc/encoding/form\"\n)\n\n// This is a struct set up for form unmarshaling.\ntype Person struct {\n\t// The annotation indicates how the HTTP values are inserted into the struct.\n\tFirstName string `form:\"first_name\"`\n\tLastName  string `form:\"last_name\"`\n\tAge       int    `form:\"age\"`\n}\n\nfunc main() {\n\t// Usually, you get values from an `*http.Request`.\n\tv := url.Values{}\n\tfor key, val := range map[string]string{\n\t\t\"first_name\": \"Batty\",\n\t\t\"last_name\":  \"Penderwick\",\n\t\t\"age\":        \"11\",\n\t} {\n\t\tv.Set(key, val)\n\t}\n\n\t// We want to unmarshal the form data into struct.\n\tperson := \u0026Person{}\n\tif err := form.Unmarshal(v, person); err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Printf(\"%s %s is %d years old\\n\", person.FirstName, person.LastName, person.Age)\n}\n```\n\n\n## Struct Tags for 'encoding/form'\n\nThe format of an `encoding/form` struct tag is:\n\n```\nform=\"NAME,omitempty,prefix=PREFIX,suffix=SUFFIX\"\n```\n\n- `NAME` may be:\n  - `-`: Field is skipped\n  - `+`: Field represents a group (currently unused)\n  - A name, which is the name of the form field (`name=` or, in rare\n    cases, `id=`)\n- `omitempty` causes the field to be left uninitialized if no value is\n  present.\n- `prefix` allows a group prefix to be specified (currently unused)\n- `suffix` allows a group suffix to be specified (currently unused)\n\n## Validator and Setter Functions\n\nDuring the decoding phase, a struct has two opportunities to interject\nfunctionality into the decoding process:\n\n- Validation: Functions following a specific naming convention can\n  _validate_ submitted values before they are inserted into the struct.\n  This provides an oppotunity to validate data and return a complete\n  error message with all validation errors.\n- Setting: Functions following the setter naming convention can _set_\n  the value on a field. If an error occurs while setting, the decoding\n  process is stopped and immediately returned.\n\n### Validator\n\nTo declare a validator as a method on a struct, use a method with the\nfollowing signature:\n\n```go\n// Validator indicates whether a form field is valid.\ntype Validator func(value []string) *ValidationError\n```\n\nIt must match the following naming convention: `FormValidateNAME`.\n\nAbove, `FIELD` is the name of the field on the struct, and `values` is\nthe array of values received in the `url.Values` object.\n\nA validation error should be returned if the `values` is not well\nformed.\n\n_A validator should validate `values`, but not manipulate or store\n`values`._\n\n### Setter\n\nTo declare a form setter as a struct method, use this signature:\n\n```go\n\n// Setter decodes a value for a field.\ntype Setter func(value []string) error\n```\n\nIt must match the following naming convention: `FormSetNAME`.\n\nA setter _is responsible for setting the `value` on the struct_. If this\nfunction returns a non-nil error, the form processing will immediately\nreturn in a failed state.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmasterminds%2Fformenc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmasterminds%2Fformenc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmasterminds%2Fformenc/lists"}