Skip to content

Commit

Permalink
feat(SchemaType): add getEmbeddedSchemaType() method to SchemaTypes
Browse files Browse the repository at this point in the history
Fix #8389
  • Loading branch information
vkarpov15 committed Sep 10, 2024
1 parent 286ab98 commit 53c8da1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/schemaType.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
9 changes: 9 additions & 0 deletions test/schematype.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
7 changes: 7 additions & 0 deletions test/types/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1634,3 +1634,10 @@ function gh14825() {
type SchemaType = InferSchemaType<typeof schema>;
expectAssignable<User>({} as SchemaType);
}

function gh8389() {
const schema = new Schema({ name: String, tags: [String] });

expectAssignable<SchemaType<any> | undefined>(schema.path('name').getEmbeddedSchemaType());
expectAssignable<SchemaType<any> | undefined>(schema.path('tags').getEmbeddedSchemaType());
}
3 changes: 3 additions & 0 deletions types/schematypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T = any, DocType = any>(): SchemaType<T, DocType> | 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).
Expand Down

0 comments on commit 53c8da1

Please sign in to comment.