Socket
Socket
Sign inDemoInstall

model-json-js

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

model-json-js

A JavaScript Model class that implements a serializable solution for data modeling on the client and/or server.


Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

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

Keywords

FAQs

Package last updated on 22 Dec 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc