Skip to content

Infrastructure components to easily process JSON schema and build models from it

Notifications You must be signed in to change notification settings

kristianmandrup/json-schema-model-builder

Repository files navigation

JSON Schema model builder

Infrastructure components to traverse, build models and cause side effects based on a JSON Schema.

This can be leveraged to build a Declarative Driven Development framework where you build most of your app from a single source of truth (enriched JSON schemas) and keep it in sync on every update.

Status

See Building Blocks for more details

Please check the tests and markdown *.md documentation files for in-depth API documentation (Note: needs update).

Quick start

  • npm: npm install json-schema-model-builder -S
  • yarn: yarn add json-schema-model-builder

Use

You can use it something like this...

import { schema } from "./schemas/person.json";
import { config } from "./config";
import {
  createBuilder,
  createState,
  createDispatcher
} from "json-schema-model-builder";

const state = createState({ config });
const dispatcher = createDispatcher({ state, config });
const builder = createBuilder({ state, schema, dispatcher, config });
const built = builder.build();
const rendered = built.render();

// Render the darn thing!
console.log({ rendered });

Note: The Builder and Renderer still need some work, but you can leverage and extend their current implementation already.

Design & Architecture

This project is a result of multiple efforts trying to generate source code or derive models from JSON schemas. There is a need for a framework with a set of well tested, battle-tested building blocks to make this much easier. The goal is to achieve true declarative model driven development.

Your domain and action models across your stack should automatically be derived from the same underlying declarative model (project blueprint).

In the near future we will likely add interactive graph driven development to the mix, using an approach like d3-force-graph-editor

You could in the end simply draw your full application as a directed graph and then assign sufficient meta data to nodes and edges. Then as you draw, have an asortment of pluggable generators generate most (if not all!) of the app for you!

As you reconfigure your graph your app would be auto-coded/generated to reflect the graph in real time!!

Ideally you would then sync the state of the graph with other developers in real time, either using something like AppSync or GraphQL backend or perhaps something like automerge-graph or Immer

Addional projects/resources

Main concepts

Infrastructure delivered by library

  • Builder builds a set of entities identified by resolvers and dispatches events as needed
  • Dispatcher Dispatches select events to state for update of model
  • State builds collections of entities and a directed graph from incoming model events

Your concerns for output target

  • Model: target model to be built from state can either subscribe to the State or process the State when builder has finished
  • Renderer Rendering can be done when target model has been built

The following examples act to demonstrate how you might go about using this infrastructure to generate source code or models.

Usage

The types collected, such as type, enum etc are collected in the store entry of the config object passed in.

const config = {};
const result = createSchema({ schema, config });
const { store } = config;
console.log(store);

Examples

Note: As soon as we have a solid foundation and working examples we will move these into their own projects and publish them on npm

Testing

Uses jest for unit testing.

Please help add more/better test coverage :)

Run tests in watch mode

$ npm run test:watch

Run all tests

$ npm run test

TODO

  • State subscribers

Author

2018 Kristian Mandrup (CTO@Tecla5)

License

MIT

About

Infrastructure components to easily process JSON schema and build models from it

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages