{"id":13448947,"url":"https://github.com/Kangaroux/go-map-schema","last_synced_at":"2025-03-22T18:32:12.878Z","repository":{"id":49991586,"uuid":"369074682","full_name":"Kangaroux/go-map-schema","owner":"Kangaroux","description":"Simple JSON type checking.","archived":false,"fork":false,"pushed_at":"2021-06-30T04:24:54.000Z","size":91,"stargazers_count":84,"open_issues_count":3,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-16T04:31:43.906Z","etag":null,"topics":["api","golang","json","schema","types","validation"],"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/Kangaroux.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":"2021-05-20T04:05:46.000Z","updated_at":"2025-02-11T21:31:28.000Z","dependencies_parsed_at":"2022-08-29T21:52:27.295Z","dependency_job_id":null,"html_url":"https://github.com/Kangaroux/go-map-schema","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kangaroux%2Fgo-map-schema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kangaroux%2Fgo-map-schema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kangaroux%2Fgo-map-schema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kangaroux%2Fgo-map-schema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Kangaroux","download_url":"https://codeload.github.com/Kangaroux/go-map-schema/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245002911,"owners_count":20545511,"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":["api","golang","json","schema","types","validation"],"created_at":"2024-07-31T06:00:25.847Z","updated_at":"2025-03-22T18:32:07.859Z","avatar_url":"https://github.com/Kangaroux.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"# go-map-schema\n\n[![Go Reference](https://img.shields.io/badge/Go-Docs-blue?style=for-the-badge)](https://pkg.go.dev/github.com/Kangaroux/go-map-schema) ![GitHub tag (latest SemVer)](https://img.shields.io/github/v/tag/Kangaroux/go-map-schema?style=for-the-badge\u0026label=Latest\u0026color=green)\n\n## Table of Contents\n\n- [Overview](#overview)\n- [Use Case](#use-case)\n- [Do I Really Need This?](#do-i-really-need-this)\n- [Examples](#examples)\n    - [Usage](#usage)\n    - [Full Code](#full-code)\n    - [Output](#output)\n- [Universal Type Names](#universal-type-names)\n\n## Overview\n\n`go-map-schema` is a tiny library that's useful for comparing a map (usually from JSON) to a struct, and finding any fields that are missing or that have incompatible types.\n\n## Use Case\nThe most common usage would be for an API that accepts JSON from clients.\n\nBefore we can fulfill the request we need to know if the JSON matches what we expect. By verifying the JSON before we even try to `json.Unmarshal` it into a struct, we can be sure the JSON will be safely converted with no loss of data or swallowed errors.\n\nAs a result, you end up with an API that has\n\n1. strict type checking, and\n2. can give the client helpful error messages when the request is invalid\n\n## Do I Really Need This?\n\nFor something like an API, it's worth asking who is going to be the client that is using the API. Is this an API for your single page app, or are you providing a service and it will be used by other developers?\n\nIf the API is \"internal\" and only used within the context of your site, it's probably not necessary. But if someone is trying to use your API, it can be very useful. API documentation is rarely perfect, and giving the client a readable error can help them debug when they get a `400`.\n\n# Examples\n\nRead below for a quick look at how you can use `go-map-schema`.\n\nFor more examples, check out the [examples/](examples/) directory.\n\n## Usage\n\nSuppose we have a `Person` model that contains a nested `Address`\n\n```go\ntype Address struct {\n    Country     string `json:\"country\"`\n    City        string `json:\"city\"`\n    AddressLine string `json:\"address_line\"`\n}\n\ntype Person struct {\n    FirstName string  `json:\"first_name\"`\n    LastName  string  `json:\"last_name\"`\n    Age       int     `json:\"age\"`\n    Address   Address `json:\"address\"`\n}\n```\n\nand a client comes along and makes a `POST` request with this JSON.\n\n```json\n{\n    \"first_name\": \"Jessie\",\n    \"age\": \"26\",\n    \"address\": {\n      \"country\": \"US\",\n      \"city\": null\n    }\n}\n```\n\nWe can unmarshal the JSON into a map to make it easier to work with, and then compare it with the `Person` model.\n\n```go\nsrc := make(map[string]interface{})\njson.Unmarshal(payload, \u0026src)\n\ndst := Person{}\nresults, err := schema.CompareMapToStruct(\u0026dst, src, nil)\n```\n\nAfter comparing we now have a `CompareResults` instance stored in `results`.\n\n```go\ntype CompareResults struct {\n\tMismatchedFields []FieldMismatch\n\tMissingFields    []FieldMissing\n}\n```\n\nWith this, we can quickly see which fields have mismatched types, as well as any fields that are in the `Person` struct but not the JSON.\n\nCheck out [examples/type-errors](examples/type-errors) for the complete example.\n\n# Universal Type Names\n\nBy default, `CompareMapToStruct` will use the `DetailedTypeName` func when reporting a type mismatch. The detailed type name includes some extra information that you may not want a client to see.\n\nFor example, trying to convert a `float64` into an `int32` will report a mismatch between `float64` and `int32`.\n\nIf you don't want to include bit size, you can use `SimpleTypeName` which converts `floatX -\u003e float`, `intX -\u003e int`, `uintX -\u003e uint`.\n\n```go\nopts := \u0026schema.CompareOpts{\n    TypeNameFunc: schema.SimpleTypeName,\n}\n\nschema.CompareMapToStruct(dst, src, opts)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FKangaroux%2Fgo-map-schema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FKangaroux%2Fgo-map-schema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FKangaroux%2Fgo-map-schema/lists"}