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

Commit 66e3e4e

Browse files
Merge pull request #259 from apiaryio/validate-body-polishing
Adopts "actual" terminology in "validateBody"
2 parents d52b08d + 1a6a76d commit 66e3e4e

File tree

6 files changed

+40
-51
lines changed

6 files changed

+40
-51
lines changed

lib/units/validateBody.js

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function isJsonContentType(contentType) {
6262
* on the given body and normalized headers.
6363
* @param {string} body
6464
* @param {string} contentType
65-
* @param {'real'|'expected'} httpMessageOrigin
65+
* @param {'actual'|'expected'} httpMessageOrigin
6666
* @returns {[string, MediaType]}
6767
*/
6868
function getBodyType(body, contentType, httpMessageOrigin) {
@@ -116,14 +116,14 @@ ${error.message}\
116116

117117
/**
118118
* Returns a body validator class based on the given
119-
* real and expected body media types.
120-
* @param {MediaType} realType
119+
* actual and expected body media types.
121120
* @param {MediaType} expectedType
121+
* @param {MediaType} actualType
122122
* @returns {Validator}
123123
*/
124-
function getBodyValidator(realType, expectedType) {
125-
const both = (predicate) => (real, expected) => {
126-
return [real, expected].every(predicate);
124+
function getBodyValidator(expectedType, actualType) {
125+
const both = (predicate) => (expected, actual) => {
126+
return [expected, actual].every(predicate);
127127
};
128128

129129
const validators = [
@@ -132,21 +132,21 @@ function getBodyValidator(realType, expectedType) {
132132
// would resolve on "application/schema+json" media type too.
133133
[
134134
JsonSchema,
135-
(real, expected) => {
136-
return isJson(real) && isJsonSchema(expected);
135+
(expected, actual) => {
136+
return isJson(actual) && isJsonSchema(expected);
137137
},
138138
'json'
139139
],
140140
[JsonExample, both(isJson), 'json']
141141
];
142142

143143
const validator = validators.find(([_name, predicate]) => {
144-
return predicate(realType, expectedType);
144+
return predicate(expectedType, actualType);
145145
});
146146

147147
if (!validator) {
148148
const error = `Can't validate actual media type '${mediaTyper.format(
149-
realType
149+
actualType
150150
)}' against the expected media type '${mediaTyper.format(expectedType)}'.`;
151151
return [error, null, null];
152152
}
@@ -157,7 +157,7 @@ function getBodyValidator(realType, expectedType) {
157157
/**
158158
* Validates given bodies of transaction elements.
159159
* @param {Object<string, any>} expected
160-
* @param {Object<string, any>} real
160+
* @param {Object<string, any>} actual
161161
*/
162162
function validateBody(expected, actual) {
163163
const values = {
@@ -171,20 +171,20 @@ function validateBody(expected, actual) {
171171
}
172172

173173
const errors = [];
174-
const realBodyType = typeof actual.body;
175-
const hasEmptyRealBody = actual.body === '';
174+
const actualBodyType = typeof actual.body;
175+
const hasEmptyActualBody = actual.body === '';
176176

177-
// Throw when user input for real body is not a string.
178-
if (realBodyType !== 'string') {
177+
// Throw when user input for actual body is not a string.
178+
if (actualBodyType !== 'string') {
179179
throw new Error(
180-
`Expected HTTP body to be a string, but got: ${realBodyType}`
180+
`Expected HTTP body to be a string, but got: ${actualBodyType}`
181181
);
182182
}
183183

184-
const [realTypeError, realType] = getBodyType(
184+
const [actualTypeError, actualType] = getBodyType(
185185
actual.body,
186186
actual.headers && actual.headers['content-type'],
187-
'real'
187+
'actual'
188188
);
189189

190190
const [expectedTypeError, expectedType] = expected.bodySchema
@@ -195,9 +195,9 @@ function validateBody(expected, actual) {
195195
'expected'
196196
);
197197

198-
if (realTypeError) {
198+
if (actualTypeError) {
199199
errors.push({
200-
message: realTypeError
200+
message: actualTypeError
201201
});
202202
}
203203

@@ -210,18 +210,18 @@ function validateBody(expected, actual) {
210210
const hasErrors = errors.length > 0;
211211

212212
// Skipping body validation in case errors during
213-
// real/expected body type definition.
213+
// actual/expected body type definition.
214214
const [validatorError, ValidatorClass, kind] = hasErrors
215215
? [null, null, null]
216-
: getBodyValidator(realType, expectedType);
216+
: getBodyValidator(expectedType, actualType);
217217

218218
if (validatorError) {
219-
// In case determined media types mismtach, check if the real is not missing.
219+
// In case determined media types mismtach, check if the actual is not missing.
220220
// Keep in mind the following scenarios:
221221
// 1. Expected '', and got '' (TextDiff/TextDiff, valid)
222-
// 2. Expected {...}, but got '' (Json/TextDiff, invalid, produces "missing real body" error)
222+
// 2. Expected {...}, but got '' (Json/TextDiff, invalid, produces "missing actual body" error)
223223
// 3. Expected {...}, but got "foo" (Json/TextDiff, invalid, produces types mismatch error).
224-
if (expected.body !== '' && hasEmptyRealBody) {
224+
if (expected.body !== '' && hasEmptyActualBody) {
225225
errors.push({
226226
message: `Expected "body" of "${mediaTyper.format(
227227
expectedType

lib/validators/json-example.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ function getSchema(json) {
1818
class JsonExample extends JsonSchema {
1919
/**
2020
* Construct a BodyValidator, check data and choose the right validator.
21-
* If real and expected data are valid JSON, and a valid schema is given,
21+
* If actual and expected data are valid JSON, and a valid schema is given,
2222
* choose JsonValidator, otherwise choose StringValidator.
23-
* @param {string} real
2423
* @param {string} expected
25-
* @throw {MalformedDataError} when real is not a String or when no schema provided and expected is not a String
24+
* @param {string} actual
25+
* @throw {MalformedDataError} when actual is not a String or when no schema provided and expected is not a String
2626
* @throw {SchemaNotJsonParsableError} when given schema is not a json parsable string or valid json
2727
* @throw {NotEnoughDataError} when at least one of expected data and json schema is not given
2828
*/

lib/validators/text-diff.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,7 @@ class TextDiff {
3434

3535
return [
3636
{
37-
message: 'Actual and expected data do not match.',
38-
values: {
39-
expected: this.expected,
40-
actual: this.actual
41-
}
37+
message: 'Actual and expected data do not match.'
4238
}
4339
];
4440
}

test/unit/units/validateBody.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ describe('validateBody', () => {
108108
expect(result)
109109
.to.have.errorAtIndex(0)
110110
.withMessage(
111-
/^Can't validate: real body 'Content-Type' header is 'application\/json' but body is not a parseable JSON:/
111+
/^Can't validate: actual body 'Content-Type' header is 'application\/json' but body is not a parseable JSON:/
112112
);
113113
});
114114
});
@@ -172,7 +172,7 @@ describe('validateBody', () => {
172172
expect(result)
173173
.to.have.errorAtIndex(0)
174174
.withMessage(
175-
/^Can't validate: real body 'Content-Type' header is 'application\/hal\+json' but body is not a parseable JSON:/
175+
/^Can't validate: actual body 'Content-Type' header is 'application\/hal\+json' but body is not a parseable JSON:/
176176
);
177177
});
178178
});

test/unit/units/validateBody/getBodyValidator.test.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ const { assert } = require('chai');
22
const mediaTyper = require('media-typer');
33
const { getBodyValidator } = require('../../../../lib/units/validateBody');
44

5-
const getMediaTypes = (real, expected) => {
6-
return [real, expected].map(mediaTyper.parse);
5+
const getMediaTypes = (expected, actual) => {
6+
return [expected, actual].map(mediaTyper.parse);
77
};
88

99
describe('getBodyValidator', () => {
@@ -14,7 +14,7 @@ describe('getBodyValidator', () => {
1414
expectedValidator: 'JsonExample'
1515
},
1616
{
17-
contentTypes: ['application/json', 'application/schema+json'],
17+
contentTypes: ['application/schema+json', 'application/json'],
1818
expectedValidator: 'JsonSchema'
1919
},
2020
{
@@ -24,14 +24,14 @@ describe('getBodyValidator', () => {
2424
];
2525

2626
knownContentTypes.forEach(({ contentTypes, expectedValidator }) => {
27-
const [realContentType, expectedContentType] = contentTypes;
28-
const [real, expected] = getMediaTypes(
29-
realContentType,
30-
expectedContentType
27+
const [expectedContentType, actualContentType] = contentTypes;
28+
const [expected, actual] = getMediaTypes(
29+
expectedContentType,
30+
actualContentType
3131
);
3232

33-
describe(`${realContentType} + ${expectedContentType}`, () => {
34-
const [error, validator] = getBodyValidator(real, expected);
33+
describe(`${expectedContentType} + ${actualContentType}`, () => {
34+
const [error, validator] = getBodyValidator(expected, actual);
3535

3636
it('returns no error', () => {
3737
assert.isNull(error);

test/unit/validators/text-diff.test.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,6 @@ describe('TextDiff', () => {
8080
'Actual and expected data do not match.'
8181
);
8282
});
83-
84-
it('error should contain compared values', () => {
85-
expect(result[0]).to.have.deep.property('values', {
86-
expected: 'john',
87-
actual: 'barry'
88-
});
89-
});
9083
});
9184
});
9285
});

0 commit comments

Comments
 (0)