{"id":13895013,"url":"https://github.com/qri-io/jsonschema","last_synced_at":"2025-05-13T19:16:00.482Z","repository":{"id":41374491,"uuid":"117295809","full_name":"qri-io/jsonschema","owner":"qri-io","description":"golang implementation of  https://json-schema.org drafts 7 \u0026 2019-09","archived":false,"fork":false,"pushed_at":"2024-12-18T08:22:28.000Z","size":4807,"stargazers_count":464,"open_issues_count":43,"forks_count":54,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-04-27T20:05:26.210Z","etag":null,"topics":["golang","json-schema"],"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/qri-io.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"code_of_conduct.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-01-12T22:58:25.000Z","updated_at":"2025-04-24T01:59:58.000Z","dependencies_parsed_at":"2024-09-20T08:01:01.144Z","dependency_job_id":"0f3ccdf0-8d0e-4e8d-915e-01d266c9bc42","html_url":"https://github.com/qri-io/jsonschema","commit_stats":{"total_commits":68,"total_committers":21,"mean_commits":3.238095238095238,"dds":0.6764705882352942,"last_synced_commit":"780655b2ba0ed03f11ecc8606a8f6e7380bb57be"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qri-io%2Fjsonschema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qri-io%2Fjsonschema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qri-io%2Fjsonschema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qri-io%2Fjsonschema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/qri-io","download_url":"https://codeload.github.com/qri-io/jsonschema/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254010822,"owners_count":21999002,"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":["golang","json-schema"],"created_at":"2024-08-06T18:01:55.932Z","updated_at":"2025-05-13T19:15:55.471Z","avatar_url":"https://github.com/qri-io.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"# jsonschema\n[![Qri](https://img.shields.io/badge/made%20by-qri-magenta.svg?style=flat-square)](https://qri.io)\n[![GoDoc](https://godoc.org/github.com/qri-io/jsonschema?status.svg)](http://godoc.org/github.com/qri-io/jsonschema)\n[![License](https://img.shields.io/github/license/qri-io/jsonschema.svg?style=flat-square)](./LICENSE)\n[![Codecov](https://img.shields.io/codecov/c/github/qri-io/jsonschema.svg?style=flat-square)](https://codecov.io/gh/qri-io/jsonschema)\n[![CI](https://img.shields.io/circleci/project/github/qri-io/jsonschema.svg?style=flat-square)](https://circleci.com/gh/qri-io/jsonschema)\n[![Go Report Card](https://goreportcard.com/badge/github.com/qri-io/jsonschema)](https://goreportcard.com/report/github.com/qri-io/jsonschema)\n\ngolang implementation of the [JSON Schema Specification](http://json-schema.org/), which lets you write JSON that validates some other json. Rad.\n\n### Package Features\n\n* Encode schemas back to JSON\n* Supply Your own Custom Validators\n* Uses Standard Go idioms\n* Fastest Go implementation of [JSON Schema validators](http://json-schema.org/implementations.html#validators) (draft2019_9 only, (old — draft 7) benchmarks are [here](https://github.com/TheWildBlue/validator-benchmarks) — thanks [@TheWildBlue](https://github.com/TheWildBlue)!)\n\n### Getting Involved\n\nWe would love involvement from more people! If you notice any errors or would\nlike to submit changes, please see our\n[Contributing Guidelines](./.github/CONTRIBUTING.md).\n\n### Developing\n\nWe’ve set up a separate document for [developer guidelines](https://github.com/qri-io/jsonschema/blob/master/DEVELOPERS.md)!\n\n## Basic Usage\n\nHere’s a quick example pulled from the [godoc](https://godoc.org/github.com/qri-io/jsonschema):\n\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"encoding/json\"\n\t\"fmt\"\n\n\t\"github.com/qri-io/jsonschema\"\n)\n\nfunc main() {\n\tctx := context.Background()\n\tvar schemaData = []byte(`{\n    \"$id\": \"https://qri.io/schema/\",\n    \"$comment\" : \"sample comment\",\n    \"title\": \"Person\",\n    \"type\": \"object\",\n    \"properties\": {\n        \"firstName\": {\n            \"type\": \"string\"\n        },\n        \"lastName\": {\n            \"type\": \"string\"\n        },\n        \"age\": {\n            \"description\": \"Age in years\",\n            \"type\": \"integer\",\n            \"minimum\": 0\n        },\n        \"friends\": {\n          \"type\" : \"array\",\n          \"items\" : { \"title\" : \"REFERENCE\", \"$ref\" : \"#\" }\n        }\n    },\n    \"required\": [\"firstName\", \"lastName\"]\n  }`)\n\n\trs := \u0026jsonschema.Schema{}\n\tif err := json.Unmarshal(schemaData, rs); err != nil {\n\t\tpanic(\"unmarshal schema: \" + err.Error())\n\t}\n\n\tvar valid = []byte(`{\n    \"firstName\" : \"George\",\n    \"lastName\" : \"Michael\"\n    }`)\n\terrs, err := rs.ValidateBytes(ctx, valid)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tif len(errs) \u003e 0 {\n\t\tfmt.Println(errs[0].Error())\n\t}\n\n\tvar invalidPerson = []byte(`{\n    \"firstName\" : \"Prince\"\n    }`)\n\n\terrs, err = rs.ValidateBytes(ctx, invalidPerson)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tif len(errs) \u003e 0 {\n\t\tfmt.Println(errs[0].Error())\n\t}\n\n\tvar invalidFriend = []byte(`{\n    \"firstName\" : \"Jay\",\n    \"lastName\" : \"Z\",\n    \"friends\" : [{\n      \"firstName\" : \"Nas\"\n      }]\n    }`)\n\terrs, err = rs.ValidateBytes(ctx, invalidFriend)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tif len(errs) \u003e 0 {\n\t\tfmt.Println(errs[0].Error())\n\t}\n}\n\n// Output:\n// /: {\"firstName\":\"Prince... \"lastName\" value is required\n// /friends/0: {\"firstName\":\"Nas\"} \"lastName\" value is required\n```\n\n## Custom Keywords\n\nThe [godoc](https://godoc.org/github.com/qri-io/jsonschema) gives an example of how to supply your own validators to extend the standard keywords supported by the spec.\n\nIt involves three steps that should happen _before_ allocating any Schema instances that use the validator:\n1. create a custom type that implements the `Keyword` interface\n2. Load the appropriate draft keyword set (see `draft2019_09_keywords.go`)\n3. call RegisterKeyword with the keyword you’d like to detect in JSON, and a `KeyMaker` function.\n\n\n```go\npackage main\n\nimport (\n    \"context\"\n    \"encoding/json\"\n    \"fmt\"\n\n    jptr \"github.com/qri-io/jsonpointer\"\n    \"github.com/qri-io/jsonschema\"\n)\n\n// your custom validator\ntype IsFoo bool\n\n// newIsFoo is a jsonschama.KeyMaker\nfunc newIsFoo() jsonschema.Keyword {\n    return new(IsFoo)\n}\n\n// Validate implements jsonschema.Keyword\nfunc (f *IsFoo) Validate(propPath string, data interface{}, errs *[]jsonschema.KeyError) {}\n\n// Register implements jsonschema.Keyword\nfunc (f *IsFoo) Register(uri string, registry *jsonschema.SchemaRegistry) {}\n\n// Resolve implements jsonschema.Keyword\nfunc (f *IsFoo) Resolve(pointer jptr.Pointer, uri string) *jsonschema.Schema {\n    return nil\n}\n\n// ValidateKeyword implements jsonschema.Keyword\nfunc (f *IsFoo) ValidateKeyword(ctx context.Context, currentState *jsonschema.ValidationState, data interface{}) {\n    if str, ok := data.(string); ok {\n        if str != \"foo\" {\n            currentState.AddError(data, fmt.Sprintf(\"should be foo. plz make '%s' == foo. plz\", str))\n        }\n    }\n}\n\nfunc main() {\n    // register a custom validator by supplying a function\n    // that creates new instances of your Validator.\n    jsonschema.RegisterKeyword(\"foo\", newIsFoo)\n\n    // If you register a custom validator, you'll need to manually register\n    // any other JSON Schema validators you need.\n    jsonschema.LoadDraft2019_09()\n\n    schBytes := []byte(`{ \"foo\": true }`)\n\n    rs := new(jsonschema.Schema)\n    if err := json.Unmarshal(schBytes, rs); err != nil {\n        // Real programs handle errors.\n        panic(err)\n    }\n\n    errs, err := rs.ValidateBytes(context.Background(), []byte(`\"bar\"`))\n    if err != nil {\n        panic(err)\n    }\n    fmt.Println(errs[0].Error())\n    // Output: /: \"bar\" should be foo. plz make 'bar' == foo. plz\n}\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqri-io%2Fjsonschema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fqri-io%2Fjsonschema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqri-io%2Fjsonschema/lists"}