Skip to content

Commit

Permalink
Programmatic model schema access (#2)* Adding schema property to mode…
Browse files Browse the repository at this point in the history
…ls* Updating readme with programmatic access
  • Loading branch information
chrisandrews7 committed May 11, 2018
1 parent b853b94 commit 323eb2e
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 68 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ Add the following configuration to `component-config.js` inside your loopback pr
}
```

### Programmatic access to the json schema

A property is added onto each model under `model.jsonSchema`

```js
// Model file
module.exports = function(Products) {
const jsonSchema = Products.jsonSchema;
//...
};
```

## References

http://json-schema.org/
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "loopback-jsonschema-generator",
"version": "1.0.1",
"version": "1.1.1",
"description": "Generates JSON schemas for your Loopback models",
"main": "dist",
"scripts": {
Expand Down
10 changes: 5 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ export default (app, opts) => {
});

each(app.models, (model) => {
model.jsonSchema = (cb) => {
const schema = generate(model, options);
cb(null, schema);
};
model.jsonSchema = generate(model, options);

model.getJsonSchema = cb =>
cb(null, model.jsonSchema);

model.remoteMethod(
'jsonSchema',
'getJsonSchema',
{
description: 'Get the json schema for the given loopback model.',
accessType: 'READ',
Expand Down
124 changes: 67 additions & 57 deletions test/e2e/test.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,69 @@
import request from 'supertest';
import { expect } from 'chai';

import app from './example/';

describe('e2e Test', () => {
let server;

const expectedJsonSchema = {
$schema: 'http://json-schema.org/draft-04/schema#',
title: 'Products',
type: 'object',
properties: {
id: {
type: 'number'
},
name: {
type: 'string',
title: 'Name',
required: true
},
price: {
type: 'number',
title: 'Price',
required: true
},
addedDate: {
type: 'string',
title: 'Added Date',
format: 'date-time'
},
inStock: {
type: 'boolean',
title: 'In Stock',
required: false
},
tags: {
title: 'Tags',
type: 'array',
items: {
type: 'string'
}
},
category: {
type: 'object',
title: 'Category',
required: true,
properties: {
id: {
type: 'number'
},
categoryName: {
type: 'string',
title: 'Category Name',
required: true
}
}
}
},
required: [
'name',
'price',
'category'
]
};

before((done) => {
server = app.listen(done);
});
Expand All @@ -16,62 +76,12 @@ describe('e2e Test', () => {
request(server)
.get('/api/products/test-json-schema')
.expect('Content-Type', /json/)
.expect(200, {
$schema: 'http://json-schema.org/draft-04/schema#',
title: 'Products',
type: 'object',
properties: {
id: {
type: 'number'
},
name: {
type: 'string',
title: 'Name',
required: true
},
price: {
type: 'number',
title: 'Price',
required: true
},
addedDate: {
type: 'string',
title: 'Added Date',
format: 'date-time'
},
inStock: {
type: 'boolean',
title: 'In Stock',
required: false
},
tags: {
title: 'Tags',
type: 'array',
items: {
type: 'string'
}
},
category: {
type: 'object',
title: 'Category',
required: true,
properties: {
id: {
type: 'number'
},
categoryName: {
type: 'string',
title: 'Category Name',
required: true
}
}
}
},
required: [
'name',
'price',
'category'
]
}, done);
.expect(200, expectedJsonSchema, done);
});

it('should return a schema property on the model with the json schema', () => {
expect(app.models.Products)
.to.have.property('jsonSchema')
.and.to.deep.equal(expectedJsonSchema);
});
});
17 changes: 12 additions & 5 deletions test/unit/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,24 @@ describe('Index', () => {
sandbox.restore();
});

it('should add a jsonSchema method onto every model', () => {
it('should expose a schema property with the jsonSchema on every model', () => {
sandbox.stub(schema, 'generate').returns('testSchemaProperty');
jsonSchema({ models: [model] });

expect(model.jsonSchema).to.equal('testSchemaProperty');
});

it('should add a getJsonSchema method onto every model', () => {
jsonSchema({ models: [model] });
/* eslint-disable no-unused-expressions */
expect(model.jsonSchema).to.be.ok;
expect(model.getJsonSchema).to.be.ok;
});

it('should call the generate json schema function with the correct arguements on call of jsonSchema', (done) => {
const spy = sandbox.spy(schema, 'generate');
jsonSchema({ models: [model] });

model.jsonSchema(() => {
model.getJsonSchema(() => {
const call = spy.getCall(0);
expect(call.args[0]).to.deep.equal(model);
done();
Expand All @@ -43,7 +50,7 @@ describe('Index', () => {
sandbox.stub(schema, 'generate').returns('testSchema');
jsonSchema({ models: [model] });

model.jsonSchema((err, result) => {
model.getJsonSchema((err, result) => {
expect(err).to.equal(null);
expect(result).to.equal('testSchema');
done();
Expand All @@ -55,7 +62,7 @@ describe('Index', () => {

const args = model.remoteMethod.getCall(0).args;

expect(args[0]).to.equal('jsonSchema');
expect(args[0]).to.equal('getJsonSchema');
expect(args[1].isStatic).to.equal(true);
expect(args[1].accessType).to.equal('READ');
expect(args[1].returns.root).to.equal(true);
Expand Down

0 comments on commit 323eb2e

Please sign in to comment.