Skip to content

Commit cb58877

Browse files
Merge branch 'master' of git://github.com/icebob/fastest-validator into feature_charset
sync fork
2 parents dcb3b7e + f20da30 commit cb58877

File tree

7 files changed

+88
-19
lines changed

7 files changed

+88
-19
lines changed

examples/full.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const Validator = require("../index");
44
const v = new Validator({
55
messages: {
6-
stringMin: "A(z) '{name}' mező túl rövid. Minimum: {0}, Jelenleg: {1}"
6+
stringMin: "A(z) '{field}' mező túl rövid. Minimum: {expected}, Jelenleg: {actual}"
77
}
88
});
99

lib/messages.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,7 @@ module.exports = {
4545

4646
email: "The '{field}' field must be a valid e-mail!",
4747

48+
url: "The '{field}' field must be a valid URL!",
49+
4850
enumValue: "The '{field} field value '{expected}' does not match any of the allowed values!",
4951
};

lib/rules/string.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ module.exports = function checkString(value, schema) {
2828
return this.makeError("stringLength", schema.length, valueLength);
2929
}
3030

31-
if (schema.pattern != null && !schema.pattern.test(value)) {
32-
return this.makeError("stringPattern", schema.pattern );
31+
if (schema.pattern != null) {
32+
const pattern = typeof schema.pattern == "string" ? new RegExp(schema.pattern, schema.patternFlags) : schema.pattern;
33+
if (!pattern.test(value))
34+
return this.makeError("stringPattern", pattern );
3335
}
3436

3537
if (schema.contains != null && value.indexOf(schema.contains) === -1) {

lib/validator.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,19 @@ function escapeEvalString(str) {
3333
// Based on https://github.com/joliss/js-string-escape
3434
return str.replace(escapeEvalRegex, function (character) {
3535
switch (character) {
36-
case "\"":
37-
case "'":
38-
case "\\":
39-
return "\\" + character;
36+
case "\"":
37+
case "'":
38+
case "\\":
39+
return "\\" + character;
4040
// Four possible LineTerminator characters need to be escaped:
41-
case "\n":
42-
return "\\n";
43-
case "\r":
44-
return "\\r";
45-
case "\u2028":
46-
return "\\u2028";
47-
case "\u2029":
48-
return "\\u2029";
41+
case "\n":
42+
return "\\n";
43+
case "\r":
44+
return "\\r";
45+
case "\u2028":
46+
return "\\u2028";
47+
case "\u2029":
48+
return "\\u2029";
4949
}
5050
});
5151
}
@@ -97,15 +97,15 @@ Validator.prototype.compile = function(schema) {
9797

9898
const rules = this.compileSchemaType(schema);
9999
this.cache.clear();
100-
return function(value) {
101-
return self.checkSchemaType(value, rules, undefined, null);
100+
return function(value, path, parent) {
101+
return self.checkSchemaType(value, rules, path, parent || null);
102102
};
103103
}
104104

105105
const rule = this.compileSchemaObject(schema);
106106
this.cache.clear();
107-
return function(value) {
108-
return self.checkSchemaObject(value, rule, undefined, null);
107+
return function(value, path, parent) {
108+
return self.checkSchemaObject(value, rule, path, parent || null);
109109
};
110110
};
111111

test/messages.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ describe("Test Messages", () => {
3838
expect(msg.dateMax).toBeDefined();
3939
expect(msg.forbidden).toBeDefined();
4040
expect(msg.email).toBeDefined();
41+
expect(msg.url).toBeDefined();
4142

4243
});
4344

test/rules/string.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ describe("Test checkString", () => {
6060
expect(check("JOHN", s)).toEqual(true);
6161
});
6262

63+
it("check pattern with string", () => {
64+
const s = { type: "string", pattern: "^[A-Z]+$", patternFlags: "g" };
65+
66+
expect(check("John", s)).toEqual({ type: "stringPattern", expected: /^[A-Z]+$/g });
67+
expect(check("JOHN", s)).toEqual(true);
68+
});
69+
6370
it("check contains", () => {
6471
const s = { type: "string", contains: "bob" };
6572

test/validator.spec.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,63 @@ describe("Test compile (integration test)", () => {
301301

302302
});
303303

304+
describe("Test check generator with custom path & parent", () => {
305+
306+
it("when schema is defined as an array, and custom path & parent are specified, they should be forwarded to validators", () => {
307+
const v = new Validator();
308+
const customValidator = jest.fn().mockReturnValue(true); // Will be called with (value, schema, path, parent)
309+
v.add("customValidator", customValidator);
310+
311+
const validate = v.compile([{ type: "customValidator" }]);
312+
const parent = {};
313+
const res = validate({ customValue: 4711 }, "customPath", parent);
314+
315+
expect(res).toBe(true);
316+
expect(customValidator.mock.calls[0][2]).toBe("customPath");
317+
expect(customValidator.mock.calls[0][3]).toBe(parent);
318+
});
319+
320+
it("when schema is defined as an array, path & parent should be set to default values in validators", () => {
321+
const v = new Validator();
322+
const customValidator = jest.fn().mockReturnValue(true); // Will be called with (value, schema, path, parent)
323+
v.add("customValidator", customValidator);
324+
325+
const validate = v.compile([{ type: "customValidator" }]);
326+
const res = validate({ customValue: 4711 });
327+
328+
expect(res).toBe(true);
329+
expect(customValidator.mock.calls[0][2]).toBeUndefined();
330+
expect(customValidator.mock.calls[0][3]).toBeNull();
331+
});
332+
333+
it("when schema is defined as an object, and custom path is specified, it should be forwarded to validators", () => {
334+
// Note: as the item we validate always must be an object, there is no use
335+
// of specifying a custom parent, like for the schema-as-array above.
336+
// The parent is currently used in the validator code (only forwarded to the generated
337+
// function that validates all properties) and there is no way to test it.
338+
const v = new Validator();
339+
const customValidator = jest.fn().mockReturnValue(true); // Will be called with (value, schema, path, parent)
340+
v.add("customValidator", customValidator);
341+
342+
const validate = v.compile({ customValue: { type: "customValidator" } });
343+
const res = validate({ customValue: 4711 }, "customPath");
344+
345+
expect(res).toBe(true);
346+
expect(customValidator.mock.calls[0][2]).toBe("customPath.customValue");
347+
});
348+
349+
it("when schema is defined as an object, path should be set to default value in validators", () => {
350+
const v = new Validator();
351+
const customValidator = jest.fn().mockReturnValue(true); // Will be called with (value, schema, path, parent)
352+
v.add("customValidator", customValidator);
353+
354+
const validate = v.compile({ customValue: { type: "customValidator" } });
355+
const res = validate({ customValue: 4711 });
356+
357+
expect(res).toBe(true);
358+
expect(customValidator.mock.calls[0][2]).toBe("customValue");
359+
});
360+
});
304361
});
305362

306363
describe("Test nested schema", () => {

0 commit comments

Comments
 (0)