1.0.6 • Published 4 years ago

model-json-js v1.0.6

Weekly downloads
16
License
MIT
Repository
github
Last release
4 years ago

Model(JSON)js

NPM Version License Code Coverage JavaScript Style Guide

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!

Changelog

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.0

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.0

6 years ago