Skip to content

Commit

Permalink
[dsch] issue#37 adding support for schemas without schemaOptions (arr…
Browse files Browse the repository at this point in the history
…ay of array)
  • Loading branch information
DScheglov committed Feb 3, 2022
1 parent 88dddcd commit 9b8ca70
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function array_jsonSchema(name) {
result.items = this.caster.jsonSchema(itemName);
}

if (result.items.__required || this.schemaOptions.required) {
if (result.items.__required || this.schemaOptions && this.schemaOptions.required) {
result.minItems = 1;
}

Expand Down
58 changes: 58 additions & 0 deletions test/suites/array-of-array.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Bug Fix for: https://github.com/DScheglov/mongoose-schema-jsonschema/issues/37
*
* Based on the sample taken from the issue
*/
const mongoose = require('../../index')(require('mongoose'));

describe('array of array', () => {
const VariableSchema = mongoose.Schema({
name: { type: String, required: true },
value: {},
});

const ElementPathSchema = new mongoose.Schema({
paths: {
paths: {
type: [[VariableSchema]],
validate: {
validator: function validateTemplate(arrayOfVariables) {
return arrayOfVariables.every(variables => VariableSchema.statics.validateTemplate(variables, 'elementQuery'));
},
message: 'elementPath.paths not valid',
},
},
},
});

it('builds the json schema', () => {
expect(ElementPathSchema.jsonSchema()).toEqual({
type: 'object',
properties: {
paths: {
title: 'paths',
type: 'object',
properties: {
paths: {
type: 'array',
items: {
type: 'array',
items: {
title: 'itemOf_itemOf_paths',
type: 'object',
properties: {
name: { type: 'string' },
value: {},
_id: { type: 'string', pattern: '^[0-9a-fA-F]{24}$' },
},
required: ['name'],
},
},
},
},
},
_id: { type: 'string', pattern: '^[0-9a-fA-F]{24}$' },
},
});
});
});

0 comments on commit 9b8ca70

Please sign in to comment.