Skip to content

Commit

Permalink
Merge pull request #14880 from Automattic/vkarpov15/gh-8389
Browse files Browse the repository at this point in the history
feat(SchemaType): add getEmbeddedSchemaType() method to SchemaTypes
  • Loading branch information
vkarpov15 authored Sep 11, 2024
2 parents 286ab98 + 9b91a6f commit faa30a1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/schemaType.js
Original file line number Diff line number Diff line change
Expand Up @@ -1724,6 +1724,25 @@ 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
* @api public
*/

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 faa30a1

Please sign in to comment.