UNPKG

@ts-fluentvalidation/core

Version:

Core library of @ts-fluentvalidation providing validations in a fluent syntax.

47 lines 2.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.wrapAsArray = wrapAsArray; exports.failureForValidation = failureForValidation; const i18n_1 = require("../i18n/i18n"); const message_formatter_1 = require("../validations/message-formatter"); /** * Wraps a value as an array if it is not already an array. * * @param value - The value to wrap as an array. * @returns The value as an array. */ function wrapAsArray(value) { return Array.isArray(value) ? value : [value]; } /** * Creates a validation failure for the given validation. * * @param model - The model being validated. * @param propertyNameFromModel - The key being validated. * @param propertyValue - The value of the property being validated. * @param validation - The validation that failed. * @returns The created validation failure. */ function failureForValidation(validationContext, propertyNameFromModel, propertyValue, validation, index) { var _a, _b, _c, _d; const propertyNameForFailure = appendIndexIfSet((_a = validation.metadata.propertyNameOverride) !== null && _a !== void 0 ? _a : propertyNameFromModel, index); const computedPropertyName = validationContext.parentPropertyName ? `${validationContext.parentPropertyName}.${propertyNameForFailure}` : propertyNameForFailure; return { propertyName: computedPropertyName, message: (0, message_formatter_1.formatMessage)((_b = validation.metadata.message) !== null && _b !== void 0 ? _b : i18n_1.i18n.getMessage((_c = validation.metadata.errorCode) !== null && _c !== void 0 ? _c : 'default'), Object.assign(Object.assign({}, validation.metadata.placeholders), { [message_formatter_1.DEFAULT_PLACEHOLDERS.propertyName]: (_d = validation.metadata.propertyName) !== null && _d !== void 0 ? _d : computedPropertyName, [message_formatter_1.DEFAULT_PLACEHOLDERS.propertyValue]: propertyValue })), errorCode: validation.metadata.errorCode, attemptedValue: propertyValue, severity: validation.metadata.severityProvider ? validation.metadata.severityProvider(validationContext.modelToValidate, propertyValue) : 'Error', customState: validation.metadata.customStateProvider ? validation.metadata.customStateProvider(validationContext.modelToValidate, propertyValue) : undefined }; } function appendIndexIfSet(propertyName, index) { return index !== undefined ? `${propertyName}[${index}]` : propertyName; } //# sourceMappingURL=utils.js.map