@hn3000/metamodel
Meta Model for TypeScript and EcmaScript apps, to help with JSON schema processing and UI construction.
Last updated 6 months ago by hn3000 .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @hn3000/metamodel 
SYNC missed versions from official npm registry.

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.

Current Tags

  • 1.8.1                                ...           latest (6 months ago)

44 Versions

  • 1.8.1                                ...           6 months ago
  • 1.8.0                                ...           6 months ago
  • 1.7.1                                ...           a year ago
  • 1.7.0                                ...           a year ago
  • 1.6.6                                ...           a year ago
  • 1.6.5                                ...           a year ago
  • 1.6.4                                ...           a year ago
  • 1.6.3                                ...           a year ago
  • 1.6.2                                ...           a year ago
  • 1.6.1                                ...           a year ago
  • 1.6.0                                ...           a year ago
  • 1.5.6                                ...           a year ago
  • 1.5.5                                ...           a year ago
  • 1.5.4                                ...           a year ago
  • 1.5.3                                ...           a year ago
  • 1.5.2                                ...           2 years ago
  • 1.5.1                                ...           2 years ago
  • 1.5.0                                ...           2 years ago
  • 1.4.1                                ...           2 years ago
  • 1.4.0                                ...           2 years ago
  • 1.3.2                                ...           2 years ago
  • 1.3.1                                ...           2 years ago
  • 1.3.0                                ...           2 years ago
  • 1.2.8                                ...           2 years ago
  • 1.2.7                                ...           2 years ago
  • 1.2.6                                ...           2 years ago
  • 1.2.5                                ...           2 years ago
  • 1.2.4                                ...           2 years ago
  • 1.2.3                                ...           2 years ago
  • 1.2.2                                ...           2 years ago
  • 1.2.1                                ...           2 years ago
  • 1.2.0                                ...           2 years ago
  • 1.0.4                                ...           2 years ago
  • 1.0.3                                ...           2 years ago
  • 1.0.2                                ...           2 years ago
  • 1.0.1                                ...           2 years ago
  • 1.0.0                                ...           2 years ago
  • 0.11.2                                ...           3 years ago
  • 0.11.1                                ...           3 years ago
  • 0.11.0                                ...           3 years ago
  • 0.10.3                                ...           3 years ago
  • 0.10.2                                ...           3 years ago
  • 0.10.1                                ...           3 years ago
  • 0.9.1                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 44
This Month 46
Last Day 44
Last Week 1
Last Month 1
Dependencies (2)
Dev Dependencies (6)

Copyright 2014 - 2017 © taobao.org |