UNPKG

@inaiat/fastify-papr

Version:
82 lines (81 loc) 3.45 kB
import { MongoServerError } from "mongodb"; //#region src/mongo-validation-error.ts const MONGO_DOCUMENT_VALIDATION_ERROR_CODE = 121; /** * 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 */ function isMongoServerError(error) { return error instanceof MongoServerError; } /** * Transforms validation error properties into a more readable format * @param properties The properties that failed validation * @returns A mapped array of property errors with details * @internal */ const formatValidationProperties = (properties) => properties.map((prop) => ({ [prop.propertyName]: prop.details })); const hasAdditionalProperties = (rule) => rule.operatorName === "additionalProperties" && (rule.additionalProperties ?? []).length > 0; const formatAdditionalPropertiesRule = (rule) => { const detail = { operatorName: "additionalProperties", specifiedAs: rule.specifiedAs, reason: "property is not allowed by the schema" }; const properties = rule.additionalProperties.map((propertyName) => ({ propertyName, details: [detail] })); return { [rule.operatorName]: formatValidationProperties(properties) }; }; const formatValidationRule = (rule) => hasAdditionalProperties(rule) ? formatAdditionalPropertiesRule(rule) : { [rule.operatorName]: formatValidationProperties(rule.propertiesNotSatisfied ?? []) }; const getRuleProperties = (ruleSet) => Object.values(ruleSet).flat(); /** * Attempts to extract validation error details from a MongoDB server error * @param error The error to process — may be any unknown value * @returns A simplified representation of validation errors or undefined if not a validation error */ function extractValidationErrors(error) { if (!isMongoServerError(error) || error.code !== MONGO_DOCUMENT_VALIDATION_ERROR_CODE) return; const schemaRulesNotSatisfied = error.errInfo?.details?.schemaRulesNotSatisfied ?? []; if (schemaRulesNotSatisfied.length === 0) return; return schemaRulesNotSatisfied.map(formatValidationRule); } /** * Error class that simplifies handling of MongoDB validation errors * Extracts and provides easy access to validation details */ var MongoValidationError = class extends Error { /** The extracted validation rules that weren't satisfied */ validationErrors; /** Flag indicating whether this is a document validation error */ hasValidationFailures; /** * Creates an instance from a MongoDB server error * @param mongoError The original MongoDB error */ constructor(mongoError) { super(mongoError.message, { cause: mongoError }); this.validationErrors = extractValidationErrors(mongoError); this.hasValidationFailures = this.validationErrors !== void 0; } /** * Returns the validation errors as a JSON string * @returns A JSON string of the validation errors or undefined if not a validation error */ getValidationErrorsAsString() { return JSON.stringify(this.validationErrors); } /** * 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) { return (this.validationErrors?.flatMap(getRuleProperties).find((property) => fieldName in property))?.[fieldName]; } }; //#endregion export { MongoValidationError, extractValidationErrors, isMongoServerError }; //# sourceMappingURL=mongo-validation-error.js.map