Model(JSON)js
A JavaScript Model class designed to serialize and transfer structured data within the services/apps you build or across networks. It is paired with Schema(JSON)js to provide structure and validation capabilities to your data model.
Motivations behind the project:
- implement a data serialization solution for structured JSON data
- backed with JSON Schema definition/validation
- leverage modern JS features
- small and lightweight with only one dependency
- universal support for the latest browsers and Node.js
- no code generation
Contents
Installing
Install using npm
:
$ npm install --save model-json-js
Examples
Below are a few examples on how to use the Model and Schema classes. For the sake of the following examples, here is a sample Schema we'll reference with all the examples.
const TestSchema = {
title: 'Test',
$id: 'https://hiveframework.io/api/v1/models/Test',
type: 'object',
properties: {
test: {
type: 'string'
},
another: {
type: 'string'
}
},
required: ['test']
}
const meta = {
schema: 'https://hiveframework.io/api/v1/models/Test'
}
const testData = {
type: 'Test',
payload: { test: 'object' },
meta
}
Initialization:
-
An example Model initialized with raw data and schema.
const model = await new Model(testData, TestSchema)
-
An example Model initialized with a cached JSON Schema references defined.
const testSchema = await new Schema(TestSchema)
const model = await new Model(testData, testSchema)
-
An example immutable Model initialized with raw data and schema.
const model = await new Model(testData, TestSchema, { immutable: true })
Validation:
-
Validate the entire Model.
const isValid = Model.validate(model)
-
Validate a single property of the model
const isValid = Model.validate(model.test, Model.schema(model).test)
Serialization:
API
Environments
- All modern browsers and Node 8+ without polyfills.
Future
- create benchmarks
- create contributing guide
- feature requests via issues
Contributing
We are currently drafting our contributing guide!
Changelog
v1.0.0
- moving out of development phase for production use
- removing build status badge since CircleCI is only used for PR temporary branches
v0.3.1
v0.3.0
- added
version
setter functionality to static function - updated documentation
v0.2.0
- added
sync
validation support during Model creation - updated documentation and dependencies
v0.1.1
- updated documentation and dependencies
v0.1.0