Skip to content

Commit 3f41557

Browse files
authored
Merge pull request #57 from ispyinternet/master
Add feature to allow personalised error messages per field.
2 parents 52fbfce + 5f2f7a7 commit 3f41557

File tree

9 files changed

+323
-96
lines changed

9 files changed

+323
-96
lines changed

.editorconfig

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# EditorConfig helps developers define and maintain consistent
2+
# coding styles between different editors and IDEs
3+
# editorconfig.org
4+
5+
root = true
6+
7+
[*]
8+
9+
# Change these settings to your own preference
10+
indent_style = tab
11+
indent_size = 4
12+
space_after_anon_function = true
13+
14+
# We recommend you to keep these unchanged
15+
end_of_line = lf
16+
charset = utf-8
17+
trim_trailing_whitespace = true
18+
insert_final_newline = true
19+
20+
[*.md]
21+
trim_trailing_whitespace = false
22+
indent_style = space
23+
indent_size = 4
24+
25+
[{package,bower}.json]
26+
indent_style = space
27+
indent_size = 2
28+
29+
[*.js]
30+
quote_type = "double"

README.md

Lines changed: 71 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ You can install it via [NPM](http://npmjs.org/).
4848
```
4949
$ npm install fastest-validator --save
5050
```
51-
or
51+
or
5252
```
5353
$ yarn add fastest-validator
5454
```
5555

5656
## Usage
5757

5858
### Simple method
59-
Call the `validate` method with the `object` and the `schema`.
59+
Call the `validate` method with the `object` and the `schema`.
6060
> If performance is important, you won't use this method.
6161
6262
```js
@@ -76,7 +76,7 @@ console.log(v.validate({ id: 5, name: "John", status: true }, schema));
7676
console.log(v.validate({ id: 5, name: "Al", status: true }, schema));
7777
/* Returns an array with errors:
7878
[
79-
{
79+
{
8080
type: 'stringMin',
8181
expected: 3,
8282
actual: 2,
@@ -111,7 +111,7 @@ console.log(check({ id: 5, name: "John", status: true }));
111111
console.log(check({ id: 2, name: "Adam" }));
112112
/* Returns an array with errors:
113113
[
114-
{
114+
{
115115
type: 'required',
116116
field: 'status',
117117
message: 'The \'status\' field is required!'
@@ -187,7 +187,7 @@ v.validate({ prop: "John" }, schema); // Valid
187187
```
188188

189189
## `array`
190-
This is an `Array` validator.
190+
This is an `Array` validator.
191191

192192
**Simple example with strings:**
193193
```js
@@ -226,7 +226,7 @@ let schema = {
226226
} }
227227
}
228228

229-
v.validate({
229+
v.validate({
230230
users: [
231231
{ id: 1, name: "John", status: true },
232232
{ id: 2, name: "Jane", status: true },
@@ -259,7 +259,7 @@ v.validate({ roles: ["guest"] }, schema); // Fail
259259

260260

261261
## `boolean`
262-
This is a `Boolean` validator.
262+
This is a `Boolean` validator.
263263

264264
```js
265265
let schema = {
@@ -278,7 +278,7 @@ Property | Default | Description
278278

279279

280280
## `date`
281-
This is a `Date` validator.
281+
This is a `Date` validator.
282282

283283
```js
284284
let schema = {
@@ -295,7 +295,7 @@ Property | Default | Description
295295
`convert` | `false`| if `true` and the type is not `Date`, try to convert with `new Date()`.
296296

297297
## `email`
298-
This is an e-mail address validator.
298+
This is an e-mail address validator.
299299

300300
```js
301301
let schema = {
@@ -313,7 +313,7 @@ Property | Default | Description
313313
`mode` | `quick` | Checker method. Can be `quick` or `precise`.
314314

315315
## `enum`
316-
This is an enum validator.
316+
This is an enum validator.
317317

318318
```js
319319
let schema = {
@@ -332,7 +332,7 @@ Property | Default | Description
332332

333333

334334
## `forbidden`
335-
This validator returns an error if the property exists in the object.
335+
This validator returns an error if the property exists in the object.
336336

337337
```js
338338
let schema = {
@@ -393,15 +393,15 @@ let schema = {
393393
} }
394394
}
395395

396-
v.validate({
396+
v.validate({
397397
address: {
398398
country: "Italy",
399399
city: "Rome",
400400
zip: 12345
401-
}
401+
}
402402
}, schema); // Valid
403403

404-
v.validate({
404+
v.validate({
405405
address: {
406406
country: "Italy",
407407
city: "Rome"
@@ -440,7 +440,7 @@ Property | Default | Description
440440

441441

442442
## `url`
443-
This is an URL validator.
443+
This is an URL validator.
444444

445445
```js
446446
let schema = {
@@ -502,9 +502,9 @@ let v = new Validator({
502502

503503
const schema = {
504504
name: { type: "string", min: 3, max: 255 },
505-
weight: {
506-
type: "custom",
507-
minWeight: 10,
505+
weight: {
506+
type: "custom",
507+
minWeight: 10,
508508
check(value, schema) {
509509
return (value < schema.minWeight)
510510
? this.makeError("weightMin", schema.minWeight, value)
@@ -518,12 +518,12 @@ console.log(v.validate({ name: "John", weight: 50 }, schema));
518518

519519
console.log(v.validate({ name: "John", weight: 8 }, schema));
520520
/* Returns an array with errors:
521-
[{
522-
type: 'weightMin',
523-
expected: 10,
524-
actual: 8,
525-
field: 'weight',
526-
message: 'The weight must be greater than 10! Actual: 8'
521+
[{
522+
type: 'weightMin',
523+
expected: 10,
524+
actual: 8,
525+
field: 'weight',
526+
message: 'The weight must be greater than 10! Actual: 8'
527527
}]
528528
*/
529529
```
@@ -542,18 +542,61 @@ const v = new Validator({
542542

543543
v.validate({ name: "John" }, { name: { type: "string", min: 6 }});
544544
/* Returns:
545-
[
546-
{
545+
[
546+
{
547547
type: 'stringMin',
548548
expected: 6,
549549
actual: 4,
550550
field: 'name',
551-
message: 'A(z) \'name\' mező túl rövid. Minimum: 6, Jelenleg: 4'
552-
}
551+
message: 'A(z) \'name\' mező túl rövid. Minimum: 6, Jelenleg: 4'
552+
}
553553
]
554554
*/
555555
```
556+
# Personalised Messages
557+
Sometimes the standard messages are too generic. You can customise messages per validation type per field:
556558

559+
```js
560+
const Validator = require("fastest-validator");
561+
const v = new Validator();
562+
const schema = {
563+
firstname: {
564+
type: "string",
565+
min: 6,
566+
messages: {
567+
string: "Please check your firstname",
568+
stringMin: "Your firstname is too short"
569+
}
570+
},
571+
lastname: {
572+
type: "string",
573+
min: 6,
574+
messages: {
575+
string: "Please check your lastname",
576+
stringMin: "Your lastname is too short"
577+
}
578+
}
579+
}
580+
v.validate({ firstname: "John", lastname: 23 }, schema );
581+
/* Returns:
582+
[
583+
{
584+
type: 'stringMin',
585+
expected: 6,
586+
actual: 4,
587+
field: 'firstname',
588+
message: 'Your firstname is too short'
589+
},
590+
{
591+
type: 'string',
592+
expected: undefined,
593+
actual: undefined,
594+
field: 'lastname',
595+
message: 'Please check your lastname'
596+
}
597+
]
598+
*/
599+
```
557600
## Message types
558601
Name | Default text
559602
------------------- | -------------

benchmark/suites/simple.js

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,30 +32,55 @@ const schema = {
3232
},
3333
email: { type: "email" },
3434
firstName: { type: "string" },
35-
phone: { type: "string" },
35+
phone: { type: "string"},
36+
age: {
37+
type: "number",
38+
min: 18
39+
}
40+
};
41+
42+
const schema2 = {
43+
name: {
44+
type: "string",
45+
min: 4,
46+
max: 25,
47+
messages: {
48+
string: "Csak szöveges érték",
49+
stringMin: "Túl rövid!",
50+
stringMax: "Túl hosszú"
51+
}
52+
},
53+
email: { type: "email" },
54+
firstName: { type: "string" },
55+
phone: { type: "string"},
3656
age: {
3757
type: "number",
3858
min: 18
3959
}
4060
};
4161

4262
bench.ref("compile & validate", () => {
43-
let res = v.validate(obj, schema);
63+
const res = v.validate(obj, schema);
4464
if (res !== true)
4565
throw new Error("Validation error!", res);
4666
});
4767

68+
bench.add("compile & validate with custom messages", () => {
69+
const res = v.validate(obj, schema2);
70+
if (res !== true)
71+
throw new Error("Validation error!", res);
72+
});
4873

49-
let check = v.compile(schema);
74+
const check = v.compile(schema);
5075

5176
bench.add("validate with pre-compiled schema", () => {
52-
let res = check(obj);
77+
const res = check(obj);
5378
if (res !== true)
5479
throw new Error("Validation error!", res);
5580
});
5681

5782
bench.add("validate with wrong obj", () => {
58-
let res = check(wrongObj);
83+
const res = check(wrongObj);
5984
if (res === true)
6085
throw new Error("Validation error!", res);
6186
});
@@ -86,4 +111,4 @@ Suite: Simple object
86111
validate with wrong obj -36.79% (704,992 rps) (avg: 1μs)
87112
-----------------------------------------------------------------------
88113
89-
*/
114+
*/

lib/rules/email.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22

3-
const PRECISE_PATTERN = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
3+
const PRECISE_PATTERN = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
44
const BASIC_PATTERN = /^\S+@\S+\.\S+$/;
55

66
module.exports = function checkEmail(value, schema) {
@@ -13,7 +13,7 @@ module.exports = function checkEmail(value, schema) {
1313
pattern = PRECISE_PATTERN;
1414
else
1515
pattern = BASIC_PATTERN;
16-
16+
1717
if (!pattern.test(value)) {
1818
return this.makeError("email");
1919
}

lib/rules/string.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const NUMERIC_PATTERN = /^-?[0-9]\d*(\.\d+)?$/;
44
const ALPHA_PATTERN = /^[a-zA-Z]+$/;
55
const ALPHANUM_PATTERN = /^[a-zA-Z0-9]+$/;
6-
const ALPHADASH_PATTERN = /^[a-zA-Z0-9_\-]+$/;
6+
const ALPHADASH_PATTERN = /^[a-zA-Z0-9_-]+$/;
77

88
module.exports = function checkString(value, schema) {
99
if (typeof value !== "string") {
@@ -26,7 +26,7 @@ module.exports = function checkString(value, schema) {
2626

2727
if (schema.length != null && valueLength !== schema.length) {
2828
return this.makeError("stringLength", schema.length, valueLength);
29-
}
29+
}
3030

3131
if (schema.pattern != null) {
3232
const pattern = typeof schema.pattern == "string" ? new RegExp(schema.pattern, schema.patternFlags) : schema.pattern;
@@ -36,7 +36,7 @@ module.exports = function checkString(value, schema) {
3636

3737
if (schema.contains != null && value.indexOf(schema.contains) === -1) {
3838
return this.makeError("stringContains", schema.contains);
39-
}
39+
}
4040

4141
if (schema.enum != null && schema.enum.indexOf(value) === -1) {
4242
return this.makeError("stringEnum", schema.enum);
@@ -59,4 +59,4 @@ module.exports = function checkString(value, schema) {
5959
}
6060

6161
return true;
62-
};
62+
};

0 commit comments

Comments
 (0)