Skip to content
Merged
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
8 changes: 8 additions & 0 deletions lib/helpers/replace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function convertible(value) {
if (value === undefined) return "";
if (value === null) return "";
if (typeof value.toString === "function") return value;
return typeof value;
}

module.exports = (string, searchValue, newValue) => string.replace(searchValue, convertible(newValue));
17 changes: 10 additions & 7 deletions lib/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ try {
AsyncFunction = (new Function("return Object.getPrototypeOf(async function(){}).constructor"))();
} catch(err) { /* async is not supported */}

//const flatten = require("./helpers/flatten");
const deepExtend = require("./helpers/deep-extend");
const replace = require("./helpers/replace");

function loadMessages() {
return Object.assign({} , require("./messages"));
Expand Down Expand Up @@ -167,7 +167,10 @@ class Validator {
async: schema.$$async === true,
rules: [],
fn: [],
customs: {}
customs: {},
utils: {
replace,
},
};
this.cache.clear();
delete schema.$$async;
Expand Down Expand Up @@ -205,11 +208,11 @@ class Validator {
sourceCode.push("if (errors.length) {");
sourceCode.push(`
return errors.map(err => {
if (err.message)
err.message = err.message
.replace(/\\{field\\}/g, err.field || "")
.replace(/\\{expected\\}/g, err.expected != null ? err.expected : "")
.replace(/\\{actual\\}/g, err.actual != null ? err.actual : "");
if (err.message) {
err.message = context.utils.replace(err.message, /\\{field\\}/g, err.field);
err.message = context.utils.replace(err.message, /\\{expected\\}/g, err.expected);
err.message = context.utils.replace(err.message, /\\{actual\\}/g, err.actual);
}

return err;
});
Expand Down
21 changes: 21 additions & 0 deletions test/helpers/replace.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const replace = require("../../lib/helpers/replace");

describe("replace", () => {
it("should replace string", () => {
expect(replace("foo bar", "foo", "zoo")).toBe("zoo bar");
});

it("should replace if newValue is null or undefined", () => {
expect(replace("foo bar", "foo", undefined)).toBe(" bar");
expect(replace("foo bar", "foo", null)).toBe(" bar");
});

it("should replace if newValue has valid toString prototype", () => {
expect(replace("foo bar", "foo", { a: "b" })).toBe("[object Object] bar");
expect(replace("foo bar", "foo", ["a", "b"])).toBe("a,b bar");
});

it("should replace if newValue has invalid toString prototype", () => {
expect(replace("foo bar", "foo", { toString: 1 })).toBe("object bar");
});
});
17 changes: 17 additions & 0 deletions test/integration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1285,3 +1285,20 @@ describe("Test context meta", () => {
expect(obj).toEqual({ name: "from-meta" });
});
});

describe("edge cases", () => {
const v = new Validator({ useNewCustomCheckerFunction: true });

it("issue #235 bug", () => {
const schema = { name: { type: "string" } };
const check = v.compile(schema);
expect(check({ name: { toString: 1 } })).toEqual([
{
actual: { toString: 1 },
field: "name",
message: "The 'name' field must be a string.",
type: "string",
},
]);
});
});
4 changes: 2 additions & 2 deletions test/typescript/validator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ describe('TypeScript Definitions', () => {

check = v.compile(schema);

const context = {
const context = expect.objectContaining({
customs: expect.any(Object),
rules: expect.any(Array),
fn: expect.any(Array),
index: 2,
async: false
};
});

expect(validFn).toHaveBeenCalledTimes(1);
expect(validFn).toHaveBeenCalledWith(expect.any(Object), 'a', context);
Expand Down
4 changes: 2 additions & 2 deletions test/validator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,13 @@ describe("Test add", () => {

check = v.compile(schema);

const context = {
const context = expect.objectContaining({
customs: expect.any(Object),
rules: expect.any(Array),
fn: expect.any(Array),
index: 2,
async: false
};
});


expect(validFn).toHaveBeenCalledTimes(1);
Expand Down