From 53c8da1b57bb19603ac8131dbca26023ed0001c7 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Tue, 10 Sep 2024 11:06:58 -0400 Subject: [PATCH] feat(SchemaType): add getEmbeddedSchemaType() method to SchemaTypes Fix #8389 --- lib/schemaType.js | 18 ++++++++++++++++++ test/schematype.test.js | 9 +++++++++ test/types/schema.test.ts | 7 +++++++ types/schematypes.d.ts | 3 +++ 4 files changed, 37 insertions(+) diff --git a/lib/schemaType.js b/lib/schemaType.js index f95ecbb3226..53cc8caedaf 100644 --- a/lib/schemaType.js +++ b/lib/schemaType.js @@ -1724,6 +1724,24 @@ SchemaType.prototype.clone = function() { return schematype; }; +/** + * Returns the embedded schema type, if any. For arrays, document arrays, and maps, `getEmbeddedSchemaType()` + * returns the schema type of the array's elements (or map's elements). For other types, `getEmbeddedSchemaType()` + * returns `undefined`. + * + * #### Example: + * + * const schema = new Schema({ name: String, tags: [String] }); + * schema.path('name').getEmbeddedSchemaType(); // undefined + * schema.path('tags').getEmbeddedSchemaType(); // SchemaString { path: 'tags', ... } + * + * @returns {SchemaType} embedded schematype + */ + +SchemaType.prototype.getEmbeddedSchemaType = function getEmbeddedSchemaType() { + return this.$embeddedSchemaType; +}; + /*! * Module exports. */ diff --git a/test/schematype.test.js b/test/schematype.test.js index ad8367d0f61..725e21966a4 100644 --- a/test/schematype.test.js +++ b/test/schematype.test.js @@ -315,4 +315,13 @@ describe('schematype', function() { /password must be at least six characters/ ); }); + + it('supports getEmbeddedSchemaType() (gh-8389)', function() { + const schema = new Schema({ name: String, tags: [String] }); + assert.strictEqual(schema.path('name').getEmbeddedSchemaType(), undefined); + const schemaType = schema.path('tags').getEmbeddedSchemaType(); + assert.ok(schemaType); + assert.equal(schemaType.instance, 'String'); + assert.equal(schemaType.path, 'tags'); + }); }); diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts index 04828bc4f17..a2164ddaa2e 100644 --- a/test/types/schema.test.ts +++ b/test/types/schema.test.ts @@ -1634,3 +1634,10 @@ function gh14825() { type SchemaType = InferSchemaType; expectAssignable({} as SchemaType); } + +function gh8389() { + const schema = new Schema({ name: String, tags: [String] }); + + expectAssignable | undefined>(schema.path('name').getEmbeddedSchemaType()); + expectAssignable | undefined>(schema.path('tags').getEmbeddedSchemaType()); +} diff --git a/types/schematypes.d.ts b/types/schematypes.d.ts index e8a0ecffdf0..1e270733e48 100644 --- a/types/schematypes.d.ts +++ b/types/schematypes.d.ts @@ -229,6 +229,9 @@ declare module 'mongoose' { /** Adds a getter to this schematype. */ get(fn: Function): this; + /** Gets this SchemaType's embedded SchemaType, if any */ + getEmbeddedSchemaType(): SchemaType | undefined; + /** * Defines this path as immutable. Mongoose prevents you from changing * immutable paths unless the parent document has [`isNew: true`](/docs/api/document.html#document_Document-isNew).