diff --git a/lib/helpers.js b/lib/helpers.js index 9406860..6963e8c 100755 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -83,10 +83,13 @@ const hasProperty = Function.prototype.call.bind( Object.prototype.hasOwnProperty, ); +const readConstraint = constraint => (Array.isArray(constraint) ? constraint[0] : constraint); + module.exports = { findPath, ensurePath, compose, idX, hasProperty, + readConstraint, }; diff --git a/lib/types.js b/lib/types.js index 10118c8..d4d4a7e 100755 --- a/lib/types.js +++ b/lib/types.js @@ -1,5 +1,6 @@ const config = require('./config'); const [FIELDS_MAPPING] = require('./options/fieldOptionsMapping'); +const { readConstraint } = require('./helpers'); module.exports = { simpleType_jsonSchema, @@ -106,28 +107,24 @@ function mixed_jsonSchema(name) { return checkNullable(result); } +const transformMatch = match => ( + match instanceof RegExp + ? match.toString().split('/').slice(1, -1).join('/') + : match.toString() +); + function __processOptions(t, type) { - t.__required = !!type.required; + t.__required = !!readConstraint(type.required); if (type.enum && type.enum.slice) t.enum = type.enum.slice(); if (type.ref) t['x-ref'] = type.ref; - if (type.min != null) t.minimum = type.min; - if (type.max != null) t.maximum = type.max; - if (type.minLength != null) t.minLength = type.minLength; - if (type.maxLength != null) t.maxLength = type.maxLength; - if (type.minlength != null) t.minLength = type.minlength; - if (type.maxlength != null) t.maxLength = type.maxlength; + if (type.min != null) t.minimum = readConstraint(type.min); + if (type.max != null) t.maximum = readConstraint(type.max); + if (type.minLength != null) t.minLength = readConstraint(type.minLength); + if (type.maxLength != null) t.maxLength = readConstraint(type.maxLength); + if (type.minlength != null) t.minLength = readConstraint(type.minlength); + if (type.maxlength != null) t.maxLength = readConstraint(type.maxlength); if (type.examples != null) t.examples = type.examples; - if (type.match) { - if (type.match instanceof RegExp) { - t.pattern = type.match - .toString() - .split('/') - .slice(1, -1) - .join('/'); - } else { - t.pattern = type.match.toString(); - } - } + if (type.match != null) t.pattern = transformMatch(readConstraint(type.match)); if (type.default !== undefined) t.default = type.default; t.description = type.description || type.descr || type.ref && `Refers to ${type.ref}`; diff --git a/test/suites/translation.test.js b/test/suites/translation.test.js index db76388..a53043e 100755 --- a/test/suites/translation.test.js +++ b/test/suites/translation.test.js @@ -578,6 +578,61 @@ describe('schema.jsonSchema', () => { }); }); + it('should correctly translate number value constaints with error messages', () => { + const mSchema = new Schema({ + value: { + type: Number, + min: [-5, 'Value shoule be greater or equal to -5'], + max: [50, 'Value should be less or equal to 50'], + required: [true, 'Value should be specified'], + default: 0, + }, + }, { id: false, _id: false }); + + const jsonSchema = mSchema.jsonSchema('Sample'); + + assert.deepEqual(jsonSchema, { + title: 'Sample', + type: 'object', + properties: { + value: { + type: 'number', + minimum: -5, + maximum: 50, + default: 0, + }, + }, + required: ['value'], + }); + }); + + it('should correctly translate number value constaints with error messages (not requires)', () => { + const mSchema = new Schema({ + value: { + type: Number, + min: [-5, 'Value shoule be greater or equal to -5'], + max: [50, 'Value should be less or equal to 50'], + required: [false, 'Value is not required'], + default: 0, + }, + }, { id: false, _id: false }); + + const jsonSchema = mSchema.jsonSchema('Sample'); + + assert.deepEqual(jsonSchema, { + title: 'Sample', + type: 'object', + properties: { + value: { + type: 'number', + minimum: -5, + maximum: 50, + default: 0, + }, + }, + }); + }); + it('should correctly translate string value constaints', () => { const mSchema = new Schema({ valueFromList: { @@ -617,6 +672,45 @@ describe('schema.jsonSchema', () => { }); }); + it('should correctly translate string value constaints with error message', () => { + const mSchema = new Schema({ + valueFromList: { + type: String, + enum: ['red', 'green', 'yellow'], + required: true, + }, + value20_30: { + type: String, + minLength: [20, 'Value should have at least 20 characters'], + maxLength: [30, 'Value should not be longer then 30 characters'], + }, + value: { type: String, match: [/^(?:H|h)ello, .+$/, 'Value should start from greating'] }, + }, { id: false, _id: false }); + + const jsonSchema = mSchema.jsonSchema('Sample'); + + assert.deepEqual(jsonSchema, { + title: 'Sample', + type: 'object', + properties: { + valueFromList: { + type: 'string', + enum: ['red', 'green', 'yellow'], + }, + value20_30: { + type: 'string', + minLength: 20, + maxLength: 30, + }, + value: { + type: 'string', + pattern: '^(?:H|h)ello, .+$', + }, + }, + required: ['valueFromList'], + }); + }); + it('should correctly translate string value constaints (minlength and maxlength)', () => { const mSchema = new Schema({ valueFromList: {