Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

Commit 0c8871b

Browse files
feat: adds "kind", "values" field properties, removes deprecations
BREAKING CHANGE: - Gavel fields no longer contain the following properties: - `validator` - `expectedType` - `realType` - `rawData` - Gavel fields now contain the next new properies: - `kind` - `values` (optional)
1 parent 9d281aa commit 0c8871b

14 files changed

+268
-543
lines changed

lib/units/validateBody.js

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,17 @@ function getBodyValidator(realType, expectedType) {
126126
};
127127

128128
const validators = [
129-
[TextDiff, both(isPlainText)],
129+
[TextDiff, both(isPlainText), 'text'],
130130
// List JsonSchema first, because weak predicate of JsonExample
131131
// would resolve on "application/schema+json" media type too.
132132
[
133133
JsonSchema,
134134
(real, expected) => {
135135
return isJson(real) && isJsonSchema(expected);
136-
}
136+
},
137+
'json'
137138
],
138-
[JsonExample, both(isJson)]
139+
[JsonExample, both(isJson), 'json']
139140
];
140141

141142
const validator = validators.find(([_name, predicate]) => {
@@ -146,10 +147,10 @@ function getBodyValidator(realType, expectedType) {
146147
const error = `Can't validate real media type '${mediaTyper.format(
147148
realType
148149
)}' against expected media type '${mediaTyper.format(expectedType)}'.`;
149-
return [error, null];
150+
return [error, null, null];
150151
}
151152

152-
return [null, validator[0]];
153+
return [null, validator[0], validator[2]];
153154
}
154155

155156
/**
@@ -161,6 +162,10 @@ function validateBody(expected, real) {
161162
const errors = [];
162163
const realBodyType = typeof real.body;
163164
const hasEmptyRealBody = real.body === '';
165+
const values = {
166+
expected: expected.body,
167+
actual: real.body
168+
};
164169

165170
// Throw when user input for real body is not a string.
166171
if (realBodyType !== 'string') {
@@ -185,22 +190,24 @@ function validateBody(expected, real) {
185190

186191
if (realTypeError) {
187192
errors.push({
188-
message: realTypeError
193+
message: realTypeError,
194+
values
189195
});
190196
}
191197

192198
if (expectedTypeError) {
193199
errors.push({
194-
message: expectedTypeError
200+
message: expectedTypeError,
201+
values
195202
});
196203
}
197204

198205
const hasErrors = errors.length > 0;
199206

200207
// Skipping body validation in case errors during
201208
// real/expected body type definition.
202-
const [validatorError, ValidatorClass] = hasErrors
203-
? [null, null]
209+
const [validatorError, ValidatorClass, kind] = hasErrors
210+
? [null, null, null]
204211
: getBodyValidator(realType, expectedType);
205212

206213
if (validatorError) {
@@ -213,11 +220,13 @@ function validateBody(expected, real) {
213220
errors.push({
214221
message: `Expected "body" of "${mediaTyper.format(
215222
expectedType
216-
)}" media type, but actual "body" is missing.`
223+
)}" media type, but actual "body" is missing.`,
224+
values
217225
});
218226
} else {
219227
errors.push({
220-
message: validatorError
228+
message: validatorError,
229+
values
221230
});
222231
}
223232
}
@@ -229,16 +238,15 @@ function validateBody(expected, real) {
229238
real.body,
230239
usesJsonSchema ? expected.bodySchema : expected.body
231240
);
232-
const rawData = validator && validator.validate();
241+
// Without ".validate()" it cannot evaluate output to result.
242+
// TODO Re-do this.
243+
validator && validator.validate();
233244
const validationErrors = validator ? validator.evaluateOutputToResults() : [];
234245
errors.push(...validationErrors);
235246

236247
return {
237-
isValid: isValidField({ errors }),
238-
validator: ValidatorClass && ValidatorClass.name,
239-
realType: mediaTyper.format(realType),
240-
expectedType: mediaTyper.format(expectedType),
241-
rawData,
248+
valid: isValidField({ errors }),
249+
kind,
242250
errors
243251
};
244252
}

lib/units/validateHeaders.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ function validateHeaders(expected, real) {
2626
const validator = hasJsonHeaders
2727
? new HeadersJsonExample(real.headers, expected.headers)
2828
: null;
29-
const rawData = validator && validator.validate();
29+
30+
// if you don't call ".validate()", it never evaluates any results.
31+
validator && validator.validate();
3032

3133
if (validator) {
3234
errors.push(...validator.evaluateOutputToResults());
@@ -42,11 +44,8 @@ and expected data media type
4244
}
4345

4446
return {
45-
isValid: isValidField({ errors }),
46-
validator: validator && 'HeadersJsonExample',
47-
realType,
48-
expectedType,
49-
rawData,
47+
valid: isValidField({ errors }),
48+
kind: hasJsonHeaders ? 'json' : 'text',
5049
errors
5150
};
5251
}

lib/units/validateMethod.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
const APIARY_METHOD_TYPE = 'text/vnd.apiary.method';
2-
31
function validateMethod(expected, real) {
42
const { method: expectedMethod } = expected;
53
const { method: realMethod } = real;
6-
const isValid = realMethod === expectedMethod;
4+
const valid = realMethod === expectedMethod;
75
const errors = [];
86

9-
if (!isValid) {
7+
if (!valid) {
108
errors.push({
11-
message: `Expected "method" field to equal "${expectedMethod}", but got "${realMethod}".`
9+
message: `Expected "method" field to equal "${expectedMethod}", but got "${realMethod}".`,
10+
values: {
11+
expected: expectedMethod,
12+
actual: realMethod
13+
}
1214
});
1315
}
1416

1517
return {
16-
isValid,
17-
validator: null,
18-
realType: APIARY_METHOD_TYPE,
19-
expectedType: APIARY_METHOD_TYPE,
18+
valid,
19+
kind: 'text',
2020
errors
2121
};
2222
}

lib/units/validateStatusCode.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
1-
const APIARY_STATUS_CODE_TYPE = 'text/vnd.apiary.status-code';
2-
31
/**
42
* Validates given real and expected status codes.
53
* @param {Object} real
64
* @param {number} expected
75
*/
86
function validateStatusCode(expected, real) {
9-
const isValid = real.statusCode === expected.statusCode;
7+
const valid = real.statusCode === expected.statusCode;
108
const errors = [];
119

12-
if (!isValid) {
10+
if (!valid) {
1311
errors.push({
14-
message: `Status code is '${real.statusCode}' instead of '${
15-
expected.statusCode
16-
}'`
12+
message: `Status code is '${real.statusCode}' instead of '${expected.statusCode}'`,
13+
values: {
14+
expected: expected.statusCode,
15+
actual: real.statusCode
16+
}
1717
});
1818
}
1919

2020
return {
21-
isValid,
22-
validator: 'TextDiff',
23-
realType: APIARY_STATUS_CODE_TYPE,
24-
expectedType: APIARY_STATUS_CODE_TYPE,
25-
rawData: '',
21+
valid,
22+
kind: 'text',
2623
errors
2724
};
2825
}

lib/units/validateURI.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
const url = require('url');
22
const deepEqual = require('deep-equal');
33

4-
const APIARY_URI_TYPE = 'text/vnd.apiary.uri';
5-
64
/**
75
* Parses the given URI and returns the properties
86
* elligible for comparison. Leaves out raw properties like "path"
@@ -20,32 +18,34 @@ const parseURI = (uri) => {
2018
};
2119
};
2220

23-
const validateURI = (expected, real) => {
21+
const validateURI = (expected, actual) => {
2422
const { uri: expectedURI } = expected;
25-
const { uri: realURI } = real;
23+
const { uri: actualURI } = actual;
2624

2725
// Parses URI to perform a correct comparison:
2826
// - literal comparison of pathname
2927
// - order-insensitive comparison of query parameters
30-
const parsedExpected = parseURI(expectedURI, true);
31-
const parsedReal = parseURI(realURI, true);
28+
const parsedExpected = parseURI(expectedURI);
29+
const parsedActual = parseURI(actualURI);
3230

3331
// Note the different order of arguments between
3432
// "validateURI" and "deepEqual".
35-
const isValid = deepEqual(parsedReal, parsedExpected);
33+
const valid = deepEqual(parsedActual, parsedExpected);
3634
const errors = [];
3735

38-
if (!isValid) {
36+
if (!valid) {
3937
errors.push({
40-
message: `Expected "uri" field to equal "${expectedURI}", but got: "${realURI}".`
38+
message: `Expected "uri" field to equal "${expectedURI}", but got: "${actualURI}".`,
39+
values: {
40+
expected: expectedURI,
41+
actual: actualURI
42+
}
4143
});
4244
}
4345

4446
return {
45-
isValid,
46-
validator: null,
47-
expectedType: APIARY_URI_TYPE,
48-
realType: APIARY_URI_TYPE,
47+
valid,
48+
kind: 'text',
4949
errors
5050
};
5151
};

lib/validate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function validate(expectedMessage, realMessage) {
4747
}
4848

4949
// Indicates the validity of the real message
50-
result.isValid = isValidResult(result);
50+
result.valid = isValidResult(result);
5151

5252
return result;
5353
}

0 commit comments

Comments
 (0)