Skip to content

Commit e7d9d99

Browse files
authored
Merge pull request #4 from JaredCE/make-a-factory
Make a factory
2 parents 0658143 + c3f1d0d commit e7d9d99

File tree

7 files changed

+108
-6
lines changed

7 files changed

+108
-6
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ To run:
7777

7878
```
7979
const jsonSchema = require('simple.json.schema')
80-
const Convertor = require('index')
80+
const ConvertorFactory = require('index')
8181
82-
const complexConvertor = new Convertor(complexOneOfSchema)
83-
const components = complexConvertor.convert()
82+
const convertedSchema = ConvertorFactory.convert(jsonSchema)
8483
```

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
'use strict'
2+
const Factory = require('./src/Factory')
3+
4+
module.exports = Factory

package-lock.json

Lines changed: 23 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json-schema-for-openapi",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Converts a regular JSON Schema to a compatible OpenAPI 3 Schema Object, extracting out $ref schemas to their own schema object",
55
"keywords": ["json", "json-schema", "openAPI", "openAPI-v3"],
66
"main": "index.js",

src/Factory.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict'
2+
3+
const Convertor = require('./Convertor')
4+
5+
class SchemaFactory {
6+
constructor() {}
7+
8+
static convert(schema) {
9+
const convertedSchema = new Convertor(schema)
10+
return convertedSchema.convert()
11+
}
12+
}
13+
14+
module.exports = SchemaFactory

test/src/Factory.spec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict'
2+
3+
const expect = require('chai').expect
4+
5+
const Factory = require('../../src/Factory')
6+
7+
const simpleSchema = require('../schemas/simple-one')
8+
const complexAllOfSchema = require('../schemas/complex-allOf')
9+
10+
describe('Factory', () => {
11+
beforeEach(function() {
12+
delete require.cache[require.resolve('../schemas/simple-one')];
13+
delete require.cache[require.resolve('../schemas/complex-allOf')];
14+
});
15+
16+
describe('convert', () => {
17+
it('should produce a converted schema as a factory', function() {
18+
let expected = Factory.convert(simpleSchema)
19+
expect(expected).to.be.an('object')
20+
expect(expected).to.have.property('schemas')
21+
expect(expected.schemas).to.have.property('main')
22+
expect(expected.schemas.main).to.not.have.property('$schema')
23+
24+
expected = Factory.convert(complexAllOfSchema)
25+
expect(expected).to.be.an('object')
26+
expect(expected).to.have.property('schemas')
27+
expect(expected.schemas).to.have.property('main')
28+
expect(expected.schemas.main).to.not.have.property('$schema')
29+
expect(expected.schemas).to.have.property('message')
30+
});
31+
});
32+
});

test/src/index.spec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict'
2+
3+
const expect = require('chai').expect
4+
5+
const Factory = require('../../index')
6+
7+
const simpleSchema = require('../schemas/simple-one')
8+
const complexAllOfSchema = require('../schemas/complex-allOf')
9+
10+
describe('index', () => {
11+
beforeEach(function() {
12+
delete require.cache[require.resolve('../schemas/simple-one')];
13+
delete require.cache[require.resolve('../schemas/complex-allOf')];
14+
});
15+
16+
describe('convert', () => {
17+
it('should produce a converted schema', function() {
18+
let expected = Factory.convert(simpleSchema)
19+
expect(expected).to.be.an('object')
20+
expect(expected).to.have.property('schemas')
21+
expect(expected.schemas).to.have.property('main')
22+
expect(expected.schemas.main).to.not.have.property('$schema')
23+
24+
expected = Factory.convert(complexAllOfSchema)
25+
expect(expected).to.be.an('object')
26+
expect(expected).to.have.property('schemas')
27+
expect(expected.schemas).to.have.property('main')
28+
expect(expected.schemas.main).to.not.have.property('$schema')
29+
expect(expected.schemas).to.have.property('message')
30+
});
31+
});
32+
});

0 commit comments

Comments
 (0)