Skip to content

Commit cfc8cd7

Browse files
committed
Single match object per condition, instead of array
1 parent 5286600 commit cfc8cd7

File tree

5 files changed

+53
-45
lines changed

5 files changed

+53
-45
lines changed

packages/dictionary/src/metaSchema/restrictionsSchemas.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,17 @@ export type MatchRuleRegex = zod.infer<typeof MatchRuleRegex>;
166166
export const MatchRuleValue = FieldValue;
167167
export type MatchRuleValue = zod.infer<typeof MatchRuleValue>;
168168

169-
export const ConditionMatchRule = zod
169+
export const MatchRule = zod.union([
170+
MatchRuleCodeList,
171+
MatchRuleCount,
172+
MatchRuleExists,
173+
MatchRuleRange,
174+
MatchRuleRegex,
175+
MatchRuleValue,
176+
]);
177+
export type MatchRule = zod.infer<typeof MatchRule>;
178+
179+
export const ConditionMatch = zod
170180
.object({
171181
codeList: MatchRuleCodeList,
172182
count: MatchRuleCount,
@@ -176,11 +186,11 @@ export const ConditionMatchRule = zod
176186
value: MatchRuleValue,
177187
})
178188
.partial();
179-
type ConditionMatchRule = zod.infer<typeof ConditionMatchRule>;
189+
type ConditionMatch = zod.infer<typeof ConditionMatch>;
180190

181191
export const RestrictionCondition = zod.object({
182192
fields: zod.string().array(),
183-
match: ConditionMatchRule.array(),
193+
match: ConditionMatch,
184194
case: ArrayTestCase.optional(),
185195
arrayFieldCase: ArrayTestCase.optional(),
186196
});

packages/dictionary/src/references.ts

+14-12
Original file line numberDiff line numberDiff line change
@@ -193,18 +193,20 @@ const replaceReferencesInStringRestrictionsObject = (
193193
if ('if' in restrictionsObject) {
194194
// Do replacements inside the if conditions
195195
restrictionsObject.if.conditions = restrictionsObject.if.conditions.map((condition) => {
196-
condition.match = condition.match.map((match) => {
197-
if (match.codeList && !isNumberArray(match.codeList)) {
198-
match.codeList = TypeUtils.asArray(resolveAllReferences(match.codeList, references, discovered, visited));
199-
}
200-
if (typeof match.value === 'string' || isStringArray(match.value)) {
201-
match.value = resolveAllReferences(match.value, references, discovered, visited);
202-
}
203-
if (match.regex) {
204-
match.regex = TypeUtils.asArray(resolveAllReferences(match.regex, references, discovered, visited));
205-
}
206-
return match;
207-
});
196+
if (condition.match.codeList && !isNumberArray(condition.match.codeList)) {
197+
condition.match.codeList = TypeUtils.asArray(
198+
resolveAllReferences(condition.match.codeList, references, discovered, visited),
199+
);
200+
}
201+
if (typeof condition.match.value === 'string' || isStringArray(condition.match.value)) {
202+
condition.match.value = resolveAllReferences(condition.match.value, references, discovered, visited);
203+
}
204+
if (condition.match.regex) {
205+
condition.match.regex = TypeUtils.asArray(
206+
resolveAllReferences(condition.match.regex, references, discovered, visited),
207+
);
208+
}
209+
208210
return condition;
209211
});
210212

packages/validation/src/validateField/conditions/testConditionalRestriction.ts

+26-29
Original file line numberDiff line numberDiff line change
@@ -45,41 +45,38 @@ const testConditionForSingularValue = (
4545
_value: SingleDataValue,
4646
fieldValues: DataRecordValue[],
4747
): boolean => {
48-
const results = condition.match.map((match) => {
49-
if (match.codeList) {
50-
if (!allValuesPassMatchTest(fieldValues, match.codeList, testMatchCodeList)) {
51-
return false;
52-
}
48+
if (condition.match.codeList) {
49+
if (!allValuesPassMatchTest(fieldValues, condition.match.codeList, testMatchCodeList)) {
50+
return false;
5351
}
54-
// count rule can have value of 0 so we need to directly check for undefined
55-
if (match.count !== undefined) {
56-
if (!allValuesPassMatchTest(fieldValues, match.count, testMatchCount)) {
57-
return false;
58-
}
52+
}
53+
// count rule can have value of 0 so we need to directly check for undefined
54+
if (condition.match.count !== undefined) {
55+
if (!allValuesPassMatchTest(fieldValues, condition.match.count, testMatchCount)) {
56+
return false;
5957
}
60-
if (match.exists) {
61-
if (!allValuesPassMatchTest(fieldValues, match.exists, testMatchExists)) {
62-
return false;
63-
}
58+
}
59+
if (condition.match.exists) {
60+
if (!allValuesPassMatchTest(fieldValues, condition.match.exists, testMatchExists)) {
61+
return false;
6462
}
65-
if (match.range) {
66-
if (!allValuesPassMatchTest(fieldValues, match.range, testMatchRange)) {
67-
return false;
68-
}
63+
}
64+
if (condition.match.range) {
65+
if (!allValuesPassMatchTest(fieldValues, condition.match.range, testMatchRange)) {
66+
return false;
6967
}
70-
if (match.regex) {
71-
if (!allValuesPassMatchTest(fieldValues, match.regex, testMatchRegex)) {
72-
return false;
73-
}
68+
}
69+
if (condition.match.regex) {
70+
if (!allValuesPassMatchTest(fieldValues, condition.match.regex, testMatchRegex)) {
71+
return false;
7472
}
75-
if (match.value) {
76-
if (!allValuesPassMatchTest(fieldValues, match.value, testMatchValue)) {
77-
return false;
78-
}
73+
}
74+
if (condition.match.value) {
75+
if (!allValuesPassMatchTest(fieldValues, condition.match.value, testMatchValue)) {
76+
return false;
7977
}
80-
return true;
81-
});
82-
return resultForArrayTestCase(results, condition.case || ARRAY_TEST_CASE_DEFAULT);
78+
}
79+
return true;
8380
};
8481

8582
const testConditionForArray = (

packages/validation/test/validateRecord/validateRecord.spec.ts

-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ describe('Record - validateRecord', () => {
143143
},
144144
schemaAllDataTypesMixedRestrictions,
145145
);
146-
console.log(JSON.stringify(result, null, 2));
147146
expect(result.valid).false;
148147
assert(result.valid === false);
149148

0 commit comments

Comments
 (0)