validlyjs
Version:
ValidlyJS is a lightweight, type-safe validation library inspired by Laravel's validation syntax
24 lines • 977 B
JavaScript
import { requiredRule } from "../common/Required.js";
export const requiredIfRule = {
validate: (value, params, ctx) => {
const [otherField, expectedValue] = params;
const otherValue = ctx.data[otherField];
if (otherValue == expectedValue) {
return requiredRule.validate(value, [], ctx); // Reuse the requiredRule validation
}
return true;
},
message: (params, ctx) => {
const [otherField, expectedValue] = params;
const customMessage = ctx.config.messages.required_if;
if (typeof customMessage === "string") {
// Replace placeholders if a custom message is provided
return customMessage
.replace(":other", otherField)
.replace(":value", expectedValue);
}
// Fallback to default message
return `${ctx.field} is required when ${otherField} is ${expectedValue}`;
},
};
//# sourceMappingURL=RequiredIf.js.map