I need help with SimpleSchema and aldeed:autoform ... please

So, we have an important JavaScript library that has nothing to do with meteor. We spend much time working on this library and we release different versions to a CDN – when it is downloaded by a website, it must be given a JSON config.

This JSON config is constructed/edited in a UI that is within a meteor app, and this JSON config must be validated.

The library that consumes the config will validate the JSON that it is given by using a SimpleSchema that is created using an NPM package called node-simple-schema, which is a fork of aldeed:simple-schema and which you can read about here: https://www.npmjs.com/package/node-simple-schema

I quote:

This is a fork of meteor-simple-schema that removes the “Meteor” parts, and is usable as a standard npm package in a regular node environment. The goal is to enable non-meteor node applications to validate objects against the same model schemas written for meteor applications.

Since we have used simple-schema to validate our JSON config in our library, we would also like to use it in the UI in the meteor app to validate the configs that we build before we send them over.

Also, a fantastic benefit of using a schema is that we can then generate a very helpful HTML form instead of having our users edit JSON directly. This is where aldeed:autoform comes in

Ideally, we would construct a SimpleSchema once and use it in two places: our meteor app, and our other library.

In order to modularize our SimpleSchema so that it can be re-used in two places, we have it in its own code base, which is an NPM package which we have named: our_private_npm_package – this NPM packages has a dependency on node-simple-schema. Now our CDN library can depend on our_private_npm_package, and our meteor app can also use it, since meteor apps can now use NPM packages!

All we have to do to get our config schema is:

import config_schema from 'our_private_npm_package';

However, I run into a lot of trouble when I try to use aldeed:autoform because aldeed autoform monkey-patches aldeed:simple-schema – I discovered this by poking around inside aldeed_autoform.js in my meteor app and seeing this:

// Extend SS for now; TODO put this in SS 
if (typeof SimpleSchema.prototype.getAllowedValuesForKey !== 'function') { 
    SimpleSchema.prototype.getAllowedValuesForKey = function (key) {
        var defs = this.getDefinition(key, ['type', 'allowedValues']);
        // For array fields, `allowedValues` is on the array item definition
        if (defs.type === Array) {
            defs = this.getDefinition(key+".$", ['allowedValues']);
        return defs.allowedValues;

So the upshot is that I cannot use aldeed:autoform to auto-generate forms from my schema because aldeed:autoform expects a monkey-patched version, which doesn’t come with the NPM version. It is expecting any SimpleSchema objects within Meteor to have come through aldeed:simple-schema, not through our own private NPM package. But if we do that, we cannot re-use the code in our other important JS library.

Any ideas? Is there something simpler I can do? Thanks much!


I briefly had a glimmer of hope when I came across “dynamic imports” in webpack ( https://webpack.github.io/docs/context.html and http://stackoverflow.com/questions/30575060/require-js-files-dynamically-on-runtime-using-webpack ), but apparently it is only dynamic between files available at compile time – I would like my npm package to be able to dynamically choose (at runtime) where to load SimpleSchema from, but I’m not sure that’s possible.