{"id":18051790,"url":"https://github.com/joegasewicz/form-validator","last_synced_at":"2025-04-10T18:15:48.502Z","repository":{"id":36994059,"uuid":"499175962","full_name":"joegasewicz/form-validator","owner":"joegasewicz","description":"Validate the incoming request's form values \u0026 cast to valid Go type","archived":false,"fork":false,"pushed_at":"2022-11-01T19:46:36.000Z","size":44,"stargazers_count":11,"open_issues_count":6,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-10T13:34:12.683Z","etag":null,"topics":["form","form-data","form-validation","forms","validate-form"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/joegasewicz.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-06-02T14:48:50.000Z","updated_at":"2025-02-27T09:52:12.000Z","dependencies_parsed_at":"2022-08-18T04:10:29.286Z","dependency_job_id":null,"html_url":"https://github.com/joegasewicz/form-validator","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joegasewicz%2Fform-validator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joegasewicz%2Fform-validator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joegasewicz%2Fform-validator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joegasewicz%2Fform-validator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/joegasewicz","download_url":"https://codeload.github.com/joegasewicz/form-validator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248269658,"owners_count":21075784,"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":["form","form-data","form-validation","forms","validate-form"],"created_at":"2024-10-30T22:56:02.145Z","updated_at":"2025-04-10T18:15:48.484Z","avatar_url":"https://github.com/joegasewicz.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Form Validator\nValidate the incoming request's form values\n\n# Install\n```bash\ngo get github.com/joegasewicz/form-validator\n```\n\n### Setup\n```go\nc := form_validator.Config{\n    MaxMemory: 0,\n    Fields: []form_validator.Field{\n        {\n            Name:     \"email\",\n            Validate: true,\n            Type:     \"string\",\n        },\n        {\n            Name:     \"email\",\n            Validate: true,\n            Type:     \"string\",\n        },\n        {\n            Name:     \"confirm_email\",\n            Validate: true,\n            Type:     \"string\",\n            Matches:  \"email\", // checks that email \u0026 confirm_email match\n        },\n    },\n}\n\nif ok := form_validator.ValidateForm(r, \u0026c); ok {\n    eail, _ := form_validator.GetString(\"email\", \u0026c)\n    password, _ := form_validator.GetString(\"password\", \u0026c)\n} else {\n\t// Get all the form errors\n    var formErrs = form_validator.FormErrors{}\n    form_validator.GetFormErrors(\u0026c, \u0026formErrs) \n}\n\n```\nThe Field type properties are:\n - Name field is the form's 'name' value\n - Validate sets whether the field requires validation\n - Default set a default value is the form field empty\n - Type sets the type conversion e.g. int8, uint, float16 ...\n# Example\nForm with text fields\n```go\n\nif ok := ValidateForm(r, \u0026c); ok {\n\t\n} else {\n\t// Handle form errors\n}\n```\nForm with files \u0026 text fields\n```go\nif ok := ValidateMultiPartForm(r, \u0026c); ok {\n\t// Form is valid\n} else {\n\t// Handle form errors\n}\n```\n\n### Match field values (password confirmation)\nIf you require password fields, for example to be matched, then assign a `Matches` value to a field:\n```go\nc := Config{\n        MaxMemory: 0,\n        Fields: []Field{\n            {\n                Name:     \"password\",\n                Validate: true,\n                Type:     \"string\",\n            },\n            {\n                Name:     \"confirm_password\",\n                Validate: true,\n                Type:     \"string\",\n                Matches:  \"password\",\n            },\n        },\n}\n```\nThe above validation will fail if the `password` field's value is not the same as the `confirm_password` field.\n\n### Form Value Errors\n`GetFormError` gets a single form error\n```go\nname := GetFormError(\"name\", \u0026c)\n```\n### GetFormErrors\n`GetFormErrors` access all form errors as a map (`FormErrors`) indexed off the form names\n```go\nvar formErrs = form_validator.FormErrors{}\nform_validator.GetFormErrors(\u0026c, \u0026formErrs)\n```\nIf the results of `formErrs` are passed to the template as data then\nall form errors can be accessed from the map via index name, for example:\n```go\n{{ if .FormErrors.title }}\n           \u003cdiv class=\"alert alert-danger\" role=\"alert\"\u003e\n               {{ .FormErrors.title.error }}\n           \u003c/div\u003e\n {{ end }}\n```\nIn this case `FormErrors.title.error` will produce an error message that\ncan be safely displayed to the user.\n\n#### Get the form field value's correct value \u0026 type\nThere are `Get\u003cTYPE\u003e(name string, *Config)` functions for each supported type.\nFor example\n```go\nvar title string\ntitle, _ = GetString(\"title\", \u0026c)\n\nvar id int32\nid, _ = GetInt32(\"id\", \u0026c)\n```\n\n\n### Form Value Type Conversion\nTo convert a form value to a specific type, set the `Type` value in the `Field` struct, for example\n```go\nname := form_validator.Field{\n    {\n        Name:     \"weight\",\n        Validate: true,\n        Default:  \"John\",\n        Type:     \"float32\",\n    },\n}\n```\nIf the form successfully validates, the \"weight\" form value will be `float32(\u003cVALUE\u003e)`\nThe following type conversions are supported:\n- string\n- bool\n- file\n- int, float32, float64\n- int8, int16, int32, int64\n- uint8, uint16, uint32, uint64\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoegasewicz%2Fform-validator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjoegasewicz%2Fform-validator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoegasewicz%2Fform-validator/lists"}