{"id":18420336,"url":"https://github.com/emurenmrz/json-data-structure-validator","last_synced_at":"2026-04-25T21:33:56.667Z","repository":{"id":160581508,"uuid":"537317794","full_name":"emurenMRz/json-data-structure-validator","owner":"emurenMRz","description":"Validates the keys required for an object and the type of its value.","archived":false,"fork":false,"pushed_at":"2024-01-21T08:48:45.000Z","size":9,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-13T11:54:07.726Z","etag":null,"topics":["javascript-library","json"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/emurenMRz.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":"2022-09-16T05:29:30.000Z","updated_at":"2022-09-16T12:38:38.000Z","dependencies_parsed_at":"2024-12-24T18:11:32.341Z","dependency_job_id":"30aa92e1-8cea-4474-b5b0-b0f85b50d9e3","html_url":"https://github.com/emurenMRz/json-data-structure-validator","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/emurenMRz/json-data-structure-validator","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emurenMRz%2Fjson-data-structure-validator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emurenMRz%2Fjson-data-structure-validator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emurenMRz%2Fjson-data-structure-validator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emurenMRz%2Fjson-data-structure-validator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/emurenMRz","download_url":"https://codeload.github.com/emurenMRz/json-data-structure-validator/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emurenMRz%2Fjson-data-structure-validator/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32278249,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-25T18:29:39.964Z","status":"ssl_error","status_checked_at":"2026-04-25T18:29:32.149Z","response_time":59,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["javascript-library","json"],"created_at":"2024-11-06T04:21:14.474Z","updated_at":"2026-04-25T21:33:56.651Z","avatar_url":"https://github.com/emurenMRz.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# json-data-structure-validator\n\nValidates the keys required for an object and the type of its value.\n\n## Example\n\n### Basic usage\n\nThe validate() function validates the JSON object passed as its first argument with the format object passed as its second argument.\n\n```\nimport { ValidationError, ValidType, validate } from \"./json-data-structure-validator/index.js\";\n\nconst json = {\n\talice: {\n\t\theight: 123,\n\t\tgender: \"girl\"\n\t},\n\tbob: {\n\t\theight: 116,\n\t\tweight: 35,\n\t\tgender: \"boy\"\n\t}\n};\n\nvalidate(json, {\n\talice: {\n\t\theight: ValidType.Number,\n\t\tgender: ValidType.String\n\t},\n\tbob: {\n\t\theight: ValidType.Number,\n\t\tweight: ValidType.Number,\n\t\tgender: ValidType.String\n\t}\n});\n```\n\nIf a problem occurs during validation, such as a different type or missing key required by the format object, a \"ValidationError\" exception is raised.\n\n```\nimport { ValidationError, ValidType, validate } from \"./json-data-structure-validator/index.js\";\n\nconst json = {\n\talice: {\n\t\theight: 123,\n\t\tgender: \"girl\"\n\t},\n};\n\nvalidate(json, {\n\talice: {\n\t\theight: ValidType.Number,\n\t\tweight: ValidType.Number,\n\t\tgender: ValidType.String\n\t},\n});\n# ValidationError Need key: {weight}\n```\n\n### Option key\n\n\"`?`\" in the key name in the format object. in the key name in the format object makes it an optional key.\n\n```\nimport { ValidationError, ValidType, validate } from \"./json-data-structure-validator/index.js\";\n\nconst json = {\n\talice: {\n\t\theight: 123,\n\t\tgender: \"girl\"\n\t},\n};\n\nvalidate(json, {\n\talice: {\n\t\theight: ValidType.Number,\n\t\t\"weight?\": ValidType.Number,\n\t\tgender: ValidType.String\n\t},\n});\n# Exceptions disappear\n```\n\n### Array data\n\nThe \"`[]`\" in the key name indicates an array.\n\n```\nimport { ValidationError, ValidType, validate } from \"./json-data-structure-validator/index.js\";\n\nconst json = {\n\tpeople: [\n\t\t\"debbie\",\n\t\t\"eliza\",\n\t\t\"flora\"\n\t],\n\t\"high people\": [\n\t\t{\n\t\t\tname: \"alice\",\n\t\t\tage: 20\n\t\t},\n\t\t{\n\t\t\tname: \"bob\",\n\t\t\tage: 21\n\t\t},\n\t\t{\n\t\t\tname: \"charlie\",\n\t\t\tage: 22\n\t\t},\n\t]\n};\n\nvalidate(json, {\n\t\"people[]\": ValidType.String,\n\t\"high people[]\": {\n\t\tname: ValidType.String,\n\t\tage: ValidType.Number\n\t},\n});\n```\n\n### Regexp key\n\nKey names can be specified by regular expressions.\n\n```\nimport { ValidationError, ValidType, validate } from \"./json-data-structure-validator/index.js\";\n\nconst json = {\n\t\"100\": \"hundred\",\n\t\"150\": \"one hundred and fifty\",\n\t\"1000\": \"thousand\",\n};\n\nvalidate(json, {\n\t\"/\\\\d+/\": ValidType.String\n});\n```\n\n### User type\n\nSince `ValidType` is a normal function, you may specify your own conversion function.\n\nAfter validation by the validate() function, the contents of json.ids are replaced with the return value of the original function.\n\n```\nimport { ValidationError, ValidType, validate } from \"./json-data-structure-validator/index.js\";\n\nconst json = {\n\tids: \"1,2,3,4,5\"\n};\n\nvalidate(json, {\n\tids: (v, nodePath) =\u003e {\n\t\tif (typeof v !== \"string\")\n\t\t\tthrow new ValidationError(`${nodePath}Need ids '${JSON.stringify(v)}'`);\n\t\treturn v.split(\",\");\n\t}\n});\n# json.ids: \"1,2,3,4,5\" =\u003e json.ids: [1,2,3,4,5]\n```\n\nThe first argument is the value to be verified. This is required.\n\nThe second argument \"`nodePath`\" is the node path being verified. It is not required, but is effective when an exception occurs in a deep object.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femurenmrz%2Fjson-data-structure-validator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femurenmrz%2Fjson-data-structure-validator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femurenmrz%2Fjson-data-structure-validator/lists"}