{"id":21423193,"url":"https://github.com/seeadoog/jsonschema","last_synced_at":"2025-07-14T08:31:19.817Z","repository":{"id":57574993,"uuid":"293036284","full_name":"seeadoog/jsonschema","owner":"seeadoog","description":"a very fast jsonschema implemention by golang ","archived":false,"fork":false,"pushed_at":"2024-11-22T07:15:28.000Z","size":243,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-08T05:12:46.574Z","etag":null,"topics":["golang","jsonschema"],"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/seeadoog.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-09-05T08:37:42.000Z","updated_at":"2024-11-22T07:14:37.000Z","dependencies_parsed_at":"2024-06-21T07:12:12.485Z","dependency_job_id":"0567a01e-9d7e-47fc-994f-1bcd0e8212a9","html_url":"https://github.com/seeadoog/jsonschema","commit_stats":null,"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"purl":"pkg:github/seeadoog/jsonschema","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seeadoog%2Fjsonschema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seeadoog%2Fjsonschema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seeadoog%2Fjsonschema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seeadoog%2Fjsonschema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/seeadoog","download_url":"https://codeload.github.com/seeadoog/jsonschema/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seeadoog%2Fjsonschema/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265262597,"owners_count":23736427,"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","jsonschema"],"created_at":"2024-11-22T21:14:44.827Z","updated_at":"2025-07-14T08:31:19.454Z","avatar_url":"https://github.com/seeadoog.png","language":"Go","readme":"## Introduction\nThis is a high-performance jsonschema implementation in Golang, achieving zero memory allocation during validation. \nIt offers a performance boost of more than 10x compared to other open-source versions.\nAdditionally, it supports a rule engine, allowing for the definition of complex validation rules and parameter conversion logic.\n\n## Features\n\n-   Supports custom validators.\n-\tCan generate JSON schemas from Go structs.\n-\tZero memory allocation during validation runtime.\n-\tAllows dynamic changes to JSON values, including setting default values.\n-\tSupports JSON parsing and setting default values.\n-\tSupports logical checks and dynamically setting JSON values during validation.\n-\tNot all standard schema features are fully implemented (no support for reference syntax, and some validators are not implemented).\n-\tSupports rule engine for dynamic value setting and custom complex logic.\n\n## Benchmark \nThis JSONSchema implementation is one of the fastest available. Below is a performance comparison with some mainstream open-source versions,\nsuch as github.com/qri-io/jsonschema and github.com/xeipuuv/gojsonschema.\n\n````\ngoos: darwin\ngoarch: amd64\npkg: github.com/seeadoog/jsonschema/v2\ncpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz\nBenchmarkSchema_local-12                         1236652               955.0 ns/op             0 B/op          0 allocs/op\nBenchmarkSchema_gojsonschema-12                    74304             15591 ns/op            7484 B/op        258 allocs/op\nBenchmarkSchema_qri_io_jsonschema-12               54739             21301 ns/op           14601 B/op        310 allocs/op\nPASS\n````\n\n## Installation\n````\ngo get github.com/seeadoog/jsonschema/v2 \n````\n\n## QuickStart\n\n```go\npackage main\n\nimport (\n\t\"encoding/json\"\n\t\"fmt\"\n\n\t\"github.com/seeadoog/jsonschema/v2\"\n)\n\nfunc main() {\n\tschema := `\n  {\n    \"type\":\"object\",\n    \"properties\":{\n      \"name\":{\n        \"type\":\"string|number\",\n        \"maxLength\":5,\n        \"minLength\":1,\n        \"maximum\":10,\n        \"minimum\":1,\n        \"enum\":[\"1\",\"2\"],\n        \"replaceKey\":\"name2\",\n        \"formatVal\":\"string\",\n        \"format\":\"phone\"\n      }\n    }\n  }\n  `\n\trootSchema := jsonschema.Schema{}\n\n\terr := json.Unmarshal([]byte(schema), \u0026rootSchema)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tjs := `\n  {\n    \"name\":\"1\"\n  }\n  `\n\tvar o interface{}\n\terr = json.Unmarshal([]byte(js), \u0026o)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tfmt.Println(rootSchema.Validate(o))\n\n}\n```\n\n## [License](./LICENSE)\n\n## Supported Validator Examples\n\n#### type \n\nSpecifies the data type of a field \nValid values: string, number, bool, object, array, integer.\n\n```json\n{\n  \"type\": \"string\"\n}\n```\nor \n```json\n{\n  \"type\": \"string|number\"\n}\n```\n\n#### properties\nDefines the structure of an object. When set to object, the field must conform to the defined properties. If undefined properties should be allowed, you can add \"additionalProperties\": true.\n\n```json\n {\n  \"type\": \"object\",\n  \"properties\": {\n    \"name\": {\n        \"type\": \"string\"\n    }\n  },\n  \"additionalProperties\": true\n}\n```\n\n#### maxLength\n\nSpecifies the maximum length of a string or array.\n\n#### minLength\n\nSpecifies the minimum length of a string or array.\n\n#### maximum\n\nDefines the maximum value for numeric fields.\n\n#### minimum\n\nDefines the minimum value for numeric fields.\n\n#### enum\n\nAn array specifying the allowed values.\n\n````json\n{\n  \"enum\": [\"1\",\"2\",\"3\"]\n}\n````\n\n#### required\n\nAn array of strings specifying fields that must be present.\n````json\n{\n  \"required\": [\"username\",\"password\"]\n}\n````\n\n#### pattern\n\nSpecifies a regular expression that the value must match.\n````json\n{\n  \"type\": \"string\",\n  \"pattern\": \"^\\\\d+$\"\n}\n````\n\n#### multipleOf\n\nRequires the value to be a multiple of the given number.\n````json\n{\n  \"type\": \"number\",\n  \"multipleOf\": 5\n}\n````\n\n#### items\n\nSpecifies the validation rules for each item in an array.\n```json\n{\n  \"type\": \"array\",\n  \"items\": {\n      \"type\": \"object\",\n      \"properties\":{\n        \"username\": {\n            \"type\": \"string\"\n        }\n      }\n  }\n}\n```\n\n#### switch\nA conditional validator. Depending on the value of a key, it applies different validation rules.\n\n```json\n\n{\n  \"switch\": \"name\",\n   \"case\": {\n      \"name1\": {\n        \"required\": [\"age1\"]\n      } ,\n      \"name2\": {\n        \"required\": [\"age2\"]\n      }\n\n   },\n   \"defaults\": {\n      \"required\": [\"key3\"]\n   }\n}\n\n```\n\n#### if\n\nIf the if condition passes without errors, the then validator is applied; otherwise, the else validator is used. Errors in if are not raised.\n\n```json\n{\n  \"if\": {\"required\": \"key1\"},\n  \"then\":{\"required\": \"key2\"},\n  \"else\": {\"required\": \"key3\"}\n}\n```\n\n#### dependencies\n\nSpecifies dependent fields that must be present when a particular field is set.\n\n```json\n{\n  \"dependencies\": {\n      \"key1\": [\"key2\",\"key3\"]\n}\n}\n```\n\n#### not\n\nThe validation passes if the not condition fails.\n\n```json\n{\n  \"not\": {\n      \"type\": \"string\"\n  }\n}\n```\n\n### allOf\n\nValidation passes only if all conditions are met.\n\n```json\n{\n  \"allOf\": [\n    {\n        \"type\": \"string\"\n    },{\n        \"maxLength\": 50\n}\n  ]\n}\n```\n\n### anyOf\n\nValidation passes if any of the conditions are met.\n```json\n{\n  \"anyOf\": [\n    {\n        \"type\": \"string\"\n    },{\n        \"maxLength\": 50\n}\n  ]\n}\n```\n\n#### constVal\n\nParameter conversion validator: the field value is replaced by the value in constVal.\n\n```json\n{\n    \"type\": \"object\",\n    \"properties\": {\n         \"name\":{\n              \"type\": \"string\",\n              \"constVal\": \"alen\"\n          }\n    }\n}\n```\n\n#### defaultVal \n\nParameter conversion validator: if the field is missing, it is added with the value from defaultVal.\n\n```json\n{\n    \"type\": \"object\",\n    \"properties\": {\n         \"name\":{\n              \"type\": \"string\",\n              \"defaultVal\": \"alen\"\n          }\n    }\n}\n```\n\n#### replaceKey\n\nParameter conversion validator: the value is copied and renamed to the key specified by replaceKey.\n\n```json\n{\n    \"type\": \"object\",\n    \"properties\": {\n         \"name\":{\n              \"type\": \"string\",\n              \"replaceKey\": \"alen\"\n          }\n    }\n}\n```\n\n\n#### Custom Logic and JSON Conversion\n\n````json\n{\n  \"type\": \"object\",\n  \"properties\": {\n    \"name\": {\n      \"type\": \"string\"\n    },\n    \"age\": {\n      \"type\": \"integer\"\n    }\n  },\n  \"allOf\": [\n    {\n      \"if\": {\n        \"gt\": {\n          \"age\": 20\n        },\n        \"lt\": {\n          \"age\": 50\n        }\n      },\n      \"then\": {\n        \"set\": {\n          \"is_stronger\": true\n        }\n      }\n    },\n    {\n      \"if\": {\n        \"gt\": {\n          \"age\": 5\n        },\n        \"lt\": {\n          \"age\": 15\n        }\n      },\n      \"then\": {\n        \"set\": {\n          \"is_child\": true\n        }\n      }\n    }\n  ]\n}\n````\n\n#### Other Validators\n\nRefer to the official JSON Schema documentation for additional validators.\n\n### Custom Validators\n\n1.\tImplement the Validator interface.\n2.\tCreate a new validator function using NewValidatorFunc.\n3.\tRegister the validator with RegisterValidator(name string, fun NewValidatorFunc).\n````go\ntype Validator interface {\n\tValidate(c *ValidateCtx, value interface{})\n}\n\ntype NewValidatorFunc func(i interface{}, path string, parent Validator) (Validator, error)\n\n\n\n\n````\n\n### Generate Schema from Struct\n```\ntype User struct {\n    Name   string   `json:\"name\" maxLength:\"15\" pattern:\"^[0-9a-zA-Z_\\\\-.]+$\"`\n    Age    int      `json:\"age\" maximum:\"150\" minimum:\"1\" multipleOf:\"2\"`\n    Childs []string `json:\"childs\"`\n}\n\nsc, err := GenerateSchema(\u0026User{})\nif err != nil {\n    panic(err)\n}\n\nfmt.Println(string(sc.Bytes()))\n```\n\nGenerated schema:\n\n````json\n{\n    \"properties\": {\n        \"age\": {\n            \"maximum\": 150,\n            \"minimum\": 1,\n            \"multipleOf\": 2,\n            \"type\": \"integer\"\n        },\n        \"childs\": {\n            \"items\": {\n                \"type\": \"string\"\n            },\n            \"type\": \"array\"\n        },\n        \"name\": {\n            \"maxLength\": 15,\n            \"pattern\": \"^[0-9a-zA-Z_\\\\-.]+$\",\n            \"type\": \"string\"\n            }\n        },\n    \"type\": \"object\"\n}\n\n````\n\n### Advanced Validators\n\n```` \n{\n    \"if\":{\n        \"eq\":{\n            \"username\":\"root\"\n        },\n        \"lt\":{\n            \"age\":30\n        }\n    },\n    \"then\":{\n        \"error\":\"root user age should be \u003c 30\"\n    },\n    \"and\":[\n      {\n        \"if\":{\n          \"neq\":{\n            \"class\":\"\",\n            \"username\":\"\"\n          }\n        },\n        \"then\":{\n          \"set\":{\n              \"desc\":\"${username}(${class})\" ,\n              \"desc_upper\":[\"str.toUpper()\",\"${username}(${class})\"]\n          }\n        }\n      },\n      {\n        \"if\":{\n          \"ipIn\":{\n            \"cip\":[\"1.2.3.4\"]\n          }\n        },\n        \"then\":{\n          \"error\":\"invalid ip: ${cip}\"\n        }\n      }\n    ]\n}\n````","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fseeadoog%2Fjsonschema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fseeadoog%2Fjsonschema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fseeadoog%2Fjsonschema/lists"}