Skip to content

Commit

Permalink
feat(schematype): add validators, path, isRequired to public AP…
Browse files Browse the repository at this point in the history
…I and TypeScript types

Fix #11139
  • Loading branch information
vkarpov15 committed Jan 9, 2022
1 parent 36c9e6a commit acb1217
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
9 changes: 9 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3252,9 +3252,15 @@ declare module 'mongoose' {
/** String representation of what type this is, like 'ObjectID' or 'Number' */
instance: string;

/** True if this SchemaType has a required validator. False otherwise. */
isRequired?: boolean;

/** The options this SchemaType was instantiated with */
options: AnyObject;

/** The path to this SchemaType in a Schema. */
path: string;

/**
* Set the model that this path refers to. This is the option that [populate](https://mongoosejs.com/docs/populate.html)
* looks at to determine the foreign collection it should query.
Expand Down Expand Up @@ -3288,6 +3294,9 @@ declare module 'mongoose' {
/** Declares an unique index. */
unique(bool: boolean): this;

/** The validators that Mongoose should run to validate properties at this SchemaType's path. */
validators: { message?: string; type?: string; validator?: Function }[];

/** Adds validator(s) for this document path. */
validate(obj: RegExp | Function | any, errorMsg?: string,
type?: string): this;
Expand Down
45 changes: 45 additions & 0 deletions lib/schematype.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,51 @@ function SchemaType(path, options, instance) {

SchemaType.prototype.OptionsConstructor = SchemaTypeOptions;

/**
* The path to this SchemaType in a Schema.
*
* ####Example:
* const schema = new Schema({ name: String });
* schema.path('name').path; // 'name'
*
* @property path
* @api public
* @memberOf SchemaType
*/

SchemaType.prototype.path;

/**
* The validators that Mongoose should run to validate properties at this SchemaType's path.
*
* ####Example:
* const schema = new Schema({ name: { type: String, required: true } });
* schema.path('name').validators.length; // 1, the `required` validator
*
* @property validators
* @api public
* @memberOf SchemaType
*/

SchemaType.prototype.validators;

/**
* True if this SchemaType has a required validator. False otherwise.
*
* ####Example:
* const schema = new Schema({ name: { type: String, required: true } });
* schema.path('name').isRequired; // true
*
* schema.path('name').required(false);
* schema.path('name').isRequired; // false
*
* @property isRequired
* @api public
* @memberOf SchemaType
*/

SchemaType.prototype.validators;

/*!
* ignore
*/
Expand Down

0 comments on commit acb1217

Please sign in to comment.