Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dsch] nullable types, stopped support for node < 8.x, and monggose v4.x #19

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/node_modules/
/locals/
/coverage/
/out/
node_modules
locals
coverage
out
coveralls.sh
.vscode
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
language: node_js
node_js:
- "4"
- "5"
- "6"
- "8"
- "9"
- "10"
- "12"

env:
- MONGOOSE_VERSION=4
- MONGOOSE_VERSION=5

install:
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var query_jsonSchema = require('./lib/query');
var types = require('./lib/types');

module.exports = exports = function(mongoose) {
// eslint-disable-next-line global-require
mongoose = mongoose || require('mongoose');
var Types = mongoose.Schema.Types;

Expand Down
8 changes: 4 additions & 4 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function model_jsonSchema(fields, populate, readonly) {

if (populate != null) {
jsonSchema = __populate.call(this, jsonSchema, populate);
};
}

__excludedPaths(this.schema, fields).forEach(
__delPath.bind(null, jsonSchema)
Expand All @@ -35,7 +35,7 @@ function model_jsonSchema(fields, populate, readonly) {
if (fields) __removeRequired(jsonSchema);

return jsonSchema;
};
}

/**
* __populate - enreaches jsonSchema with a sub-document schemas
Expand Down Expand Up @@ -204,7 +204,7 @@ function __getPath(schema, path) {
p += path[i];
if (schema.paths[p] && schema.paths[p].schema) {
return __getPath(schema.paths[p].schema, path.slice(i+1).join('.'))
};
}
p += '.';
}
return null;
Expand Down Expand Up @@ -238,4 +238,4 @@ function __excludedReadonlyPaths(schema, rules, prefix) {

return schema;

};
}
2 changes: 1 addition & 1 deletion lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function __build(name, schema) {
jss.cont.required.push(jss.prop);
}
delete sch.__required;
};
}

if (result.required.length === 0) delete result.required;

Expand Down
35 changes: 24 additions & 11 deletions lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@ module.exports = exports = {
*
* @memberof mongoose.Schema.Types.String
*
* @param {String} name the name of parameter
* @return {object} the jsonSchema for parameter
*/
function simpleType_jsonSchema(name) {
function simpleType_jsonSchema() {
var result = {};
result.type = this.instance.toLowerCase();

__processOptions(result, this.options)

return result;
return checkNullable(result);
}


Expand All @@ -40,7 +39,7 @@ function objectId_jsonSchema(name) {
result.type = 'string';
result.pattern = '^[0-9a-fA-F]{24}$';

return result;
return checkNullable(result);
}


Expand All @@ -57,7 +56,7 @@ function date_jsonSchema(name) {
result.type = 'string';
result.format = 'date-time';

return result;
return checkNullable(result);
}


Expand Down Expand Up @@ -92,7 +91,7 @@ function array_jsonSchema(name) {
__processOptions(result, this.options);
delete result.items.__required;

return result;
return checkNullable(result);
}


Expand All @@ -107,7 +106,7 @@ function array_jsonSchema(name) {
function mixed_jsonSchema(name) {
var result = __describe(name, this.options.type);
__processOptions(result, this.options);
return result;
return checkNullable(result);
}


Expand All @@ -130,7 +129,8 @@ function __processOptions(t, type) {
t.pattern = type.match.toString();
}
}
if (type.default != null) t.default = type.default;
if (type.default !== undefined) t.default = type.default;

t.description =
type.description || type.descr || type.ref && 'Refers to ' + type.ref;
if (!t.description) delete t.description;
Expand Down Expand Up @@ -187,10 +187,10 @@ function __describe(name, type) {

if (type.type) {
type.__buildingSchema = true;
var t = __describe(name, type.type);
var ts = __describe(name, type.type);
delete type.__buildingSchema;
__processOptions(t, type);
return t;
__processOptions(ts, type);
return checkNullable(ts);
}

if (type.constructor.name !== 'Object') {
Expand Down Expand Up @@ -222,3 +222,16 @@ function __describe(name, type) {
}

__describe.__jsonSchemaIdCounter = 0;


/**
*
*/

function checkNullable(typeDef) {
if (typeDef.default === null) {
typeDef.type = [typeDef.type, null];
}

return typeDef;
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
"istanbul": "^0.4.5",
"jsonschema": "^1.2.2",
"mocha": "^5.2.0",
"mongoose": "^4.13.8 || ^5.0.0"
"mongoose": "^5.0.0"
},
"peerDependencies": {
"mongoose": ">= 4.8.5 <= 5.x"
"mongoose": "^5.0.0"
},
"dependencies": {
"pluralize": "^7.0.0"
Expand Down
37 changes: 3 additions & 34 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,38 +261,6 @@ newSchemaType.prototype.jsonSchema = function() {
```



## Command line

There is a command line utility to build schema without importing `mongoose-schema-jsonschema`
to your project

```shell
npm i -g jsonschema-builder
```

### Usage

Getting options:
```shell
jsonschema-builder --help
```

Output:
```shell
Usage: jsonschema-builder [options] <pathToModels>

Options:

-h, --help output usage information
-V, --version output the version number
-j, --json JSON format for output
-n, --noid Removes id field from resulting schema
```
Samples and other details can be reached by the link:
[jsonschema-builder](https://www.npmjs.com/package/jsonschema-builder)


### Releases:
- version 1.0 - Basic functionality
- version 1.1 - Mongoose.Query support implemented
Expand All @@ -302,7 +270,8 @@ Samples and other details can be reached by the link:
- version 1.1.11 - required issue fixed [issue#2](https://github.com/DScheglov/mongoose-schema-jsonschema/issues/2)
- version 1.1.12 - mixed-type fields description and title support added (fix for issue: [issue#3](https://github.com/DScheglov/mongoose-schema-jsonschema/issues/3))
- version 1.1.15 - support for mongoose@5.x ensured [issue#8](https://github.com/DScheglov/mongoose-schema-jsonschema/issues/8)
- version 1.2.0 - nullable types. Stopped support for node less then 8. Stopped support for mongoose v4.x

### Supported versions:
- node.js: 4.x, 5.x, 6.x, 8.x, 9.x
- mongoose: 4.x, 5.x
- node.js: 8.x, 9.x, 10.x, 12.x
- mongoose: 5.x
3 changes: 2 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ require('./suites/circular-refs');
require('./suites/queries');
require('./suites/readonly');
require('./suites/required-issue');
require('./suites/discriminators');
require('./suites/discriminators');
require('./suites/nullable-types');
141 changes: 141 additions & 0 deletions test/suites/nullable-types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
'use strict';

var mongoose = require('../../index')(require('mongoose'));
var Schema = mongoose.Schema;
var validate = require('jsonschema').validate;
var assert = require('assert');

describe('schema.jsonSchema', function() {

it ('should correctly translate all simmple types', function () {
var mSchema = new Schema({
n: { type: Number, default: null },
s: { type: String, default: null },
d: { type: Date, default: null },
b: { type: Boolean, default: null },
u: { type: Schema.Types.ObjectId, default: null },
});

var jsonSchema = mSchema.jsonSchema('Sample');

assert.deepEqual(jsonSchema, {
title: 'Sample',
type: 'object',
properties: {
n: { type: ['number', null], default: null },
s: { type: ['string', null], default: null },
d: { type: ['string', null], default: null, format: 'date-time' },
b: { type: ['boolean', null], default: null },
u: { type: ['string', null], default: null, pattern: '^[0-9a-fA-F]{24}$' },
_id: { type: 'string', pattern: '^[0-9a-fA-F]{24}$' }
}
});

});

it ('should correctly translate many levels of nested Schemas', function () {

var mSchema1 = new Schema({x: Number}, {id: false, _id: false});
var mSchema2 = new Schema({y: Number, x: mSchema1}, {id: false, _id: false});
var mSchema3 = new Schema({z: Number, y: mSchema2}, {id: false, _id: false});
var mSchema4 = new Schema({
t: Number,
xyz: {
type: {
x: [{
type: mSchema1,
required: true
}],
y: {type: mSchema2, required: true},
z: {type: [mSchema3]},
t: { type: [{ x: Number, y: Number }], default: null },
any: { type: Schema.Types.Mixed, required: true }
},
default: null
}
});

var jsonSchema = mSchema4.jsonSchema('Sample');

assert.deepEqual(jsonSchema, {
title: 'Sample',
type: 'object',
properties: {
t: {type: 'number'},
xyz: {
title: 'xyz',
type: ['object', null],
default: null,
properties: {
x: {
type: 'array',
minItems: 1,
items: {
type: 'object',
title: 'itemOf_x',
properties: {
x: {type: 'number'}
}
}
},
y: {
type: 'object',
title: 'y',
properties: {
y: {type: 'number'},
x: {
type: 'object',
title: 'x',
properties: {
x: {type: 'number'}
}
}
}
},
z: {
type: 'array',
items: {
type: 'object',
title: 'itemOf_z',
properties: {
z: {type: 'number'},
y: {
type: 'object',
title: 'y',
properties: {
y: {type: 'number'},
x: {
type: 'object',
title: 'x',
properties: {
x: {type: 'number'}
}
}
}
}
}
}
},
t: {
type: ['array', null],
default: null,
items: {
type: 'object',
title: 'itemOf_t',
properties: {
x: {type: 'number'},
y: {type: 'number'}
}
}
},
any: { }
},
required: ['y', 'any']
},
_id: {type: 'string', pattern: '^[0-9a-fA-F]{24}$'}
}
});

});

});