{"id":18974049,"url":"https://github.com/pungyeon/required","last_synced_at":"2025-04-19T16:39:47.713Z","repository":{"id":40335827,"uuid":"228222744","full_name":"Pungyeon/required","owner":"Pungyeon","description":null,"archived":false,"fork":false,"pushed_at":"2022-10-19T10:50:16.000Z","size":212,"stargazers_count":6,"open_issues_count":1,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-29T10:12:39.046Z","etag":null,"topics":["go","golang","json","json-parser","parser","parsing","parsing-library","required-fields"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Pungyeon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-12-15T17:23:28.000Z","updated_at":"2024-05-31T17:29:06.000Z","dependencies_parsed_at":"2022-08-18T22:10:28.003Z","dependency_job_id":null,"html_url":"https://github.com/Pungyeon/required","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/Pungyeon%2Frequired","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Pungyeon%2Frequired/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Pungyeon%2Frequired/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Pungyeon%2Frequired/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Pungyeon","download_url":"https://codeload.github.com/Pungyeon/required/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249223935,"owners_count":21232833,"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":["go","golang","json","json-parser","parser","parsing","parsing-library","required-fields"],"created_at":"2024-11-08T15:13:56.196Z","updated_at":"2025-04-16T09:33:50.413Z","avatar_url":"https://github.com/Pungyeon.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Required JSON tags\n\n[![GoDoc](https://godoc.org/github.com/Pungyeon/required?status.svg)](https://godoc.org/github.com/Pungyeon/required) \n[![Go Report Card](https://goreportcard.com/badge/github.com/Pungyeon/required)](https://goreportcard.com/report/github.com/Pungyeon/required)  \n\n## Introduction\nLong story short, a junior engineer at work asked me (a long time ago now). How to make `JSON` fields required. I didn't have a good answer for him, and after some short research it turns out that there isn't a good answer for this!\n\nI therefore wrote the following article on making a package validating `JSON` input: [article/README.md](Original Article).\n\nHowever, a delve into the rabbit hole later, I have made my own `JSON` parser, which does support required tags. It is definitely not a finished or production ready product, so all help and feedback is extremely welcome. The original article was meant to solve a problem, whereas the `JSON` parser spawned from pure curiosity.\n\n## Usage\nIt should be possible to substitute the std `json` import with the current library, without issue.\n\nIf this is not the case, please make sure to report this as an issue here on GitHub :)\n\n### JSON Unmarshalling\nThe usage is very simple. Adding the `JSON` struct tag \"required\", will have the parser enforce this field to be present, when unmarshalling:\n\n```go\ntype User struct {\n  FirstName string `json:\"first_name,required\"`\n  LastName  string `json:\"last_name,required\"`\n  Email     string `json:\"email,required\"`\n  GitHub    string `json:\"github\"`\n  LinkedIn  string\n}\n``` \n\nIn the above example, `FirstName`, `LastName` and `Email` are required, where as `GitHub` and `LinkedIn` are not.\n\n```go\nfunc main() {\n    var user User\n    if err := Unmarshal([]byte(`{}`), \u0026user); err != nil {\n        panic(err) // this will return an error because of the missing required fields\n    }\n    fmt.Println(user)\n}\n```\n\nWhere as the following JSON string will parse without returning an error:\n\n```go\n`{\n  \"first_name\": \"lasse\",\n  \"last_name\": \"jakobsen\",\n  \"email\": \"lasse@jakobsen.dev\"\n}`\n```\n\nFurthermore, it is possible to implement the `Required` interface, to create custom validation of a type.\n\n```go\ntype Required interface {\n\tIsValueValid() error\n}\n```\n\nBelow is an example of implementing this interface for a type alias of `string` called `Email`. This will validate the string to adhere to the general structure of an e-mail. This means, that when being parsed, the `json` will be ensured to not only exist, but also that it's valid as defined in the `IsValidValue` function.\n\n```go\ntype Email string\n\nfunc (email Email) IsValueValid() error {\n\tmatched, err := regexp.MatchString(`.+@.+\\..+`, string(email))\n\tif err != nil {\n\t\treturn err\n\t}\n\tif !matched {\n\t\treturn errors.New(\"invalid email\")\n\t}\n\treturn nil\n}\n```\n\nRefer to samples for a more detailed example of this.\n\n### Marshalling\nAs of writing this document, this library is currently using a custom `json.Marshal` and `json.Encoder`. This library *does not currently support `required` tag checking*, please show your interest, if you would like this by creating a new issue. The `json.Marshal` function is compatible with the standard library functionality. Though, substantially faster:\n\n```\ngoos: darwin\ngoarch: amd64\npkg: github.com/Pungyeon/required/pkg/json\nBenchmarkMarshalStd-8             603121              1859 ns/op             752 B/op         12 allocs/op\nBenchmarkMarshalPkg-8             729928              1581 ns/op             832 B/op         10 allocs/op\nPASS\nok      github.com/Pungyeon/required/pkg/json   2.445s\n```\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpungyeon%2Frequired","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpungyeon%2Frequired","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpungyeon%2Frequired/lists"}