UNPKG

@inaiat/fastify-papr

Version:
93 lines (90 loc) 3.62 kB
import { MongoServerError } from 'mongodb'; /** * Represents details about a property that failed validation */ type ValidationDetail = { /** The validation operator that failed (e.g., 'maxLength', 'minimum') */ operatorName?: string; /** The specification that was violated */ specifiedAs?: Record<string, unknown>; /** Human-readable reason for the validation failure */ reason?: string; /** The actual value that was rejected */ consideredValue?: unknown; /** The type of the value that was rejected */ consideredType?: unknown; }; /** * Represents a property that failed MongoDB validation */ type ValidationProperty = { /** Name of the property that failed validation */ propertyName: string; /** Details about why validation failed */ details: ValidationDetail[]; }; /** * MongoDB validation error structure as returned by the server */ type DocumentValidationError = { /** ID of the document that failed validation */ failingDocumentId?: string; /** Error details from MongoDB */ details?: { /** The validation operator (usually '$jsonSchema') */ operatorName?: string; /** Rules that weren't satisfied by the document */ schemaRulesNotSatisfied?: [ { /** The specific rule operator that failed */ operatorName: string; /** Properties that didn't satisfy the validation rules */ propertiesNotSatisfied: ValidationProperty[]; } ]; }; }; /** * A simplified representation of MongoDB validation errors * Maps operator names to property details for easier client-side handling */ type ValidationErrors = Record<string, Record<string, ValidationDetail[]>[]>[] | undefined; /** * Checks if an error is a MongoDB server error * @param error The error to check * @returns True if the error is a MongoDB server error */ declare function isMongoServerError(error: unknown): error is MongoServerError; /** * Attempts to extract validation error details from a MongoDB server error * @param error The MongoDB server error to process * @returns A simplified representation of validation errors or undefined if not a validation error */ declare function extractValidationErrors(error: Readonly<MongoServerError>): ValidationErrors | undefined; /** * Error class that simplifies handling of MongoDB validation errors * Extracts and provides easy access to validation details */ declare class MongoValidationError extends Error { /** The extracted validation rules that weren't satisfied */ validationErrors?: ValidationErrors; /** Flag indicating whether this is a document validation error */ hasValidationFailures: boolean; /** * Creates an instance from a MongoDB server error * @param mongoError The original MongoDB error */ constructor(mongoError: Readonly<MongoServerError>); /** * Returns the validation errors as a JSON string * @returns A JSON string of the validation errors or undefined if not a validation error */ getValidationErrorsAsString(): string | undefined; /** * Finds validation errors for a specific field * @param fieldName The field name to find errors for * @returns Array of validation details for the field or undefined if none found */ getFieldErrors(fieldName: string): ValidationDetail[] | undefined; } export { type DocumentValidationError, MongoValidationError, type ValidationDetail, type ValidationErrors, type ValidationProperty, extractValidationErrors, isMongoServerError };