Coder Social home page Coder Social logo

metamodel's Introduction

Meta Model for JavaScript Apps

Coming from statically typed languages like Pascal, C and C++, I always wanted to have more strict typings for my JavaScript classes. While TypeScript allows me to describe the data using interfaces, I feel it lacks RTTI -- runtime information about the objects involved that would allow me to construct user interfaces dynamically and validate incoming data (either from some remote connection or typed in by a user) against the type information.

This is my attempt at building something to fill this gap.

A metamodel is created like this:

var sampleObject = {
    lala: 12,
    blah: "Some String",
    blub: 3.1415 * 12
};

var modelTypes = require("metamodel").modelTypes;

var model = modelTypes.addObjectType('sample')
  .addItem('lala', modelTypes.type('number/int'))
  .addItem('blah', modelTypes.type('string'))
  .addItem('blub', modelTypes.type('number'));

Once the model exists it in the modelTypes registry, it can always be referred to by name:

var model = modelTypes.type('sample');

And input data can be validated by the model:

var inputData = {
    lala: "12",
    blah: "Another String",
    blub: 27.12
};

var context = modelTypes.createParseContext(inputData, model);
context.allowConversion = true;
model.validate(context);
if (context.warnings.length) {
    console.warn(`context.messages.map(e => e.msg).join(', ')`);
}
if (context.errors.length) {
    console.error(`context.errors.map(e => e.msg).join(', ')`);
    throw new Error('Validation failed');
}

At this point, context will contain warnings and errors if the data does not fit the metamodel. With allowConversion=true, the metamodel will parse "12" into a number, if allowConversion=false, the string value will result in an error message.

metamodel's People

Contributors

dependabot[bot] avatar hn3000 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

metamodel's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.