Skip to content

Commit e893399

Browse files
committed
fix: rename jsonSchema -> toJSONSchema for consistency
1 parent efbef97 commit e893399

File tree

4 files changed

+27
-27
lines changed

4 files changed

+27
-27
lines changed

lib/schema.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2899,16 +2899,16 @@ Schema.prototype._preCompile = function _preCompile() {
28992899
* #### Example:
29002900
* const schema = new Schema({ name: String });
29012901
* // { required: ['_id'], properties: { name: { type: ['string', 'null'] }, _id: { type: 'string' } } }
2902-
* schema.jsonSchema();
2902+
* schema.toJSONSchema();
29032903
*
29042904
* // { required: ['_id'], properties: { name: { bsonType: ['string', 'null'] }, _id: { bsonType: 'objectId' } } }
2905-
* schema.jsonSchema({ useBsonType: true });
2905+
* schema.toJSONSchema({ useBsonType: true });
29062906
*
29072907
* @param {Object} [options]
29082908
* @param [Boolean] [options.useBsonType=false] if true, specify each path's type using `bsonType` rather than `type` for MongoDB $jsonSchema support
29092909
*/
29102910

2911-
Schema.prototype.jsonSchema = function jsonSchema(options) {
2911+
Schema.prototype.toJSONSchema = function toJSONSchema(options) {
29122912
const useBsonType = options?.useBsonType ?? false;
29132913
const result = useBsonType ? { required: [], properties: {} } : { type: 'object', required: [], properties: {} };
29142914
for (const path of Object.keys(this.paths)) {

lib/schema/documentArray.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ SchemaDocumentArray.prototype.toJSONSchema = function toJSONSchema(options) {
665665
const isRequired = this.options.required && typeof this.options.required !== 'function';
666666
return {
667667
...createJSONSchemaTypeDefinition('array', 'array', options?.useBsonType, isRequired),
668-
items: { ...itemsTypeDefinition, ...this.schema.jsonSchema(options) }
668+
items: { ...itemsTypeDefinition, ...this.schema.toJSONSchema(options) }
669669
};
670670
};
671671

lib/schema/subdocument.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ SchemaSubdocument.prototype.clone = function() {
409409
SchemaSubdocument.prototype.toJSONSchema = function toJSONSchema(options) {
410410
const isRequired = this.options.required && typeof this.options.required !== 'function';
411411
return {
412-
...this.schema.jsonSchema(options),
412+
...this.schema.toJSONSchema(options),
413413
...createJSONSchemaTypeDefinition('object', 'object', options?.useBsonType, isRequired)
414414
};
415415
};

test/schema.test.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2173,7 +2173,7 @@ describe('schema', function() {
21732173
const keys = Object.keys(SchemaStringOptions.prototype).
21742174
filter(key => key !== 'constructor' && key !== 'populate');
21752175
const functions = Object.keys(Schema.Types.String.prototype).
2176-
filter(key => ['constructor', 'cast', 'castForQuery', 'checkRequired'].indexOf(key) === -1);
2176+
filter(key => ['constructor', 'cast', 'castForQuery', 'checkRequired', 'toJSONSchema'].indexOf(key) === -1);
21772177
assert.deepEqual(keys.sort(), functions.sort());
21782178
});
21792179

@@ -3344,7 +3344,7 @@ describe('schema', function() {
33443344
}
33453345
}, { autoCreate: false, autoIndex: false });
33463346

3347-
assert.deepStrictEqual(schema.jsonSchema({ useBsonType: true }), {
3347+
assert.deepStrictEqual(schema.toJSONSchema({ useBsonType: true }), {
33483348
required: ['name', '_id'],
33493349
properties: {
33503350
_id: {
@@ -3363,7 +3363,7 @@ describe('schema', function() {
33633363
}
33643364
});
33653365

3366-
assert.deepStrictEqual(schema.jsonSchema(), {
3366+
assert.deepStrictEqual(schema.toJSONSchema(), {
33673367
type: 'object',
33683368
required: ['name', '_id'],
33693369
properties: {
@@ -3385,7 +3385,7 @@ describe('schema', function() {
33853385

33863386
await db.createCollection(collectionName, {
33873387
validator: {
3388-
$jsonSchema: schema.jsonSchema({ useBsonType: true })
3388+
$jsonSchema: schema.toJSONSchema({ useBsonType: true })
33893389
}
33903390
});
33913391
const Test = db.model('Test', schema, collectionName);
@@ -3414,7 +3414,7 @@ describe('schema', function() {
34143414
);
34153415

34163416
const ajv = new Ajv();
3417-
const validate = ajv.compile(schema.jsonSchema());
3417+
const validate = ajv.compile(schema.toJSONSchema());
34183418

34193419
assert.ok(validate({ _id: 'test', name: 'Taco' }));
34203420
assert.ok(validate({ _id: 'test', name: 'Billy', age: null, ageSource: null }));
@@ -3438,7 +3438,7 @@ describe('schema', function() {
34383438
int32: 'Int32'
34393439
});
34403440

3441-
assert.deepStrictEqual(schema.jsonSchema({ useBsonType: true }), {
3441+
assert.deepStrictEqual(schema.toJSONSchema({ useBsonType: true }), {
34423442
required: ['_id'],
34433443
properties: {
34443444
num: {
@@ -3480,7 +3480,7 @@ describe('schema', function() {
34803480
}
34813481
});
34823482

3483-
assert.deepStrictEqual(schema.jsonSchema(), {
3483+
assert.deepStrictEqual(schema.toJSONSchema(), {
34843484
type: 'object',
34853485
required: ['_id'],
34863486
properties: {
@@ -3531,7 +3531,7 @@ describe('schema', function() {
35313531
docArr: [new Schema({ field: Date }, { _id: false })]
35323532
});
35333533

3534-
assert.deepStrictEqual(schema.jsonSchema({ useBsonType: true }), {
3534+
assert.deepStrictEqual(schema.toJSONSchema({ useBsonType: true }), {
35353535
required: ['_id'],
35363536
properties: {
35373537
tags: {
@@ -3564,7 +3564,7 @@ describe('schema', function() {
35643564

35653565
await db.createCollection(collectionName, {
35663566
validator: {
3567-
$jsonSchema: schema.jsonSchema({ useBsonType: true })
3567+
$jsonSchema: schema.toJSONSchema({ useBsonType: true })
35683568
}
35693569
});
35703570
const Test = db.model('Test', schema, collectionName);
@@ -3575,7 +3575,7 @@ describe('schema', function() {
35753575
await Test.create({ tags: 'javascript', coordinates: [[0, 0]], docArr: [{}] });
35763576

35773577
const ajv = new Ajv();
3578-
const validate = ajv.compile(schema.jsonSchema());
3578+
const validate = ajv.compile(schema.toJSONSchema());
35793579

35803580
assert.ok(validate({ _id: 'test', tags: ['javascript'], coordinates: [[0, 0]], docArr: [{ field: '2023-07-16' }] }));
35813581
assert.ok(validate({ _id: 'test', tags: ['javascript'], coordinates: [[0, 0]], docArr: [{}] }));
@@ -3592,7 +3592,7 @@ describe('schema', function() {
35923592
}, { _id: false })
35933593
});
35943594

3595-
assert.deepStrictEqual(schema.jsonSchema({ useBsonType: true }), {
3595+
assert.deepStrictEqual(schema.toJSONSchema({ useBsonType: true }), {
35963596
required: ['_id'],
35973597
properties: {
35983598
name: {
@@ -3615,7 +3615,7 @@ describe('schema', function() {
36153615
}
36163616
});
36173617

3618-
assert.deepStrictEqual(schema.jsonSchema(), {
3618+
assert.deepStrictEqual(schema.toJSONSchema(), {
36193619
required: ['_id'],
36203620
type: 'object',
36213621
properties: {
@@ -3641,7 +3641,7 @@ describe('schema', function() {
36413641

36423642
await db.createCollection(collectionName, {
36433643
validator: {
3644-
$jsonSchema: schema.jsonSchema({ useBsonType: true })
3644+
$jsonSchema: schema.toJSONSchema({ useBsonType: true })
36453645
}
36463646
});
36473647
const Test = db.model('Test', schema, collectionName);
@@ -3650,7 +3650,7 @@ describe('schema', function() {
36503650
await Test.create({ name: { first: 'Mike', last: 'James' }, subdoc: { prop: 42 } });
36513651

36523652
const ajv = new Ajv();
3653-
const validate = ajv.compile(schema.jsonSchema());
3653+
const validate = ajv.compile(schema.toJSONSchema());
36543654

36553655
assert.ok(validate({ _id: 'test', name: { last: 'James' }, subdoc: {} }));
36563656
assert.ok(validate({ _id: 'test', name: { first: 'Mike', last: 'James' }, subdoc: { prop: 42 } }));
@@ -3686,7 +3686,7 @@ describe('schema', function() {
36863686
}
36873687
});
36883688

3689-
assert.deepStrictEqual(schema.jsonSchema({ useBsonType: true }), {
3689+
assert.deepStrictEqual(schema.toJSONSchema({ useBsonType: true }), {
36903690
required: ['props', '_id'],
36913691
properties: {
36923692
props: {
@@ -3746,7 +3746,7 @@ describe('schema', function() {
37463746

37473747
await db.createCollection(collectionName, {
37483748
validator: {
3749-
$jsonSchema: schema.jsonSchema({ useBsonType: true })
3749+
$jsonSchema: schema.toJSONSchema({ useBsonType: true })
37503750
}
37513751
});
37523752
const Test = db.model('Test', schema, collectionName);
@@ -3783,7 +3783,7 @@ describe('schema', function() {
37833783
);
37843784

37853785
const ajv = new Ajv();
3786-
const validate = ajv.compile(schema.jsonSchema());
3786+
const validate = ajv.compile(schema.toJSONSchema());
37873787

37883788
assert.ok(validate({
37893789
_id: 'test',
@@ -3829,7 +3829,7 @@ describe('schema', function() {
38293829
}
38303830
});
38313831

3832-
assert.deepStrictEqual(schema.jsonSchema({ useBsonType: true }), {
3832+
assert.deepStrictEqual(schema.toJSONSchema({ useBsonType: true }), {
38333833
required: ['_id'],
38343834
properties: {
38353835
props: {
@@ -3844,7 +3844,7 @@ describe('schema', function() {
38443844
}
38453845
});
38463846

3847-
assert.deepStrictEqual(schema.jsonSchema(), {
3847+
assert.deepStrictEqual(schema.toJSONSchema(), {
38483848
type: 'object',
38493849
required: ['_id'],
38503850
properties: {
@@ -3866,7 +3866,7 @@ describe('schema', function() {
38663866
name: { type: String, enum: ['Edwald', 'Tobi'], required: true }
38673867
});
38683868

3869-
assert.deepStrictEqual(RacoonSchema.jsonSchema({ useBsonType: true }), {
3869+
assert.deepStrictEqual(RacoonSchema.toJSONSchema({ useBsonType: true }), {
38703870
required: ['name', '_id'],
38713871
properties: {
38723872
name: {
@@ -3885,8 +3885,8 @@ describe('schema', function() {
38853885
mixed: mongoose.Mixed
38863886
});
38873887

3888-
assert.throws(() => schema.jsonSchema({ useBsonType: true }), /unsupported SchemaType to JSON Schema: Mixed/);
3889-
assert.throws(() => schema.jsonSchema(), /unsupported SchemaType to JSON Schema: Mixed/);
3888+
assert.throws(() => schema.toJSONSchema({ useBsonType: true }), /unsupported SchemaType to JSON Schema: Mixed/);
3889+
assert.throws(() => schema.toJSONSchema(), /unsupported SchemaType to JSON Schema: Mixed/);
38903890
});
38913891
});
38923892
});

0 commit comments

Comments
 (0)