@platecms/delta-validation 1.18.0 → 1.20.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.18.0",
3
+ "version": "1.20.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.18.0",
21
- "@platecms/delta-plate-resource-notation": "1.18.0",
22
- "@platecms/delta-types": "1.18.0",
20
+ "@platecms/delta-cast": "1.20.0",
21
+ "@platecms/delta-plate-resource-notation": "1.20.0",
22
+ "@platecms/delta-types": "1.20.0",
23
23
  "class-transformer": "0.5.1",
24
24
  "reflect-metadata": "0.2.2",
25
25
  "tslib": "2.8.1"
@@ -1,3 +1,4 @@
1
+ import { Root } from "@platecms/delta-cast";
1
2
  import { PRN } from "@platecms/delta-plate-resource-notation";
2
3
  import { ContentValueTypeNames } from "@platecms/delta-types";
3
4
  import { RuleType } from "../enums/rule-types.enum";
@@ -77,6 +78,41 @@ describe("ruleToSchema", () => {
77
78
  const validationResult = schema.validate(["invalid-email"], { path: ["test"] });
78
79
  expect(validationResult.isValid).toBe(false);
79
80
  });
81
+
82
+ it("ignores values that are not strings, so a link or smart text value can be saved", () => {
83
+ // Arrange
84
+ const rule = {
85
+ ruleType: RuleType.STRING_FORMAT,
86
+ settings: { allowedFormat: "^https?://" },
87
+ prn: mockPrn,
88
+ };
89
+ const pathPart = PRN.fromString("prn:plate:-1:cms:path-part:1");
90
+ const smartText: Root = { type: "root", children: [] };
91
+
92
+ // Act
93
+ const validationResult = ruleToSchema(rule).validate([pathPart, smartText], { path: ["test"] });
94
+
95
+ // Assert
96
+ expect(validationResult).toEqual({ isValid: true, errors: [] });
97
+ });
98
+
99
+ it("still rejects a non-matching string next to values of other types", () => {
100
+ // Arrange
101
+ const rule = {
102
+ ruleType: RuleType.STRING_FORMAT,
103
+ settings: { allowedFormat: "^https?://" },
104
+ prn: mockPrn,
105
+ };
106
+ const pathPart = PRN.fromString("prn:plate:-1:cms:path-part:1");
107
+
108
+ // Act
109
+ const validationResult = ruleToSchema(rule).validate([pathPart, "not a url"], { path: ["test"] });
110
+
111
+ // Assert
112
+ expect(validationResult.isValid).toBe(false);
113
+ expect(validationResult.errors).toHaveLength(1);
114
+ expect(validationResult.errors[0].message).toBe("String does not match the required format");
115
+ });
80
116
  });
81
117
 
82
118
  describe("NUMBER_BETWEEN rule", () => {
@@ -217,6 +253,24 @@ describe("ruleToSchema", () => {
217
253
  });
218
254
  });
219
255
 
