Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: access of input value on template string for error messages #420

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,9 @@ __name__ {String} - The name of the rule.

__callbackFn__ {Function} - Returns a boolean to represent a successful or failed validation.

__errorMessage__ {String} - An optional string where you can specify a custom error message. _:attribute_ inside errorMessage will be replaced with the attribute name.
__errorMessage__ {String} - An optional string where you can specify a custom error message.
- **_:attribute_** inside errorMessage will be replaced with the attribute name.
- **_:inputValue_** inside errorMessage will be replaced with the attribute value passed. If input value is a `Object({})` it will be coalesced to _"[Object]"_.

```js
Validator.register('telephone', function(value, requirement, attribute) { // requirement parameter defaults to null
Expand Down
91 changes: 91 additions & 0 deletions spec/input-value-access.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
const { Validator, expect } = require("./setup.js");

describe("access input value", function() {
it("should pass if value is not null", function() {
const validator = new Validator({}, { value: "string" });
expect(validator.fails()).to.be.false;
expect(validator.passes()).to.be.true;
});

it("should fail if value is null and not a string", function() {
const validator = new Validator(
{ value: 12 },
{ value: "required|string" }
);
expect(validator.fails()).to.be.true;
expect(validator.passes()).to.be.false;
});

it("should pass if value is replaced in the template error with :inputValue", function() {
const validator = new Validator(
{ value: 12 },
{ value: "required|in:val1,val2" },
{ "in.value": "The value ':inputValue' is not a valid value." }
);
expect(validator.fails()).to.be.true;
expect(validator.passes()).to.be.false;
expect(validator.errors.get("value"));
expect(validator.errors.get("value")[0]).equal(
"The value '12' is not a valid value."
);
});

it("should pass if array is replaced in the template error with :inputValue", function() {
const validator = new Validator(
{ value: [12, '13'] },
{ value: "required|in:val1,val2" },
{ "in.value": "The value ':inputValue' is not a valid value." }
);
expect(validator.fails()).to.be.true;
expect(validator.passes()).to.be.false;
expect(validator.errors.get("value"));
expect(validator.errors.get("value")[0]).equal(
"The value '12,13' is not a valid value."
);
});


it("should pass if empty array is replaced in the template error with :inputValue", function() {
const validator = new Validator(
{ value: [] },
{ value: "string" },
{ "string.value": "The value ':inputValue' is not a valid string." }
);
expect(validator.fails()).to.be.true;
expect(validator.passes()).to.be.false;
expect(validator.errors.get("value"));
expect(validator.errors.get("value")[0]).equal(
"The value '' is not a valid string."
);
});


it("should pass if empty object '{}' is replaced for [Object]", function() {
const validator = new Validator(
{ value: {} },
{ value: "string" },
{ "string.value": "The value ':inputValue' is not a valid string." }
);
expect(validator.fails()).to.be.true;
expect(validator.passes()).to.be.false;
expect(validator.errors.get("value"));
expect(validator.errors.get("value")[0]).equal(
"The value '[Object]' is not a valid string."
);
});

it("should pass if null is replaced for null text", function() {
const validator = new Validator(
{ value: null },
{ value: "required|string|same:34" },
{ "required.value": "The value ':inputValue' is not a valid string." }
);
expect(validator.fails()).to.be.true;
expect(validator.passes()).to.be.false;
console.log(validator.errors.get("value"));
expect(validator.errors.get("value"));
expect(validator.errors.get("value")[0]).equal(
"The value 'null' is not a valid string."
);
});
});
6 changes: 5 additions & 1 deletion src/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,17 @@ Messages.prototype = {
var message, attribute;

data.attribute = this._getAttributeName(rule.attribute);
data.inputValue = rule.inputValue;
data[rule.name] = data[rule.name] || rule.getParameters().join(',');

if (typeof template === 'string' && typeof data === 'object') {
message = template;

for (attribute in data) {
message = message.replace(new RegExp(':' + attribute, 'g'), data[attribute]);
const attrValue = data[attribute];
message = message.replace(new RegExp(':' + attribute, 'g'),
!!attrValue && attrValue.constructor === Object ? '[Object]' : attrValue
);
}
}

Expand Down