sequelize-json-schema
Advanced tools
Comparing version
{ | ||
"name": "sequelize-json-schema", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Use your Sequelize models in JSON Schemas or Swagger", | ||
@@ -31,2 +31,3 @@ "keywords": [ | ||
"mocha": "^2.5.3", | ||
"mocha-junit-reporter": "^1.12.1", | ||
"sequelize": ">= 3", | ||
@@ -33,0 +34,0 @@ "sqlite3": "^3.1.4" |
# sequelize-json-schema | ||
[](https://circleci.com/gh/chaliy/sequelize-json-schema) | ||
Use your Sequelize models in JSON Schemas or Swagger | ||
@@ -4,0 +6,0 @@ |
@@ -13,2 +13,3 @@ 'use strict'; | ||
let type = attribute.type; | ||
if (type instanceof Sequelize.ENUM) return enumProperty(attribute); | ||
@@ -45,3 +46,14 @@ if (type instanceof Sequelize.BOOLEAN) return { type: 'boolean' }; | ||
|| type instanceof Sequelize.TIME) { | ||
return { type: 'string' }; | ||
const schema = { | ||
type: 'string' | ||
}; | ||
if (type.options && type.options.length) { | ||
schema.maxLength = type.options.length; | ||
} else { | ||
schema.maxLength = type._length; | ||
} | ||
return schema; | ||
} | ||
@@ -70,14 +82,26 @@ | ||
return { type: 'any' }; | ||
} | ||
}; | ||
let modelProperties = (model, options) => { | ||
/** | ||
* Generates JSON Schema by specified Sequelize Model | ||
* @constructor | ||
* @param {object} model - The Sequelize Model | ||
* @param {objecct} options - Optional options | ||
*/ | ||
module.exports = (model, options) => { | ||
options = options || {}; | ||
const schema = { | ||
type: 'object', | ||
properties: {}, | ||
required: [] | ||
}; | ||
let exclude = options.exclude || options.private || []; | ||
let attributes = options.attributes || Object.keys(model.rawAttributes); | ||
let attributes = options.attributes || Object.keys(model.rawAttributes); | ||
let properties = {}; | ||
for(let attributeName of attributes){ | ||
for(let attributeName of attributes) { | ||
if (exclude.indexOf(attributeName) >= 0){ | ||
if (exclude.indexOf(attributeName) >= 0) { | ||
continue; | ||
@@ -89,20 +113,10 @@ } | ||
if (attribute) { | ||
properties[attributeName] = property(attribute); | ||
schema.properties[attributeName] = property(attribute); | ||
if (false === attribute.allowNull) { | ||
schema.required.push(attributeName); | ||
} | ||
} | ||
} | ||
return properties; | ||
} | ||
/** | ||
* Generates JSON Schema by specified Sequelize Model | ||
* @constructor | ||
* @param {object} model - The Sequelize Model | ||
* @param {objecct} options - Optional options | ||
*/ | ||
module.exports = (model, options) => { | ||
return { | ||
type: 'object', | ||
properties: modelProperties(model, options) | ||
}; | ||
return schema; | ||
}; |
@@ -82,2 +82,45 @@ 'use strict'; | ||
it('should add required for non-null columns', () => { | ||
let Simple = sequelize.define('simple', { | ||
title: Sequelize.STRING, | ||
password: { | ||
allowNull: false, | ||
type: Sequelize.STRING | ||
}, | ||
secret: Sequelize.INTEGER | ||
}); | ||
let def = definition(Simple); | ||
expect(def.properties.title).to.exist; | ||
expect(def.properties.password).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.not.contain('title'); | ||
expect(def.required).to.contain('password'); | ||
expect(def.required).to.not.contain('secret'); | ||
expect(def.properties.secret).to.exist; | ||
}); | ||
it('should specify string length', () => { | ||
let Simple = sequelize.define('simple', { | ||
title: Sequelize.STRING, | ||
password: { | ||
type: Sequelize.STRING(100) | ||
}, | ||
secret: Sequelize.STRING(40) | ||
}); | ||
let def = definition(Simple); | ||
expect(def.properties.title).to.exist; | ||
expect(def.properties.title.maxLength).to.equal(255); | ||
expect(def.properties.password).to.exist; | ||
expect(def.properties.password.maxLength).to.equal(100); | ||
expect(def.properties.secret).to.exist; | ||
expect(def.properties.secret.maxLength).to.equal(40); | ||
}); | ||
}); | ||
@@ -84,0 +127,0 @@ |
Sorry, the diff of this file is not supported yet
11542
22.03%8
14.29%186
32.86%94
2.17%5
25%