Skip to content

Commit

Permalink
fix(index.d.ts): correct type definition for SchemaType#cast()
Browse files Browse the repository at this point in the history
Fix #9980
  • Loading branch information
vkarpov15 committed Mar 18, 2021
1 parent 96ca70b commit 7b196f7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2535,8 +2535,8 @@ declare module 'mongoose' {
/** Attaches a getter for all instances of this schema type. */
static get(getter: (value: any) => any): void;

/** Get/set the function used to cast arbitrary values to this type. */
cast(caster: (v: any) => any): (v: any) => any;
/** Cast `val` to this schema type. Each class that inherits from schema type should implement this function. */
cast(val: any, doc: Document<any>, init: boolean): any;

/** Sets a default value for this SchemaType. */
default(val: any): any;
Expand Down
13 changes: 13 additions & 0 deletions lib/schematype.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,19 @@ SchemaType.prototype.castFunction = function castFunction(caster) {
return this._castFunction;
};

/**
* The function that Mongoose calls to cast arbitrary values to this SchemaType.
*
* @param {Object} value value to cast
* @param {Document} doc document that triggers the casting
* @param {Boolean} init
* @api public
*/

SchemaType.prototype.cast = function cast() {
throw new Error('Base SchemaType class does not implement a `cast()` function');
};

/**
* Sets a default option for this schema type.
*
Expand Down

0 comments on commit 7b196f7

Please sign in to comment.