sequelize-json-schema
Advanced tools
Comparing version
{ | ||
"name": "sequelize-json-schema", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Use your Sequelize models in JSON Schemas or Swagger", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
# sequelize-json-schema | ||
[](https://npmjs.org/package/sequelize-json-schema) | ||
[](https://circleci.com/gh/chaliy/sequelize-json-schema) | ||
@@ -9,2 +10,13 @@ | ||
## Installation | ||
This is a [Node.js](https://nodejs.org/en/) module available through the | ||
[npm registry](https://www.npmjs.com/). Installation is done using the | ||
[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally): | ||
```bash | ||
$ npm install sequelize-json-schema | ||
``` | ||
## Example | ||
@@ -11,0 +23,0 @@ |
@@ -11,22 +11,24 @@ 'use strict'; | ||
let property = attribute => { | ||
let property = (attribute, options) => { | ||
let type = attribute.type; | ||
let addNull = attribute.allowNull && options.allowNull | ||
if (type instanceof Sequelize.ENUM) return enumProperty(attribute); | ||
if (type instanceof Sequelize.BOOLEAN) return { type: 'boolean' }; | ||
if (type instanceof Sequelize.INTEGER) return { type: 'integer', format: 'int32' }; | ||
if (type instanceof Sequelize.BIGINT) return { type: 'integer', format: 'int64' }; | ||
if (type instanceof Sequelize.BOOLEAN) return { type: addNull ? ['boolean', 'null'] : 'boolean' }; | ||
if (type instanceof Sequelize.INTEGER) return { type: addNull ? ['integer', 'null'] : 'integer', format: 'int32' }; | ||
if (type instanceof Sequelize.BIGINT) return { type: addNull ? ['integer', 'null'] : 'integer', format: 'int64' }; | ||
if (type instanceof Sequelize.FLOAT | ||
|| type instanceof Sequelize.REAL) { | ||
return { type: 'number', format: 'float' }; | ||
return { type: addNull ? ['number', 'null'] : 'number', format: 'float' }; | ||
} | ||
if (type instanceof Sequelize.DOUBLE) { return { type: 'number', format: 'double' }; } | ||
if (type instanceof Sequelize.DOUBLE) { return { type: addNull ? ['number', 'null'] : 'number', format: 'double' }; } | ||
if (type instanceof Sequelize.DECIMAL) { return { type: 'number' }; } | ||
if (type instanceof Sequelize.DECIMAL) { return { type: addNull ? ['number', 'null'] : 'number' }; } | ||
if (type instanceof Sequelize.DATEONLY) { return { type: 'string', format: 'date' }; } | ||
if (type instanceof Sequelize.DATE) { return { type: 'string', format: 'date-time' }; } | ||
if (type instanceof Sequelize.TIME) { return { type: 'string' }; } | ||
if (type instanceof Sequelize.DATEONLY) { return { type: addNull ? ['string', 'null'] : 'string', format: 'date' }; } | ||
if (type instanceof Sequelize.DATE) { return { type: addNull ? ['string', 'null'] : 'string', format: 'date-time' }; } | ||
if (type instanceof Sequelize.TIME) { return { type: addNull ? ['string', 'null'] : 'string'}; } | ||
@@ -36,3 +38,3 @@ if (type instanceof Sequelize.UUID | ||
|| type instanceof Sequelize.UUIDV4) { | ||
return { type: 'string', format: 'uuid' }; | ||
return { type: addNull ? ['string', 'null'] : 'string', format: 'uuid' }; | ||
} | ||
@@ -48,3 +50,3 @@ | ||
const schema = {type: 'string'}; | ||
const schema = {type: addNull ? ['string', 'null'] : 'string'}; | ||
@@ -74,3 +76,3 @@ var maxLength = (type.options && type.options.length) || type._length; | ||
if (type instanceof Sequelize.VIRTUAL) { | ||
return type.returnType ? property({ type: type.returnType }) : { type: 'string' }; | ||
return type.returnType ? property({ type: type.returnType, allowNull: type.allowNull }, options) : { type: addNull ? ['string', 'null'] : 'string'}; | ||
} | ||
@@ -120,4 +122,4 @@ | ||
if (attribute) { | ||
schema.properties[attributeName] = property(attribute); | ||
if (false === attribute.allowNull) { | ||
schema.properties[attributeName] = property(attribute, options); | ||
if (false === attribute.allowNull || options.alwaysRequired) { | ||
schema.required.push(attributeName); | ||
@@ -124,0 +126,0 @@ } |
@@ -106,2 +106,51 @@ 'use strict'; | ||
it('should add null type if option allowNull turned on', () => { | ||
let Simple = sequelize.define('simple', { | ||
title: Sequelize.STRING, | ||
password: { | ||
allowNull: true, | ||
type: Sequelize.STRING | ||
} | ||
}); | ||
let def = definition(Simple, { | ||
allowNull: true | ||
}); | ||
expect(def.properties.title).to.exist; | ||
expect(def.required).to.be.an('array'); | ||
expect(def.required).to.contain('id'); | ||
expect(def.required).to.contain('createdAt'); | ||
expect(def.required).to.contain('updatedAt'); | ||
expect(def.required).not.to.contain('title'); | ||
expect(def.required).not.to.contain('password'); | ||
expect(def.properties.password.type).to.eql(['string', 'null']) | ||
expect(def.properties.title.type).to.eql('string') | ||
}) | ||
it('should add to required if option allowNull and alwaysRequired turned on', () => { | ||
let Simple = sequelize.define('simple', { | ||
title: Sequelize.STRING, | ||
password: { | ||
allowNull: true, | ||
type: Sequelize.STRING | ||
} | ||
}); | ||
let def = definition(Simple, { | ||
allowNull: true, | ||
alwaysRequired: true | ||
}); | ||
expect(def.properties.title).to.exist; | ||
expect(def.required).to.be.an('array'); | ||
expect(def.required).to.contain('id'); | ||
expect(def.required).to.contain('createdAt'); | ||
expect(def.required).to.contain('updatedAt'); | ||
expect(def.required).to.contain('title'); | ||
expect(def.required).to.contain('password'); | ||
expect(def.properties.password.type).to.eql(['string', 'null']) | ||
expect(def.properties.title.type).to.eql('string') | ||
}) | ||
it('should specify string length', () => { | ||
@@ -108,0 +157,0 @@ let Simple = sequelize.define('simple', { |
15125
21.7%242
22.22%106
12.77%