Skip to content

Commit 323eb2e

Browse files
committed
Programmatic model schema access (#2)* Adding schema property to models* Updating readme with programmatic access
1 parent b853b94 commit 323eb2e

File tree

5 files changed

+97
-68
lines changed

5 files changed

+97
-68
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ Add the following configuration to `component-config.js` inside your loopback pr
8181
}
8282
```
8383

84+
### Programmatic access to the json schema
85+
86+
A property is added onto each model under `model.jsonSchema`
87+
88+
```js
89+
// Model file
90+
module.exports = function(Products) {
91+
const jsonSchema = Products.jsonSchema;
92+
//...
93+
};
94+
```
95+
8496
## References
8597

8698
http://json-schema.org/

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "loopback-jsonschema-generator",
3-
"version": "1.0.1",
3+
"version": "1.1.1",
44
"description": "Generates JSON schemas for your Loopback models",
55
"main": "dist",
66
"scripts": {

src/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ export default (app, opts) => {
88
});
99

1010
each(app.models, (model) => {
11-
model.jsonSchema = (cb) => {
12-
const schema = generate(model, options);
13-
cb(null, schema);
14-
};
11+
model.jsonSchema = generate(model, options);
12+
13+
model.getJsonSchema = cb =>
14+
cb(null, model.jsonSchema);
1515

1616
model.remoteMethod(
17-
'jsonSchema',
17+
'getJsonSchema',
1818
{
1919
description: 'Get the json schema for the given loopback model.',
2020
accessType: 'READ',

test/e2e/test.spec.js

Lines changed: 67 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,69 @@
11
import request from 'supertest';
2+
import { expect } from 'chai';
3+
24
import app from './example/';
35

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

9+
const expectedJsonSchema = {
10+
$schema: 'http://json-schema.org/draft-04/schema#',
11+
title: 'Products',
12+
type: 'object',
13+
properties: {
14+
id: {
15+
type: 'number'
16+
},
17+
name: {
18+
type: 'string',
19+
title: 'Name',
20+
required: true
21+
},
22+
price: {
23+
type: 'number',
24+
title: 'Price',
25+
required: true
26+
},
27+
addedDate: {
28+
type: 'string',
29+
title: 'Added Date',
30+
format: 'date-time'
31+
},
32+
inStock: {
33+
type: 'boolean',
34+
title: 'In Stock',
35+
required: false
36+
},
37+
tags: {
38+
title: 'Tags',
39+
type: 'array',
40+
items: {
41+
type: 'string'
42+
}
43+
},
44+
category: {
45+
type: 'object',
46+
title: 'Category',
47+
required: true,
48+
properties: {
49+
id: {
50+
type: 'number'
51+
},
52+
categoryName: {
53+
type: 'string',
54+
title: 'Category Name',
55+
required: true
56+
}
57+
}
58+
}
59+
},
60+
required: [
61+
'name',
62+
'price',
63+
'category'
64+
]
65+
};
66+
767
before((done) => {
868
server = app.listen(done);
969
});
@@ -16,62 +76,12 @@ describe('e2e Test', () => {
1676
request(server)
1777
.get('/api/products/test-json-schema')
1878
.expect('Content-Type', /json/)
19-
.expect(200, {
20-
$schema: 'http://json-schema.org/draft-04/schema#',
21-
title: 'Products',
22-
type: 'object',
23-
properties: {
24-
id: {
25-
type: 'number'
26-
},
27-
name: {
28-
type: 'string',
29-
title: 'Name',
30-
required: true
31-
},
32-
price: {
33-
type: 'number',
34-
title: 'Price',
35-
required: true
36-
},
37-
addedDate: {
38-
type: 'string',
39-
title: 'Added Date',
40-
format: 'date-time'
41-
},
42-
inStock: {
43-
type: 'boolean',
44-
title: 'In Stock',
45-
required: false
46-
},
47-
tags: {
48-
title: 'Tags',
49-
type: 'array',
50-
items: {
51-
type: 'string'
52-
}
53-
},
54-
category: {
55-
type: 'object',
56-
title: 'Category',
57-
required: true,
58-
properties: {
59-
id: {
60-
type: 'number'
61-
},
62-
categoryName: {
63-
type: 'string',
64-
title: 'Category Name',
65-
required: true
66-
}
67-
}
68-
}
69-
},
70-
required: [
71-
'name',
72-
'price',
73-
'category'
74-
]
75-
}, done);
79+
.expect(200, expectedJsonSchema, done);
80+
});
81+
82+
it('should return a schema property on the model with the json schema', () => {
83+
expect(app.models.Products)
84+
.to.have.property('jsonSchema')
85+
.and.to.deep.equal(expectedJsonSchema);
7686
});
7787
});

test/unit/index.spec.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,24 @@ describe('Index', () => {
2222
sandbox.restore();
2323
});
2424

25-
it('should add a jsonSchema method onto every model', () => {
25+
it('should expose a schema property with the jsonSchema on every model', () => {
26+
sandbox.stub(schema, 'generate').returns('testSchemaProperty');
27+
jsonSchema({ models: [model] });
28+
29+
expect(model.jsonSchema).to.equal('testSchemaProperty');
30+
});
31+
32+
it('should add a getJsonSchema method onto every model', () => {
2633
jsonSchema({ models: [model] });
2734
/* eslint-disable no-unused-expressions */
28-
expect(model.jsonSchema).to.be.ok;
35+
expect(model.getJsonSchema).to.be.ok;
2936
});
3037

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

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

46-
model.jsonSchema((err, result) => {
53+
model.getJsonSchema((err, result) => {
4754
expect(err).to.equal(null);
4855
expect(result).to.equal('testSchema');
4956
done();
@@ -55,7 +62,7 @@ describe('Index', () => {
5562

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

58-
expect(args[0]).to.equal('jsonSchema');
65+
expect(args[0]).to.equal('getJsonSchema');
5966
expect(args[1].isStatic).to.equal(true);
6067
expect(args[1].accessType).to.equal('READ');
6168
expect(args[1].returns.root).to.equal(true);

0 commit comments

Comments
 (0)