Converts loopback models into a json schema model file
npm install loopback-json-schema-builder
This module can create json schema objects based on loopback models.
This is useful if you want to quickly create forms for your loopback models using something
like angular schema form.
node . (path to loopback root)/server/server.js
returns to console out a map of all schema models
###loadAllModels(app, options)
loads schema for all loopback models associated with the given loopback app. Options is currently unused.
returns a map of model name to model schema
###loadModel(model)
returns a jsonSchema
``
var schemaBuilder = require('loopback-json-schema-builder');
module.exports = function (BaseModel) {
...
BaseModel.jsonSchema = function(cb) {
const schema = schemaBuilder.loadModel(BaseModel.modelName, this);
cb(null, JSON.stringify(schema, null, 2));
}
BaseModel.setup = function() {
// We need to call the base class's setup method
BaseModel.base.setup.call(this);
var BaseModelInternal = this;
BaseModelInternal.remoteMethod(
'jsonSchema',
{
description: 'Get the json schema for the given loopback model.',
accessType: 'READ',
returns: {arg: 'schema', type: 'string', root: true},
isStatic: true,
http: {path: '/json-schema', verb: 'GET'}
}
);
}
...
}
``
NOTE: This module only does loopback models TO json schema. If you are looking at going from json schema TO loopback models, checkout this neat project loopback-jsonschema