Skip to content

Commit f05f053

Browse files
authored
Merge pull request #5 from balavishnuvj/upgrade/fastest-validator
Updates: Fastest validator - 1.4.1
2 parents 13105d0 + 67002c8 commit f05f053

File tree

4 files changed

+57
-33
lines changed

4 files changed

+57
-33
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@
4646
"react": "^16.12.0"
4747
},
4848
"dependencies": {
49-
"fastest-validator": "0.6.19"
49+
"fastest-validator": "1.4.1"
5050
}
5151
}

src/constants.js

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,63 @@
1-
export const PER_FIELD_SCHEMA = {
2-
$$strict: true, // no additional properties allowed
3-
defaultValue: { type: 'any', optional: true },
1+
const PER_FIELD_CONFIG = {
2+
defaultValue: { type: "any", optional: true },
43
required: [
5-
{ type: 'boolean', optional: true },
4+
{ type: "boolean", optional: true },
65
{
7-
type: 'object',
6+
type: "object",
87
optional: true,
98
props: {
10-
errorMsg: { type: 'string' },
9+
errorMsg: { type: "string" },
1110
},
1211
},
1312
],
1413
min: [
15-
{ type: 'number', optional: true, positive: true, integer: true },
14+
{ type: "number", optional: true, positive: true, integer: true },
1615
{
17-
type: 'object',
16+
type: "object",
1817
optional: true,
1918
props: {
20-
errorMsg: { type: 'string' },
21-
length: { type: 'number', positive: true, integer: true },
19+
errorMsg: { type: "string" },
20+
length: { type: "number", positive: true, integer: true },
2221
},
2322
},
2423
],
2524
max: [
26-
{ type: 'number', optional: true, positive: true, integer: true },
25+
{ type: "number", optional: true, positive: true, integer: true },
2726
{
28-
type: 'object',
27+
type: "object",
2928
optional: true,
3029
props: {
31-
errorMsg: { type: 'string' },
32-
length: { type: 'number', positive: true, integer: true },
30+
errorMsg: { type: "string" },
31+
length: { type: "number", positive: true, integer: true },
3332
},
3433
},
3534
],
3635
patterns: {
37-
type: 'array',
36+
type: "array",
3837
items: {
39-
type: 'object',
38+
type: "object",
4039
props: {
41-
regex: { type: 'regex' },
42-
errorMsg: { type: 'string' },
40+
regex: { type: "regex" },
41+
errorMsg: { type: "string" },
4342
},
4443
},
4544
optional: true,
4645
},
47-
validationFns: { type: 'array', items: 'function', optional: true },
46+
validationFns: { type: "array", items: "function", optional: true },
47+
};
48+
49+
export const PER_FIELD_SCHEMA = {
50+
$$strict: true, // no additional properties allowed
51+
...PER_FIELD_CONFIG,
4852
};
53+
4954
export const FIELD_CONFIG_SCHEMA = {
55+
$$strict: true, // no additional properties allowed
5056
configs: {
51-
type: 'array',
57+
type: "array",
5258
items: {
53-
type: 'object',
54-
props: PER_FIELD_SCHEMA,
59+
type: "object",
60+
props: PER_FIELD_CONFIG,
5561
},
5662
},
5763
};

src/schema-validator.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
1-
import Validator from 'fastest-validator';
1+
import Validator from "fastest-validator";
22

3-
const validator = new Validator();
3+
const validator = new Validator({
4+
messages: {
5+
regex: "The '{field}' field must be an valid regex! Actual: {actual}",
6+
},
7+
});
48

59
// Register a custom 'regex' validator
6-
validator.add('regex', (value) => {
7-
if (!(value instanceof RegExp))
8-
return validator.makeError('regex', null, value);
9-
return true;
10+
// validator.add("regex", (value) => {
11+
// if (!(value instanceof RegExp))
12+
// return validator.makeError("regex", null, value);
13+
// return true;
14+
// });
15+
validator.add("regex", function ({ schema, messages }, path, context) {
16+
return {
17+
source: `
18+
if (!(value instanceof RegExp))
19+
${this.makeError({
20+
type: "regex",
21+
actual: "value",
22+
messages,
23+
})}
24+
25+
return value;
26+
`,
27+
};
1028
});
1129
export function getSchemaValidator(schema) {
1230
return validator.compile(schema);

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1886,10 +1886,10 @@ fast-levenshtein@~2.0.6:
18861886
resolved "https://registry.yarnpkg.com/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz#3d8a5c66883a16a30ca8643e851f19baa7797917"
18871887
integrity sha1-PYpcZog6FqMMqGQ+hR8Zuqd5eRc=
18881888

1889-
fastest-validator@0.6.19:
1890-
version "0.6.19"
1891-
resolved "https://registry.yarnpkg.com/fastest-validator/-/fastest-validator-0.6.19.tgz#5522cf29a13090df71862c7df4b29d1f49165a91"
1892-
integrity sha512-U+hjn/Au+sgGfrwb26t26hywrItCdkpQ7VEJPY7Ey0FU2mnH/L4xhcs0R9Bd+y57aUKlasC3W4J6t8fOyXji6Q==
1889+
fastest-validator@1.4.1:
1890+
version "1.4.1"
1891+
resolved "https://registry.yarnpkg.com/fastest-validator/-/fastest-validator-1.4.1.tgz#a49bb73bbb1cac3e94f890fb91f2f264a0957413"
1892+
integrity sha512-KOs52Z8HjcTPXzgxy9O0drpeuYq0epry/sZMIBwNOYB9bUyRgeGeyqbEsfX7HvIsBT8Yfb7LwgenaQ2obHF40Q==
18931893

18941894
fb-watchman@^2.0.0:
18951895
version "2.0.1"

0 commit comments

Comments
 (0)