sequelize-json-schema
Advanced tools
Comparing version
{ | ||
"name": "sequelize-json-schema", | ||
"version": "2.1.0", | ||
"version": "2.1.1", | ||
"description": "Use your Sequelize models in JSON Schemas or Swagger", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -81,3 +81,3 @@ <!-- | ||
⇒ id: { type: 'integer', format: 'int32' }, | ||
⇒ name: { type: [ 'string', 'null' ] }, | ||
⇒ name: { type: [ 'string', 'null' ], maxLength: 255 }, | ||
⇒ createdAt: { type: 'string', format: 'date-time' }, | ||
@@ -104,3 +104,3 @@ ⇒ updatedAt: { type: 'string', format: 'date-time' } | ||
⇒ type: 'object', | ||
⇒ properties: { name: { type: [ 'string', 'null' ] } } | ||
⇒ properties: { name: { type: [ 'string', 'null' ], maxLength: 255 } } | ||
⇒ } | ||
@@ -133,3 +133,3 @@ ⇒ } | ||
⇒ properties: { | ||
⇒ name: { type: [ 'string', 'null' ] }, | ||
⇒ name: { type: [ 'string', 'null' ], maxLength: 255 }, | ||
⇒ Address: { '$ref': '#/definitions/Address' } | ||
@@ -142,3 +142,3 @@ ⇒ } | ||
⇒ street: { type: [ 'string', 'null' ], maxLength: 255 }, | ||
⇒ city: { type: [ 'string', 'null' ] }, | ||
⇒ city: { type: [ 'string', 'null' ], maxLength: 255 }, | ||
⇒ state: { type: [ 'string', 'null' ], maxLength: 2 }, | ||
@@ -171,3 +171,3 @@ ⇒ zipcode: { type: [ 'number', 'null' ] }, | ||
⇒ properties: { | ||
⇒ name: { type: [ 'string', 'null' ] }, | ||
⇒ name: { type: [ 'string', 'null' ], maxLength: 255 }, | ||
⇒ Address: { '$ref': '#/definitions/Address' } | ||
@@ -207,3 +207,3 @@ ⇒ } | ||
⇒ id: { type: 'integer', format: 'int32' }, | ||
⇒ name: { type: [ 'string', 'null' ] }, | ||
⇒ name: { type: [ 'string', 'null' ], maxLength: 255 }, | ||
⇒ createdAt: { type: 'string', format: 'date-time' }, | ||
@@ -226,3 +226,3 @@ ⇒ updatedAt: { type: 'string', format: 'date-time' }, | ||
⇒ id: { type: 'integer', format: 'int32' }, | ||
⇒ name: { type: [ 'string', 'null' ] }, | ||
⇒ name: { type: [ 'string', 'null' ], maxLength: 255 }, | ||
⇒ createdAt: { type: 'string', format: 'date-time' }, | ||
@@ -250,3 +250,3 @@ ⇒ updatedAt: { type: 'string', format: 'date-time' }, | ||
⇒ { type: [ 'string', 'null' ] } | ||
⇒ { type: [ 'string', 'null' ], maxLength: 255 } | ||
``` | ||
@@ -253,0 +253,0 @@ |
@@ -7,2 +7,3 @@ // Common types. These should never be exposed directly but, instead, be cloned | ||
const INTEGER = {type: 'integer'}; | ||
const NULL = {type: 'null'}; | ||
const NUMBER = {type: 'number'}; | ||
@@ -20,29 +21,6 @@ const OBJECT = {type: 'object'}; | ||
/** | ||
* Add/remove `null` type from an property schema. This will switch `type` | ||
* between an array and a single value, depending on the # of types. | ||
* | ||
* @param {Object} prop property schema | ||
* @param {Boolean} allowNull true = add null type, false = remove null type | ||
*/ | ||
function allowNullType(prop, allowNull = true) { | ||
// Sanity check that this is a property schema | ||
if (!prop.type) throw Error('Attribute `type` not defined'); | ||
const hasNull = Array.isArray(prop.type) ? | ||
prop.type.includes('null') : | ||
prop.type === 'null'; | ||
if (hasNull !== allowNull) { | ||
if (allowNull) { | ||
// Convert to array | ||
if (!Array.isArray(prop.type)) prop.type = [prop.type]; | ||
prop.type.push('null'); | ||
} else { | ||
prop.type = prop.type.filter(t => t !== 'null'); | ||
if (prop.type.length === 1) prop.type = prop.type[0]; | ||
} | ||
} | ||
return prop; | ||
// Naive utility for detecting empty objects | ||
function _isEmpty(obj) { | ||
for (const k in obj) return false; | ||
return true; | ||
} | ||
@@ -56,2 +34,41 @@ | ||
// Naive utility for adding a type to schema.type | ||
function _addType(schema, type = 'null') { | ||
// Empty schemas always validate | ||
if (_isEmpty(schema)) return schema; | ||
if (!schema.type) throw Error('schema.type not defined'); | ||
// Gather types and add type | ||
const types = new Set(Array.isArray(schema.type) ? schema.type : [schema.type]); | ||
types.add(type); | ||
// Update type field | ||
schema.type = types.size > 1 ? [...types] : [...types][0]; | ||
return schema; | ||
} | ||
// Naive utility for removing a type to schema.type | ||
function _removeType(schema, type = 'null') { | ||
if (!schema.type) throw Error('schema.type not defined'); | ||
// Gather types and remove type | ||
const types = new Set(Array.isArray(schema.type) ? schema.type : [schema.type]); | ||
types.delete(type); | ||
// Note: Technically an empty type field is permissable, but the semantics of | ||
// that are complicated. Is schema empty (always validates)? Is it using one | ||
// of the combining properties (anyOf, oneOf, allOf, not)? Getting this wrong | ||
// (e.g. producing an empty schema that always validates) could lead to | ||
// security vulenerabilities. So we just throw here to force callers to | ||
// figure this out. | ||
if (types.size <= 0) throw Error('schema.type must have at least one type'); | ||
// Update type field | ||
schema.type = types.size > 1 ? [...types] : [...types][0]; | ||
return schema; | ||
} | ||
/** | ||
@@ -80,2 +97,3 @@ * Generate JSON schema for a Sequelize attribute | ||
case 'VIRTUAL': { | ||
if (!att.type.returnType) throw Error(`No type defined for VIRTUAL field "${att.field}"`); | ||
return getAttributeSchema({...att, type: att.type.returnType}); | ||
@@ -130,7 +148,8 @@ } | ||
schema = {...STRING}; | ||
let length = att.type.options && att.type.options.length; | ||
// Resolve aliases | ||
// Include max char length if available | ||
let length = att.type._length || (att.type.options && att.type.options.length); | ||
length = STRING_LENGTHS[length] || length; | ||
if (length) schema.maxLength = length; | ||
break; | ||
@@ -149,6 +168,6 @@ } | ||
// differently. | ||
if (!schema) schema = {...ANY}; | ||
if (!schema) schema = {type: {...ANY}}; | ||
// Add 'null' type? | ||
if (att.allowNull !== false) allowNullType(schema, att.allowNull); | ||
// Add null? (Sequelize allowNull defaults to true) | ||
if (att.allowNull !== false) schema = _addType(schema, 'null'); | ||
@@ -179,3 +198,3 @@ return schema; | ||
if (options.allowNull) { | ||
throw Error('`allowNull` option is no longer supported (Use sjs.allowNullType(property[, allowNull]) to add/remove null types in the returned schema)'); | ||
throw Error('`allowNull` option is no longer supported'); | ||
} | ||
@@ -182,0 +201,0 @@ |
19097
4.73%222
5.21%