model-json-js v1.0.6
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.
// JSON Schema
const TestSchema = {
title: 'Test',
$id: 'https://hiveframework.io/api/v1/models/Test',
type: 'object',
properties: {
test: {
type: 'string'
},
another: {
type: 'string'
}
},
required: ['test']
}
// model FSA
const testData = {
type: 'Test',
payload: { test: 'object' }
}
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
Serialize the Model with JSON stringify.
const json = JSON.stringify(model)
API
Environments
- All modern browsers and Node 10+ without polyfills.
Future
- create contributing guide
- feature requests via issues
Contributing
We are currently drafting our contributing guide in the new monorepo!