@mocking-bird/mongoose
Version:
Generates fixtures for `mongoose`. Simply provide the schema or model, and it will generate mock data based on the types and constraints of the schema.
127 lines (126 loc) • 5.35 kB
TypeScript
/// <reference types="mongoose/types/aggregate" />
/// <reference types="mongoose/types/callback" />
/// <reference types="mongoose/types/collection" />
/// <reference types="mongoose/types/connection" />
/// <reference types="mongoose/types/cursor" />
/// <reference types="mongoose/types/document" />
/// <reference types="mongoose/types/error" />
/// <reference types="mongoose/types/expressions" />
/// <reference types="mongoose/types/helpers" />
/// <reference types="mongoose/types/middlewares" />
/// <reference types="mongoose/types/indexes" />
/// <reference types="mongoose/types/models" />
/// <reference types="mongoose/types/mongooseoptions" />
/// <reference types="mongoose/types/pipelinestage" />
/// <reference types="mongoose/types/populate" />
/// <reference types="mongoose/types/query" />
/// <reference types="mongoose/types/schemaoptions" />
/// <reference types="mongoose/types/schematypes" />
/// <reference types="mongoose/types/session" />
/// <reference types="mongoose/types/types" />
/// <reference types="mongoose/types/utility" />
/// <reference types="mongoose/types/validation" />
/// <reference types="mongoose/types/virtuals" />
/// <reference types="mongoose/types/inferschematype" />
import { Rule, Validator, Value } from '@mocking-bird/core';
import { SchemaType, Validator as MongooseValidatorType } from 'mongoose';
/**
* MongooseValidator is a class that extends the Validator class and provides functionalities to validate mongoose
* schema types and rules.
*/
export declare class MongooseValidator extends Validator {
constructor();
/**
* Validates a generated value against both the schema type and the custom rule
*
* @param schemaType The schema type to validate the value against.
* @param value The value to validate.
* @param rule The custom rule to validate the value against.
*
* @throws {Error} If the value does not comply with the schema type or the custom rule.
*/
validateValue(schemaType: SchemaType, value: Value | undefined, rule: Rule | undefined): void;
/**
* Parses mongoose schema rules or validators to a rule object.
*
* @param schemaRules The mongoose schema rules to parse.
*
* @returns The parsed rule object.
*/
parseValidators(schemaRules: (MongooseValidatorType & {
enumValues?: (string | number)[];
min?: number;
max?: number;
})[]): Rule | undefined;
/**
* Validates the custom rule against the schema rule.
*
* @param schemaRule The mongoose schema rule.
* @param rule The custom rule to validate against the schema rule.
*/
validateRules(schemaRule: Rule, rule: Rule): void;
/**
* Combines the schema rule and the custom rule.
*
* @param schemaRule The mongoose schema rule.
* @param rule The custom rule to combine with the schema rule.
*
* @returns The combined rule.
*/
combineRules(schemaRule: Rule | undefined, rule: Rule | undefined): Rule | undefined;
/**
* Validates a generated value against the mongoose schema rules or validators.
*
* @param value The value to validate.
* @param schemaType The mongoose schema type to validate the value against.
*
* @throws {Error} If the value does not comply with the schema rule.
*
* @private
*/
private validateValueBySchema;
/**
* Validates the `required` rule for both the schema and the custom rule.
* If the mongoose rule defines the field as required, the custom rule cannot override it to be non-required.
*
* @param schemaRule The mongoose schema rule.
* @param rule The custom rule to validate against the schema rule.
*
* @throws {Error} If the custom rule overrides the required field in the mongoose schema to be non-required.
*
* @private
*/
private checkRequiredRule;
/**
* Checks the `min` and `max` rules for both the schema and the custom rule.
*
* @example
*
* - If the mongoose schema defines the `min` value as 5, the custom rule cannot override it to be less than 5.
* - If the mongoose schema defines the `max` value as 10, the custom rule cannot override it to be greater than 10.
* - if the mongoose schema defines the `max` value as 10, the custom `min` rule cannot override it to be greater
* than 10.
* - if the mongoose schema defines the `min` value as 5, the custom `max` rule cannot override it to be less than 5.
*
* @param schemaValue The value of the `min` or `max` rule in the mongoose schema.
* @param ruleValue The value of the `min` or `max` rule in the custom rule.
* @param type The type of the rule, i.e., `min` or `max`.
*
* @throws {Error} If the custom rule breaks the `min` or `max` rule in the mongoose schema.
*
* @private
*/
private checkMinMaxRule;
/**
* Validates the `enum` rule for both the schema and the custom rule.
*
* @param schemaRule The mongoose schema rule.
* @param rule The custom rule to validate against the schema rule.
*
* @throws {Error} If the custom rule overrides the `enum` rule in the mongoose schema to include values not in
* the schema rule.
*
* @private
*/
private checkEnumValues;
}