Skip to content

ralusek/jsonschema-to-mobx-state-tree

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.
});

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published