{"id":20056253,"url":"https://github.com/chialab/schema-model","last_synced_at":"2025-10-08T19:09:28.924Z","repository":{"id":18498716,"uuid":"84457116","full_name":"chialab/schema-model","owner":"chialab","description":"Generate Model classes based on JSON Schema definition.","archived":false,"fork":false,"pushed_at":"2022-02-12T18:14:26.000Z","size":251,"stargazers_count":11,"open_issues_count":4,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-10-02T13:58:50.612Z","etag":null,"topics":["json-schema"],"latest_commit_sha":null,"homepage":"https://www.chialab.io/p/schema-model","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/chialab.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":"2017-03-09T15:25:14.000Z","updated_at":"2024-09-07T17:59:15.000Z","dependencies_parsed_at":"2022-07-26T23:01:57.344Z","dependency_job_id":null,"html_url":"https://github.com/chialab/schema-model","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/chialab/schema-model","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chialab%2Fschema-model","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chialab%2Fschema-model/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chialab%2Fschema-model/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chialab%2Fschema-model/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chialab","download_url":"https://codeload.github.com/chialab/schema-model/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chialab%2Fschema-model/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278250085,"owners_count":25955840,"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","status":"online","status_checked_at":"2025-10-03T02:00:06.070Z","response_time":53,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["json-schema"],"created_at":"2024-11-13T12:52:53.281Z","updated_at":"2025-10-08T19:09:28.905Z","avatar_url":"https://github.com/chialab.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n    \u003cstrong\u003eSchemaModel\u003c/strong\u003e • Generate Model classes based on \u003ca href=\"http://json-schema.org/\"\u003eJSON Schema\u003c/a\u003e definition.\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://www.chialab.io/p/schema-model\"\u003e\u003cimg alt=\"Documentation link\" src=\"https://img.shields.io/badge/Docs-chialab.io-lightgrey.svg?style=flat-square\"\u003e\u003c/a\u003e\n    \u003ca href=\"https://github.com/chialab/schema-model\"\u003e\u003cimg alt=\"Source link\" src=\"https://img.shields.io/badge/Source-GitHub-lightgrey.svg?style=flat-square\"\u003e\u003c/a\u003e\n    \u003ca href=\"https://www.chialab.it\"\u003e\u003cimg alt=\"Authors link\" src=\"https://img.shields.io/badge/Authors-Chialab-lightgrey.svg?style=flat-square\"\u003e\u003c/a\u003e\n    \u003ca href=\"https://www.npmjs.com/package/@chialab/schema-model\"\u003e\u003cimg alt=\"NPM\" src=\"https://img.shields.io/npm/v/@chialab/schema-model.svg?style=flat-square\"\u003e\u003c/a\u003e\n    \u003ca href=\"https://github.com/chialab/schema-model/blob/master/LICENSE\"\u003e\u003cimg alt=\"License\" src=\"https://img.shields.io/npm/l/@chialab/schema-model.svg?style=flat-square\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n---\n\n## Install\n\n```sh\n$ npm install @chialab/schema-model\n# or\n$ yarn add @chialab/schema-model\n```\n\nUse via cdn:\n```html\n\u003cscript type=\"text/javascript\" src=\"https://unpkg.com/@chialab/schema-model-js\"\u003e\u003c/script\u003e\n```\n\n## Usage\n\nThe `SchemaModel` object can be extended or use as factory to create Model classes dynamically.\n\n### Extend the base model\n\n```js\nimport SchemaModel from '@chialab/schema-model';\n\nclass PersonModel extends SchemaModel {\n    static get schema() {\n        return {\n            type: 'object',\n            properties: {\n                id: {\n                    type: 'number',\n                },\n                firstName: {\n                    type: 'string',\n                },\n                lastName: {\n                    type: 'string',\n                },\n                married: {\n                    type: 'boolean',\n                },\n            },\n            required: ['id'],\n        };\n    }\n}\n\nlet person = new PersonModel({\n    id: 1,\n});\n```\n\n### As factory\n\n```js\nconst PersonModel = SchemaModel.create({\n    type: 'object',\n    properties: {\n        id: {\n            type: 'number',\n        },\n        firstName: {\n            type: 'string',\n        },\n        lastName: {\n            type: 'string',\n        },\n        married: {\n            type: 'boolean',\n        },\n    },\n    required: ['id'],\n});\n\nlet person = new PersonModel({\n    id: 1,\n});\n```\n\n## Validation\n\nSchemaModel uses [tv4](https://github.com/geraintluff/tv4) to validate model data on creation and update.\n\n### Get the validation state\n\nUse the `.validate` method to retrieve the validation state of the model.\n\n```js\nlet person = new PersonModel();\nlet validation = person.validate();\nconsole.log(validation.valid); // --\u003e false\nconsole.log(validation.error.message); // --\u003e 'Missing required property: id'\n```\n\n### Creation and update\n\nWhen a set of data is pass to the constructor or to the `.set` method, the model will try to validate them. If the validation fails, an exception is thrown and the new data **will not** be set.\n\n```js\ntry {\n    let person = new PersonModel({ firstName: 'Alan' });\n} catch (err) {\n    console.log(err.message); // --\u003e 'Missing required property: id'\n}\n```\n\n```js\ntry {\n    let person = new PersonModel({ id: 1, firstName: 'Alan' });\n    person.set({\n        lastName: 10,\n    });\n} catch (err) {\n    console.log(err.message); // --\u003e 'Invalid type: number (expected string)'\n}\n```\n\n### Skip validation\n\nBy the way, you can disabled the auto validation passing `validate: false` as option for constructor/set.\n\n```js\nlet person = new PersonModel({ firstName: 'Alan' }, { validate: false });\nlet validation = person.validate();\nconsole.log(validation.valid); // --\u003e false\nconsole.log(validation.error.message); // --\u003e 'Missing required property: id'\n```\n\n## Getting and setting data\n\nIn order to get an object representing model data, you can use the `.toJSON` helper, which converts all model instances in javascript plain objects:\n\n```js\nlet person = new PersonModel({ id: 1, firstName: 'Alan' });\nconsole.log(person); // --\u003e PersonModel{ id: 1, firstName: 'Alan' }\nconsole.log(person.toJSON()); // --\u003e { id: 1, firstName: 'Alan' }\n```\n\nYou can access a property of the model using the `.get` method, or accessing directly to its reference:\n\n```js\nlet person = new PersonModel({ id: 1, firstName: 'Alan' });\nconsole.log(person.get('id')); // --\u003e 1\nconsole.log(person.firstName); // --\u003e 'Alan'\n```\n\nBy the way, you should always use the `.set` method to update the model:\n```js\nlet person = new PersonModel({ id: 1, firstName: 'Alan' });\n\n// ok!\nperson.set({\n    lastName: 'Turing',\n});\n\n// no no no\nperson.lastName = 'Turing';\n```\n\n### Define getters and setters\n\nUsing ES2015 classes (or `Object.defineProperty` programmatically), you can define a custom getter and setter for a property:\n\n```js\nimport SchemaModel from '@chialab/schema-model';\n\nclass PersonModel extends SchemaModel {\n    static get schema() {\n        return {\n            type: 'object',\n            properties: {\n                id: {\n                    type: 'number',\n                },\n                firstName: {\n                    type: 'string',\n                },\n                lastName: {\n                    type: 'string',\n                },\n                married: {\n                    type: 'boolean',\n                },\n            },\n            required: ['id'],\n        };\n    }\n\n    set married(married) {\n        // passing the `internal: true` options you can set a private property.\n        this.set('married', !!married, { internal: true });\n    }\n\n    get married() {\n        // setup a default value for a property\n        return this.get('married', { internal: true }) || false;\n    }\n}\n\nlet person = new PersonModel({\n    id: 1,\n});\nconsole.log(person.married); // --\u003e false\nperson.set({ married: true });\nconsole.log(person.married); // --\u003e true\n```\n\n## Development\n\n[![Build status](https://github.com/chialab/schema-model/workflows/Main/badge.svg)](https://github.com/chialab/schema-model/actions?query=workflow%3ABuild)\n[![codecov](https://codecov.io/gh/chialab/schema-model/branch/master/graph/badge.svg)](https://codecov.io/gh/chialab/schema-model)\n\n[![Sauce Test Status](https://saucelabs.com/browser-matrix/chialab-sl-013.svg)](https://app.saucelabs.com/u/chialab-sl-013)\n\n### Requirements\n\nIn order to build and test SchemaModel, the following requirements are needed:\n* [NodeJS](https://nodejs.org/) (\u003e= 10.0.0)\n* [Yarn](https://yarnpkg.com)\n* [RNA](https://github.com/chialab/rna-cli) (\u003e= 3.0.0)\n\n### Build the project\n\nInstall the dependencies and run the `build` script:\n```\n$ yarn install\n$ yarn build\n```\n\nThis will generate the UMD and ESM bundles in the `dist` folder, as well as the declaration file.\n\n### Test the project\n\nRun the `test` script:\n\n```\n$ yarn test\n```\n\n---\n\n## License\n\nSchemaModel is released under the [MIT](https://github.com/chialab/schema-model/blob/master/LICENSE) license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchialab%2Fschema-model","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchialab%2Fschema-model","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchialab%2Fschema-model/lists"}