1.0.1 • Published 4 years ago

into-json v1.0.1

Weekly downloads
2
License
ISC
Repository
github
Last release
4 years ago

IntoJSON

Purpose

Convert to JSON based on jsonschema. the jsonschema based on draft4.

Install

npm i into-json -S

UseAge

Simple

import IntoJSON from 'into-json'
const data = null
const schema = {
  type: 'object',
  properties: {
    name: { type: 'string' },
    value: { type: 'number' },
    children: {
      type: 'array'
    }
  }
}
const value = IntoJSON(data, schema)

console.log(value)
// Output
{
  name: '',
  value: null,
  children: []
}

Complex example, can listen for error

const saveErrors = function(errors) {
  <!-- record the errors to server -->
}
const value = IntoJSON(data, schema, true, true, saveErrors)

Options

argumentdescriptiontypedefault
datathe data want to convert.any-
schemavalidator with the jsonschema.JSON-
isConsolethe data want to convert.anytrue
isRecordthe data want to convert.anyfalse
callbackthe data want to convert.any-