Skip to content

Commit

Permalink
add Buffer and UUID, add link to JSON schema docs, add additional tes…
Browse files Browse the repository at this point in the history
…t coverage
  • Loading branch information
vkarpov15 committed Jan 17, 2025
1 parent 149f0f5 commit 402bbec
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -2889,7 +2889,7 @@ Schema.prototype._preCompile = function _preCompile() {
/**
* Returns a JSON schema representation of this Schema.
*
* By default, returns normal JSON schema representation, which is not typically what you want to use with
* By default, returns normal [JSON schema representation](https://json-schema.org/learn/getting-started-step-by-step), which is not typically what you want to use with
* [MongoDB's `$jsonSchema` collection option](https://www.mongodb.com/docs/manual/core/schema-validation/specify-json-schema/).
* Use the `useBsonType: true` option to return MongoDB `$jsonSchema` syntax instead.
*
Expand Down Expand Up @@ -3018,6 +3018,12 @@ function _schemaTypeToJSONSchema(schemaType, isRequired, options) {
} else if (schemaType.instance === 'Decimal128') {
bsonType = ['decimal'];
type = ['string'];
} else if (schemaType.instance === 'Buffer') {
bsonType = ['binData'];
type = ['string'];
} else if (schemaType.instance === 'UUID') {
bsonType = ['binData'];
type = ['string'];
} else if (schemaType.instance === 'Embedded') {
bsonType = ['object'],
type = ['object'];
Expand Down
148 changes: 148 additions & 0 deletions test/schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3419,6 +3419,86 @@ describe('schema', function() {
assert.ok(!validate({}));
});

it('handles all primitive data types', async function() {
const schema = new Schema({
num: Number,
str: String,
bool: Boolean,
date: Date,
id: mongoose.ObjectId,
decimal: mongoose.Types.Decimal128,
buf: Buffer,
uuid: 'UUID'
});

assert.deepStrictEqual(schema.jsonSchema({ useBsonType: true }), {
required: ['_id'],
properties: {
num: {
bsonType: ['number', 'null']
},
str: {
bsonType: ['string', 'null']
},
bool: {
bsonType: ['bool', 'null']
},
date: {
bsonType: ['date', 'null']
},
id: {
bsonType: ['objectId', 'null']
},
decimal: {
bsonType: ['decimal', 'null']
},
buf: {
bsonType: ['binData', 'null']
},
uuid: {
bsonType: ['binData', 'null']
},
_id: {
bsonType: 'objectId'
}
}
});

assert.deepStrictEqual(schema.jsonSchema(), {
type: 'object',
required: ['_id'],
properties: {
num: {
type: ['number', 'null']
},
str: {
type: ['string', 'null']
},
bool: {
type: ['boolean', 'null']
},
date: {
type: ['string', 'null']
},
id: {
type: ['string', 'null']
},
decimal: {
type: ['string', 'null']
},
buf: {
type: ['string', 'null']
},
uuid: {
type: ['string', 'null']
},
_id: {
type: 'string'
}
}
});
});

it('handles arrays and document arrays', async function() {
const schema = new Schema({
tags: [String],
Expand Down Expand Up @@ -3691,5 +3771,73 @@ describe('schema', function() {
}
}));
});

it('handles map with required element', async function() {
const schema = new Schema({
props: {
type: Map,
of: { type: String, required: true }
}
});

assert.deepStrictEqual(schema.jsonSchema({ useBsonType: true }), {
required: ['_id'],
properties: {
props: {
bsonType: ['object', 'null'],
additionalProperties: {
bsonType: 'string'
}
},
_id: {
bsonType: 'objectId'
}
}
});

assert.deepStrictEqual(schema.jsonSchema(), {
type: 'object',
required: ['_id'],
properties: {
props: {
type: ['object', 'null'],
additionalProperties: {
type: 'string'
}
},
_id: {
type: 'string'
}
}
});
})

Check failure on line 3813 in test/schema.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Missing semicolon

Check failure on line 3813 in test/schema.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Missing semicolon

it('handles required enums', function() {
const RacoonSchema = new Schema({
name: { type: String, enum: ['Edwald', 'Tobi'], required: true }
});

assert.deepStrictEqual(RacoonSchema.jsonSchema({ useBsonType: true }), {
required: ['name', '_id'],
properties: {
name: {
bsonType: 'string',
enum: ['Edwald', 'Tobi']
},
_id: {
bsonType: 'objectId'
}
}
});
});

it('throws error on mixed type', function() {
const schema = new Schema({
mixed: mongoose.Mixed
});

assert.throws(() => schema.jsonSchema({ useBsonType: true }), /unsupported schematype "Mixed"/);
assert.throws(() => schema.jsonSchema(), /unsupported schematype "Mixed"/);
});
});
});

0 comments on commit 402bbec

Please sign in to comment.