{"id":23350855,"url":"https://github.com/e0selmy4v/schema2class","last_synced_at":"2026-05-02T03:31:19.882Z","repository":{"id":65084631,"uuid":"581799550","full_name":"E0SelmY4V/schema2class","owner":"E0SelmY4V","description":"Parse JSON schema to class","archived":false,"fork":false,"pushed_at":"2023-01-25T17:48:17.000Z","size":28,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-13T20:17:45.947Z","etag":null,"topics":["class","es3","ie6","json-schema","parse","parser","schema","schema-parser"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/schema2class","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/E0SelmY4V.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":"2022-12-24T11:32:54.000Z","updated_at":"2022-12-26T08:36:36.000Z","dependencies_parsed_at":"2023-02-14T10:01:49.089Z","dependency_job_id":null,"html_url":"https://github.com/E0SelmY4V/schema2class","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/E0SelmY4V%2Fschema2class","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/E0SelmY4V%2Fschema2class/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/E0SelmY4V%2Fschema2class/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/E0SelmY4V%2Fschema2class/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/E0SelmY4V","download_url":"https://codeload.github.com/E0SelmY4V/schema2class/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247704579,"owners_count":20982299,"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":["class","es3","ie6","json-schema","parse","parser","schema","schema-parser"],"created_at":"2024-12-21T08:18:58.484Z","updated_at":"2026-05-02T03:31:19.846Z","avatar_url":"https://github.com/E0SelmY4V.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Schema to Class\n\nParse JSON schema to class, and you can 'new' your data easily.\n\nThis is also an ES3 edition for IE Browser.\n\n## Import\n\n- ES Module\n\n  ```javascript\n  import schema2class from 'schema2class';\n  ```\n\n- Typescript\n\n  ```javascript\n  import schema2class = require('schema2class');\n  ```\n\n- CommonJS\n\n  ```javascript\n  const schema2class = require('schema2class');\n  // or\n  var schema2class = require('schema2class/es3');\n  ```\n\n- Broswer\n\n  ```html\n  \u003c!--[if IE]\u003e\u003cscript src=\"schema2class/es3.js\"\u003e\u003c![endif]--\u003e\n    \u003cscript src=\"schema2class/index.js\"\u003e\u003c/script\u003e\n  \u003c!--[if IE]\u003e\u003c/script\u003e\u003c![endif]--\u003e\n  \u003cscript\u003e\n    /// \u003creference path=\"schema2class/browser.d.ts\" /\u003e\n\n    console.log(schema2class);\n  \u003c/script\u003e\n  ```\n\n## Usage\n\nFor example, if you have a JSON schema like this:\n\n```json\n{\n  \"type\": \"object\",\n  \"properties\": {\n    \"a\": {\n      \"type\": \"integer\",\n      \"default\": 100\n    },\n    \"b\": {\n      \"type\": \"array\",\n      \"items\": {\n        \"type\": [\n          \"array\",\n          \"object\"\n        ],\n        \"items\": {\n          \"type\": \"string\",\n          \"default\": \"klm\"\n        },\n        \"properties\": {\n          \"str\": {\n            \"type\": \"string\",\n            \"default\": \"abc\"\n          }\n        },\n        \"default\": [\n          \"hig\"\n        ]\n      },\n      \"default\": [\n        {\n          \"str\": \"def\"\n        }\n      ]\n    }\n  }\n}\n```\n\nYou can create the factory function for this schema, just like:\n\n```javascript\nconst shcema = require('./schema.json');\nconst s2c = require('schema2class');\nconst log = console.log;\n\nconst schemaFactory0 = s2c(shcema, {\n  realArray: true, // Bad performance, but '[]'\n});\nconst data0 = schemaFactory0();\nlog(data0); // ParsedObj {}\nlog(data0.a); // 100\nlog(data0.b); // ParsedArr(1) [ \u003c1 empty items\u003e ]\nlog(data0.b[0].str); // def\nlog(data0.b instanceof Array); // true\nlog(Array.isArray(data0.b)); // true\n\nconst schemaFactory1 = s2c(shcema);\nconst data1 = schemaFactory1({\n  b: [\n    { str: \"jkl\" },\n    {},\n    void 0,\n    [void 0],\n  ],\n});\nlog(data1.b[0]); // ParsedObj { str: 'jkl' }\nlog(data1.b[1].str); // abc\nlog(data1.b[2][0]); // hig\nlog(data1.b[3]); // ParsedArr { '0': 'klm', length: 1 }\nlog(data1.b instanceof Array); // true\nlog(Array.isArray(data1.b)); // false\n```\n\nYou can also check the correctness of the data through the factory.\n\n```javascript\nconst shcema = require('./schema.json');\nconst s2c = require('schema2class');\n\nconst schemaFactory = s2c(shcema, {\n  check: true, // Check all the error types\n});\n\nschemaFactory({ c: 123 });\n// ReferenceError: 'c' is not a key of { a; b; }\n\nschemaFactory({ b: ['asd'] });\n// TypeError: Type 'string' is not assignable to type 'array | object'\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fe0selmy4v%2Fschema2class","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fe0selmy4v%2Fschema2class","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fe0selmy4v%2Fschema2class/lists"}