@platecms/delta-validation 1.12.1 → 1.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@platecms/delta-validation",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.13.0",
|
|
4
4
|
"description": "This package contains the validation system for Plate.",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"publishConfig": {
|
|
@@ -17,9 +17,9 @@
|
|
|
17
17
|
"src/**/*"
|
|
18
18
|
],
|
|
19
19
|
"peerDependencies": {
|
|
20
|
-
"@platecms/delta-cast": "1.
|
|
21
|
-
"@platecms/delta-plate-resource-notation": "1.
|
|
22
|
-
"@platecms/delta-types": "1.
|
|
20
|
+
"@platecms/delta-cast": "1.13.0",
|
|
21
|
+
"@platecms/delta-plate-resource-notation": "1.13.0",
|
|
22
|
+
"@platecms/delta-types": "1.13.0",
|
|
23
23
|
"class-transformer": "0.5.1",
|
|
24
24
|
"reflect-metadata": "0.2.2",
|
|
25
25
|
"tslib": "2.8.1"
|
|
@@ -51,7 +51,7 @@ describe("ArrayValidationSchema", () => {
|
|
|
51
51
|
const result = schema.validate([1, 2] as ContentValueType[], { path: ["test"] });
|
|
52
52
|
expect(result.isValid).toBe(false);
|
|
53
53
|
expect(result.errors).toHaveLength(1);
|
|
54
|
-
expect(result.errors[0].message).
|
|
54
|
+
expect(result.errors[0].message).toBe("The number of provided values must be greater than or equal to 3.");
|
|
55
55
|
});
|
|
56
56
|
|
|
57
57
|
it("fails validation when array length is above maximum", () => {
|
|
@@ -59,7 +59,7 @@ describe("ArrayValidationSchema", () => {
|
|
|
59
59
|
const result = schema.validate([1, 2, 3] as ContentValueType[], { path: ["test"] });
|
|
60
60
|
expect(result.isValid).toBe(false);
|
|
61
61
|
expect(result.errors).toHaveLength(1);
|
|
62
|
-
expect(result.errors[0].message).
|
|
62
|
+
expect(result.errors[0].message).toBe("The number of provided values must be less than or equal to 2.");
|
|
63
63
|
});
|
|
64
64
|
|
|
65
65
|
it("passes validation when no min/max constraints are set", () => {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { PRN } from "@platecms/delta-plate-resource-notation";
|
|
2
2
|
import { InvalidValidationRuleSettingsError } from "../errors/invalid-validation-rule-settings.error";
|
|
3
|
+
import { ValidationErrorDetails } from "../errors/validation-error-details";
|
|
3
4
|
import { CountValidationRule } from "./count.validation-rule";
|
|
4
5
|
import { ValidationRuleUpdateImpact } from "../enums/validation-rule-update-impact.enum";
|
|
5
6
|
|
|
@@ -31,6 +32,30 @@ describe("CountValidationRule", () => {
|
|
|
31
32
|
])("returns %s if the value count is within the bounds", (value, expected) => {
|
|
32
33
|
expect(rule.validate(value)).toBeContentValidationResult([expected]);
|
|
33
34
|
});
|
|
35
|
+
|
|
36
|
+
it("returns a required-field message when min is 1 and no values are provided", () => {
|
|
37
|
+
// Arrange & Act
|
|
38
|
+
const [result] = rule.validate([]);
|
|
39
|
+
|
|
40
|
+
// Assert
|
|
41
|
+
expect(result).toBeInstanceOf(ValidationErrorDetails);
|
|
42
|
+
if (!(result instanceof ValidationErrorDetails)) {
|
|
43
|
+
throw new Error("Expected ValidationErrorDetails");
|
|
44
|
+
}
|
|
45
|
+
expect(result.message).toBe("This is a required field");
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("returns a max count message when too many values are provided", () => {
|
|
49
|
+
// Arrange & Act
|
|
50
|
+
const [result] = rule.validate([1, 2, 3, 4]);
|
|
51
|
+
|
|
52
|
+
// Assert
|
|
53
|
+
expect(result).toBeInstanceOf(ValidationErrorDetails);
|
|
54
|
+
if (!(result instanceof ValidationErrorDetails)) {
|
|
55
|
+
throw new Error("Expected ValidationErrorDetails");
|
|
56
|
+
}
|
|
57
|
+
expect(result.message).toBe("The number of provided values must be less than or equal to 3.");
|
|
58
|
+
});
|
|
34
59
|
});
|
|
35
60
|
|
|
36
61
|
describe("with only min", () => {
|
|
@@ -49,6 +74,23 @@ describe("CountValidationRule", () => {
|
|
|
49
74
|
});
|
|
50
75
|
});
|
|
51
76
|
|
|
77
|
+
describe("with min greater than 1", () => {
|
|
78
|
+
it("returns a count message when fewer than the minimum values are provided", () => {
|
|
79
|
+
// Arrange
|
|
80
|
+
const rule = new CountValidationRule(rulePrn, { min: 2 });
|
|
81
|
+
|
|
82
|
+
// Act
|
|
83
|
+
const [result] = rule.validate([1]);
|
|
84
|
+
|
|
85
|
+
// Assert
|
|
86
|
+
expect(result).toBeInstanceOf(ValidationErrorDetails);
|
|
87
|
+
if (!(result instanceof ValidationErrorDetails)) {
|
|
88
|
+
throw new Error("Expected ValidationErrorDetails");
|
|
89
|
+
}
|
|
90
|
+
expect(result.message).toBe("The number of provided values must be greater than or equal to 2.");
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
52
94
|
describe("with only max", () => {
|
|
53
95
|
let rule: CountValidationRule;
|
|
54
96
|
|
|
@@ -7,21 +7,39 @@ import { RuleType } from "../enums/rule-types.enum";
|
|
|
7
7
|
import { BaseValidationRule, ContentValidationResultType } from "./base.validation-rule";
|
|
8
8
|
import { ValidationRuleUpdateImpact } from "../enums/validation-rule-update-impact.enum";
|
|
9
9
|
|
|
10
|
+
const REQUIRED_FIELD_MESSAGE = "This is a required field";
|
|
11
|
+
|
|
12
|
+
function minValuesCountMessage(min: number): string {
|
|
13
|
+
if (min === 1) {
|
|
14
|
+
return REQUIRED_FIELD_MESSAGE;
|
|
15
|
+
}
|
|
16
|
+
return `The number of provided values must be greater than or equal to ${min}.`;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function maxValuesCountMessage(max: number): string {
|
|
20
|
+
return `The number of provided values must be less than or equal to ${max}.`;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function minKeysCountMessage(min: number): string {
|
|
24
|
+
if (min === 1) {
|
|
25
|
+
return REQUIRED_FIELD_MESSAGE;
|
|
26
|
+
}
|
|
27
|
+
return `The number of provided object keys must be greater than or equal to ${min}.`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function maxKeysCountMessage(max: number): string {
|
|
31
|
+
return `The number of provided object keys must be less than or equal to ${max}.`;
|
|
32
|
+
}
|
|
33
|
+
|
|
10
34
|
export function validateValuesCount<T>(
|
|
11
35
|
values: T[],
|
|
12
36
|
ruleInstance: RuleInstance<CountValidationRuleSettings>,
|
|
13
37
|
): ContentValidationResultType {
|
|
14
38
|
if (ruleInstance.settings.min != null && values.length < ruleInstance.settings.min) {
|
|
15
|
-
return new ValidationErrorDetails(
|
|
16
|
-
`The amount of provided values must be greater than or equal ${ruleInstance.settings.min}.`,
|
|
17
|
-
ruleInstance,
|
|
18
|
-
);
|
|
39
|
+
return new ValidationErrorDetails(minValuesCountMessage(ruleInstance.settings.min), ruleInstance);
|
|
19
40
|
}
|
|
20
41
|
if (ruleInstance.settings.max != null && values.length > ruleInstance.settings.max) {
|
|
21
|
-
return new ValidationErrorDetails(
|
|
22
|
-
`The amount of provided values must be be less than or equal ${ruleInstance.settings.max}.`,
|
|
23
|
-
ruleInstance,
|
|
24
|
-
);
|
|
42
|
+
return new ValidationErrorDetails(maxValuesCountMessage(ruleInstance.settings.max), ruleInstance);
|
|
25
43
|
}
|
|
26
44
|
return true;
|
|
27
45
|
}
|
|
@@ -33,16 +51,10 @@ export function validateKeysCount<T>(
|
|
|
33
51
|
const indexableValue = value as unknown as Record<string, unknown>;
|
|
34
52
|
|
|
35
53
|
if (ruleInstance.settings.min != null && Object.keys(indexableValue).length < ruleInstance.settings.min) {
|
|
36
|
-
return new ValidationErrorDetails(
|
|
37
|
-
`The amount of provided object keys must be greater than or equal ${ruleInstance.settings.min}.`,
|
|
38
|
-
ruleInstance,
|
|
39
|
-
);
|
|
54
|
+
return new ValidationErrorDetails(minKeysCountMessage(ruleInstance.settings.min), ruleInstance);
|
|
40
55
|
}
|
|
41
56
|
if (ruleInstance.settings.max != null && Object.keys(indexableValue).length > ruleInstance.settings.max) {
|
|
42
|
-
return new ValidationErrorDetails(
|
|
43
|
-
`The amount of provided object keys must be be less than or equal ${ruleInstance.settings.max}.`,
|
|
44
|
-
ruleInstance,
|
|
45
|
-
);
|
|
57
|
+
return new ValidationErrorDetails(maxKeysCountMessage(ruleInstance.settings.max), ruleInstance);
|
|
46
58
|
}
|
|
47
59
|
return true;
|
|
48
60
|
}
|