Skip to content

Commit a19ed69

Browse files
committed
chore: rename validation utility functions for clarity and consistency, improve JSDoc descriptions (#3886)
Signed-off-by: Michał Walczak <[email protected]>
1 parent df81ef7 commit a19ed69

File tree

1 file changed

+46
-16
lines changed

1 file changed

+46
-16
lines changed

packages/server/tests/acceptance/data/conformity/utils/validations.ts

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export function hasResponseFormatIssues(
5555
actualResponse: Record<string, unknown> | ErrorResponse | JsonRpcResponse,
5656
expectedResponse: Record<string, unknown> | string | ErrorResponse,
5757
wildcards: string[] = [],
58-
) {
58+
): boolean {
5959
let parsedExpectedResponse: Record<string, unknown> | ErrorResponse = expectedResponse as Record<string, unknown>;
6060
if (typeof expectedResponse === 'string') {
6161
try {
@@ -90,13 +90,17 @@ export function hasResponseFormatIssues(
9090
return true;
9191
}
9292

93-
return areValuesMatching(actualResponse, parsedExpectedResponse, wildcards);
93+
return hasValuesMismatch(actualResponse, parsedExpectedResponse, wildcards);
9494
}
9595

9696
/**
97-
* Checks if the actual response has the required error properties
97+
* Checks if the actual response is missing required error properties
98+
*
99+
* @param actual - The actual response to check
100+
* @param expected - The expected error response
101+
* @returns {boolean} - Returns true if error properties are missing or mismatched, false if all required properties exist
98102
*/
99-
function checkErrorResponse(actual: Record<string, unknown>, expected: ErrorResponse): boolean {
103+
function hasErrorResponseMismatch(actual: Record<string, unknown>, expected: ErrorResponse): boolean {
100104
if (!actual || typeof actual !== 'object' || !actual.error) {
101105
return true;
102106
}
@@ -115,24 +119,36 @@ function arePrimitivesDifferent(actual: unknown, expected: unknown): boolean {
115119

116120
/**
117121
* Checks if two arrays have different values
122+
*
123+
* @param actual - The actual array from the response
124+
* @param expected - The expected array to compare against
125+
* @param wildcards - Array of property paths to ignore during comparison
126+
* @param path - Current property path being evaluated
127+
* @returns {boolean} - Returns true if arrays have different values, false if they match
118128
*/
119-
function checkArrayValues(actual: unknown[], expected: unknown[], wildcards: string[], path: string): boolean {
129+
function hasArrayValuesMismatch(actual: unknown[], expected: unknown[], wildcards: string[], path: string): boolean {
120130
if (actual.length !== expected.length) {
121131
return true;
122132
}
123133

124134
for (let i = 0; i < expected.length; i++) {
125-
if (areValuesMatching(actual[i], expected[i], wildcards, `${path}[${i}]`)) {
135+
if (hasValuesMismatch(actual[i], expected[i], wildcards, `${path}[${i}]`)) {
126136
return true;
127137
}
128138
}
129139
return false;
130140
}
131141

132142
/**
133-
* Checks if an object has all the required properties with matching values
143+
* Checks if an object is missing required properties or has mismatched values
144+
*
145+
* @param actual - The actual object from the response
146+
* @param expected - The expected object to compare against
147+
* @param wildcards - Array of property paths to ignore during comparison
148+
* @param path - Current property path being evaluated
149+
* @returns {boolean} - Returns true if properties are missing or values are mismatched, false if all match
134150
*/
135-
function checkObjectProperties(
151+
function hasObjectPropertiesMismatch(
136152
actual: Record<string, unknown>,
137153
expected: Record<string, unknown>,
138154
wildcards: string[],
@@ -146,14 +162,23 @@ function checkObjectProperties(
146162
if (!(key in actual)) {
147163
return true;
148164
}
149-
if (areValuesMatching(actual[key], expected[key], wildcards, newPath)) {
165+
if (hasValuesMismatch(actual[key], expected[key], wildcards, newPath)) {
150166
return true;
151167
}
152168
}
153169
return false;
154170
}
155171

156-
function checkComplexTypes(actual: object | null, expected: object, wildcards: string[], path: string): boolean {
172+
/**
173+
* Checks if complex types (objects or arrays) have mismatches in their structure or values
174+
*
175+
* @param actual - The actual object/array from the response
176+
* @param expected - The expected object/array to compare against
177+
* @param wildcards - Array of property paths to ignore during comparison
178+
* @param path - Current property path being evaluated
179+
* @returns {boolean} - Returns true if mismatches are found, false if values match
180+
*/
181+
function hasComplexTypeMismatch(actual: object | null, expected: object, wildcards: string[], path: string): boolean {
157182
if (actual === null) {
158183
return true;
159184
}
@@ -166,10 +191,15 @@ function checkComplexTypes(actual: object | null, expected: object, wildcards: s
166191
}
167192

168193
if (isExpectedArray) {
169-
return checkArrayValues(actual as unknown[], expected as unknown[], wildcards, path);
194+
return hasArrayValuesMismatch(actual as unknown[], expected as unknown[], wildcards, path);
170195
}
171196

172-
return checkObjectProperties(actual as Record<string, unknown>, expected as Record<string, unknown>, wildcards, path);
197+
return hasObjectPropertiesMismatch(
198+
actual as Record<string, unknown>,
199+
expected as Record<string, unknown>,
200+
wildcards,
201+
path,
202+
);
173203
}
174204

175205
/**
@@ -186,12 +216,12 @@ function checkComplexTypes(actual: object | null, expected: object, wildcards: s
186216
* - Error responses: Validates error structure when expected response contains an error property
187217
* - Null/undefined values: Handles null checks appropriately
188218
* - Type mismatches: Returns true (different) when types don't match
189-
* - Complex objects: Delegates to checkComplexTypes for arrays and objects
219+
* - Complex objects: Delegates to hasComplexTypeMismatch for arrays and objects
190220
* - Primitive values: Uses direct comparison for primitive types
191221
*/
192-
function areValuesMatching(actual: unknown, expected: unknown, wildcards: string[], path = ''): boolean {
222+
function hasValuesMismatch(actual: unknown, expected: unknown, wildcards: string[], path = ''): boolean {
193223
if (path === '' && expected && typeof expected === 'object' && (expected as ErrorResponse).error) {
194-
return checkErrorResponse(actual as Record<string, unknown>, expected as ErrorResponse);
224+
return hasErrorResponseMismatch(actual as Record<string, unknown>, expected as ErrorResponse);
195225
}
196226

197227
if (expected == null) {
@@ -203,7 +233,7 @@ function areValuesMatching(actual: unknown, expected: unknown, wildcards: string
203233
}
204234

205235
if (typeof expected === 'object') {
206-
return checkComplexTypes(actual as object | null, expected, wildcards, path);
236+
return hasComplexTypeMismatch(actual as object | null, expected, wildcards, path);
207237
}
208238

209239
return arePrimitivesDifferent(actual, expected);

0 commit comments

Comments
 (0)