JSPM

jsonschema-to-mobx-state-tree

1.0.5
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q15945F
  • License MIT

Utility for converting from jsonschema standard to Mobx State Tree types.

Package Exports

  • jsonschema-to-mobx-state-tree

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (jsonschema-to-mobx-state-tree) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Utility for converting JSON Schema to MobX State Tree types.

JSON Schema: http://json-schema.org/

MobX State Tree Types: https://github.com/mobxjs/mobx-state-tree#types-overview

Use Case:

Let's say we had an event schema that looked like this:

{
  type: 'object',
  title: 'Event',
  properties: {
    title: {
      type: 'string'
    },
    public: {
      type: 'boolean',
      default: false
    },
    time: {
      type: 'object',
      properties: {
        start: {
          type: 'string',
          format: 'datetime'
        },
        end: {
          type: 'string',
          format: 'datetime'
        }
      },
      required: ['start']
    }
  },
  required: ['title', 'public']
}

Should output:

types.model('Event', {
  title: types.string,
  public: types.optional(type.boolean, false),
  time: types.maybe({
    start: types.Date,
    end: types.maybe(types.Date)
  })
});

Usage:

$ npm install --save jsonschema-to-mobx-state-tree

const { types } = require('mobx-state-tree');
const jsonSchemaToTypes = require('jsonschema-to-mobx-state-tree')(types);

jsonSchemaToTypes(myJSONSchema);

We can additionally pass on an onNode function to be called every node, whose response will be used in place of the node that would have been returned in the conversion.

jsonSchemaToTypes(myJSONSchema, (result, {node, meta, typeMap}) => {
  // Log out result, node, meta, and typeMap to see options.
});