Skip to content

Commit 0d12259

Browse files
vankopevilebottnawi
authored andcommitted
fix: rework format for maxLength, minLength (#67)
1 parent 7fc8069 commit 0d12259

File tree

3 files changed

+34
-24
lines changed

3 files changed

+34
-24
lines changed

src/ValidationError.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -407,19 +407,24 @@ class ValidationError extends Error {
407407
if (typeof schema.minLength === 'number') {
408408
if (schema.minLength === 1) {
409409
type = 'non-empty string';
410-
} else {
410+
} else if (schema.minLength !== 0) {
411+
/* if min length === 0 it does not make hint for user */
412+
const length = schema.minLength - 1;
413+
411414
hints.push(
412-
`should not be shorter than ${schema.minLength} characters`
415+
`should be longer than ${length} character${length > 1 ? 's' : ''}`
413416
);
414417
}
415418
}
416419

417420
if (typeof schema.maxLength === 'number') {
418-
hints.push(
419-
`should not be longer than ${schema.maxLength} character${
420-
schema.maxLength > 1 ? 's' : ''
421-
}`
422-
);
421+
if (schema.maxLength === 0) {
422+
type = 'empty string';
423+
} else {
424+
hints.push(
425+
`should be shorter than ${schema.maxLength + 1} characters`
426+
);
427+
}
423428
}
424429

425430
if (schema.pattern) {
@@ -791,10 +796,11 @@ class ValidationError extends Error {
791796
error.parentSchema
792797
)}.${this.getSchemaPartDescription(error.parentSchema)}`;
793798
}
799+
const length = error.params.limit - 1;
794800

795-
return `${dataPath} should not be shorter than ${
796-
error.params.limit
797-
} characters${getSchemaNonTypes(
801+
return `${dataPath} should be longer than ${length} character${
802+
length > 1 ? 's' : ''
803+
}${getSchemaNonTypes(
798804
error.parentSchema
799805
)}.${this.getSchemaPartDescription(error.parentSchema)}`;
800806
}
@@ -825,9 +831,8 @@ class ValidationError extends Error {
825831
)}.${this.getSchemaPartDescription(error.parentSchema)}`;
826832
}
827833
case 'maxLength':
828-
return `${dataPath} should not be longer than ${
829-
error.params.limit
830-
} characters${getSchemaNonTypes(
834+
return `${dataPath} should be shorter than ${error.params.limit +
835+
1} characters${getSchemaNonTypes(
831836
error.parentSchema
832837
)}.${this.getSchemaPartDescription(error.parentSchema)}`;
833838
case 'maxItems':

test/__snapshots__/index.test.js.snap

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ exports[`Validation should fail validation for array with only number 1`] = `
411411
exports[`Validation should fail validation for array with string items and minLength 1`] = `
412412
"Invalid configuration object. Object has been initialised using a configuration object that does not match the API schema.
413413
- configuration.longString should be an array:
414-
[string (should not be shorter than 2 characters), ...]"
414+
[string (should be longer than 1 character), ...]"
415415
`;
416416
417417
exports[`Validation should fail validation for array without items 1`] = `
@@ -880,12 +880,12 @@ exports[`Validation should fail validation for maxItems Keyword 1`] = `
880880
881881
exports[`Validation should fail validation for maxLength 1`] = `
882882
"Invalid configuration object. Object has been initialised using a configuration object that does not match the API schema.
883-
- configuration.customObject.maxLength should not be longer than 3 characters."
883+
- configuration.customObject.maxLength should be shorter than 4 characters."
884884
`;
885885
886886
exports[`Validation should fail validation for maxLength with type string 1`] = `
887887
"Invalid configuration object. Object has been initialised using a configuration object that does not match the API schema.
888-
- configuration.stringWithMaxLength should not be longer than 2 characters."
888+
- configuration.stringWithMaxLength should be shorter than 3 characters."
889889
`;
890890
891891
exports[`Validation should fail validation for maxProperties 1`] = `
@@ -931,7 +931,7 @@ exports[`Validation should fail validation for maximum with type number 1`] = `
931931
932932
exports[`Validation should fail validation for min length 1`] = `
933933
"Invalid configuration object. Object has been initialised using a configuration object that does not match the API schema.
934-
- configuration.minLengthTwo should not be shorter than 2 characters."
934+
- configuration.minLengthTwo should be longer than 1 character."
935935
`;
936936
937937
exports[`Validation should fail validation for min properties 1`] = `
@@ -947,7 +947,7 @@ exports[`Validation should fail validation for minItems Keyword 1`] = `
947947
948948
exports[`Validation should fail validation for minLength with type string 1`] = `
949949
"Invalid configuration object. Object has been initialised using a configuration object that does not match the API schema.
950-
- configuration.stringWithMinLength should not be shorter than 2 characters."
950+
- configuration.stringWithMinLength should be longer than 1 character."
951951
`;
952952
953953
exports[`Validation should fail validation for minProperties 1`] = `
@@ -1177,12 +1177,12 @@ exports[`Validation should fail validation for no type like string with formatMa
11771177
11781178
exports[`Validation should fail validation for no type like string with maxLength 1`] = `
11791179
"Invalid configuration object. Object has been initialised using a configuration object that does not match the API schema.
1180-
- configuration.noTypeLikeStringMaxLength should not be longer than 2 characters | should be any non-string."
1180+
- configuration.noTypeLikeStringMaxLength should be shorter than 3 characters | should be any non-string."
11811181
`;
11821182
11831183
exports[`Validation should fail validation for no type like string with minLength 1`] = `
11841184
"Invalid configuration object. Object has been initialised using a configuration object that does not match the API schema.
1185-
- configuration.noTypeLikeStringMinLength should not be shorter than 2 characters | should be any non-string."
1185+
- configuration.noTypeLikeStringMinLength should be longer than 1 character | should be any non-string."
11861186
`;
11871187
11881188
exports[`Validation should fail validation for no type like string with minLength equals 1 1`] = `
@@ -1481,7 +1481,7 @@ exports[`Validation should fail validation for oneOf #1 1`] = `
14811481
exports[`Validation should fail validation for oneOf #2 1`] = `
14821482
"Invalid configuration object. Object has been initialised using a configuration object that does not match the API schema.
14831483
- configuration.optimization.runtimeChunk should be one of these:
1484-
boolean | \\"single\\" | \\"multiple\\" | object { name? }
1484+
boolean | \\"single\\" | \\"multiple\\" | empty string | object { name? }
14851485
-> Create an additional chunk which contains only the webpack runtime and chunk hash maps
14861486
Details:
14871487
* configuration.optimization.runtimeChunk.name should be one of these:
@@ -1495,12 +1495,13 @@ exports[`Validation should fail validation for oneOf #2 1`] = `
14951495
exports[`Validation should fail validation for oneOf #3 1`] = `
14961496
"Invalid configuration object. Object has been initialised using a configuration object that does not match the API schema.
14971497
- configuration.optimization.runtimeChunk should be one of these:
1498-
boolean | \\"single\\" | \\"multiple\\" | object { name? }
1498+
boolean | \\"single\\" | \\"multiple\\" | empty string | object { name? }
14991499
-> Create an additional chunk which contains only the webpack runtime and chunk hash maps
15001500
Details:
15011501
* configuration.optimization.runtimeChunk should be a boolean.
15021502
* configuration.optimization.runtimeChunk should be one of these:
15031503
\\"single\\" | \\"multiple\\"
1504+
* configuration.optimization.runtimeChunk should be a empty string.
15041505
* configuration.optimization.runtimeChunk should be an object:
15051506
object { name? }"
15061507
`;
@@ -1712,7 +1713,7 @@ exports[`Validation should fail validation for string #1 1`] = `
17121713
17131714
exports[`Validation should fail validation for string 1`] = `
17141715
"Invalid configuration object. Object has been initialised using a configuration object that does not match the API schema.
1715-
- configuration.stringKeyword should not be shorter than 11 characters."
1716+
- configuration.stringKeyword should be longer than 10 characters."
17161717
`;
17171718
17181719
exports[`Validation should fail validation for string with empty pattern 1`] = `
@@ -1727,7 +1728,7 @@ exports[`Validation should fail validation for string with minLength and maxLeng
17271728
17281729
exports[`Validation should fail validation for string with minLength, maxLength and pattern 1`] = `
17291730
"Invalid configuration object. Object has been initialised using a configuration object that does not match the API schema.
1730-
- configuration.stringWithMinAndMaxLength should be a string (should not be shorter than 2 characters, should not be longer than 5 characters, should match pattern \\"[bc]+\\")."
1731+
- configuration.stringWithMinAndMaxLength should be a string (should be longer than 1 character, should be shorter than 6 characters, should match pattern \\"[bc]+\\")."
17311732
`;
17321733
17331734
exports[`Validation should fail validation for terser-webpack-plugin name 1`] = `

test/fixtures/schema.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,10 @@
591591
{
592592
"enum": ["single", "multiple"]
593593
},
594+
{
595+
"type": "string",
596+
"maxLength": 0
597+
},
594598
{
595599
"type": "object",
596600
"additionalProperties": false,

0 commit comments

Comments
 (0)