256
+ describe("rules for one value type", () => {
257
+ it.each([
258
+ { ruleType: RuleType.NUMBER_BETWEEN, settings: { min: 0, max: 100 } },
259
+ { ruleType: RuleType.DECIMAL_COUNT, settings: { max: 2 } },
260
+ { ruleType: RuleType.DATE_BETWEEN, settings: { start: new Date("2023-01-01"), end: new Date("2023-12-31") } },
261
+ ])("$ruleType ignores values of other types", ({ ruleType, settings }) => {
262
+ // Arrange
263
+ const rule = { ruleType, settings, prn: mockPrn };
264
+ const values = ["text", PRN.fromString("prn:plate:-1:cms:path-part:1")];
265
+
266
+ // Act
267
+ const validationResult = ruleToSchema(rule).validate(values, { path: ["test"] });
268
+
269
+ // Assert
270
+ expect(validationResult).toEqual({ isValid: true, errors: [] });
271
+ });
272
+ });
273
+
220
274
  describe("VALUE_TYPE rule", () => {
221
275
  it("converts VALUE_TYPE rule to array schema with inner content-value schema", () => {
222
276
  const ruleInstance = {
@@ -1,5 +1,5 @@
1
1
  import { PRN } from "@platecms/delta-plate-resource-notation";
2
- import { ContentValueType } from "@platecms/delta-types";
2
+ import { ContentValueType, ContentValueTypeName, ContentValueTypeNames } from "@platecms/delta-types";
3
3
  import { RuleType } from "../enums/rule-types.enum";
4
4
  import { RuleInstance } from "../types/rule-instance.interface";
5
5
  import { AllowedValuesValidationRuleSettings } from "../validation-rules/allowed-values.validation-rule";
@@ -15,6 +15,21 @@ import { ContentValueValidationSchema } from "../schemas/content-value.schema";
15
15
  import { StringValidationSchema } from "../schemas/string.schema";
16
16
  import { NumberValidationSchema } from "../schemas/number.schema";
17
17
  import { DateValidationSchema } from "../schemas/date.schema";
18
+ import { ValidationSchema } from "../types/validation-schema.interface";
19
+
20
+ /**
21
+ * Applies a schema only to the values of one type, as fieldsToSchema does: a field can allow several types
22
+ * (a link, or smart text, next to plain text), and checking that every value has an allowed type is the VALUE_TYPE
23
+ * rule's job, not this rule's.
24
+ */
25
+ function ofValueType(
26
+ valueType: ContentValueTypeName<ContentValueType>,
27
+ schema: ValidationSchema<ContentValueType>,
28
+ ): ArrayValidationSchema<ContentValueType> {
29
+ return new ArrayValidationSchema<ContentValueType>().of(
30
+ new ContentValueValidationSchema().subSchema(valueType, schema),
31
+ );
32
+ }
18
33
 
19
34
  export function ruleToSchema(ruleInstance: RuleInstance<unknown>): ArrayValidationSchema<ContentValueType> {
20
35
  switch (ruleInstance.ruleType) {
@@ -34,7 +49,8 @@ export function ruleToSchema(ruleInstance: RuleInstance<unknown>): ArrayValidati
34
49
 
35
50
  case RuleType.STRING_FORMAT: {
36
51
  const formatSettings = ruleInstance.settings as StringFormatValidationRuleSettings;
37
- return new ArrayValidationSchema<ContentValueType>().of(
52
+ return ofValueType(
53
+ ContentValueTypeNames.STRING,
38
54
  new StringValidationSchema(ruleInstance).format(formatSettings.allowedFormat),
39
55
  );
40
56
  }
@@ -50,7 +66,7 @@ export function ruleToSchema(ruleInstance: RuleInstance<unknown>): ArrayValidati
50
66
  numberSchema.max(numberSettings.max);
51
67
  }
52
68
 
53
- return new ArrayValidationSchema<ContentValueType>().of(numberSchema);
69
+ return ofValueType(ContentValueTypeNames.NUMBER, numberSchema);
54
70
  }
55
71
 
56
72
  case RuleType.DATE_BETWEEN: {
@@ -64,12 +80,13 @@ export function ruleToSchema(ruleInstance: RuleInstance<unknown>): ArrayValidati
64
80
  dateSchema.end(dateSettings.end);
65
81
  }
66
82
 
67
- return new ArrayValidationSchema<ContentValueType>().of(dateSchema);
83
+ return ofValueType(ContentValueTypeNames.DATE, dateSchema);
68
84
  }
69
85
 
70
86
  case RuleType.DECIMAL_COUNT: {
71
87
  const decimalSettings = ruleInstance.settings as DecimalCountValidationRuleSettings;
72
- return new ArrayValidationSchema<ContentValueType>().of(
88
+ return ofValueType(
89
+ ContentValueTypeNames.NUMBER,
73
90
  new NumberValidationSchema(ruleInstance).maxDecimals(decimalSettings.max),
74
91
  );
75
92
  }