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

Commit 7870a99

Browse files
fix: improves explanatory comments
1 parent 459c4e8 commit 7870a99

File tree

8 files changed

+33
-17
lines changed

8 files changed

+33
-17
lines changed

lib/units/coerce/index.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ const coercionMap = {
77
headers: otherwise({})
88
};
99

10-
// Coercion uses strict evolve to ensure the properties
11-
// set in expected schema are set on the result object,
12-
// even if not present in data object. This is what
13-
// coercion is about, in the end.
10+
// Coercion is strict by default, meaning it would populate
11+
// all the keys illegible for coercion on the given object,
12+
// even if those keys are missing.
1413
const coerce = evolve(coercionMap, { strict: true });
14+
15+
// There is also a weak variant of coercion that operates
16+
// only on the present keys in the given object.
17+
// This is used for coercing expected HTTP message, for example.
1518
const coerceWeak = evolve(coercionMap);
1619

1720
module.exports = {

lib/units/normalize/normalizeHeaders.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ const normalizeStringValue = (value) => {
33
};
44

55
/**
6-
* Normalizes the given headers.
7-
* @param {Object} headers
8-
* @returns {Object}
6+
* Normalizes given headers.
7+
* @param {Object<string, string>} headers
8+
* @returns {Object<string, string>}
99
*/
1010
const normalizeHeaders = (headers) => {
1111
const headersType = typeof headers;

lib/units/normalize/normalizeMethod.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
2-
* Normalizes given HTTP message method.
2+
* Normalizes given method.
33
* @param {string} method
4+
* @returns {string}
45
*/
56
const normalizeMethod = (method) => {
67
return method.trim().toUpperCase();

lib/units/normalize/normalizeStatusCode.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* Normalizes given status code.
3+
* @param {string} value
4+
* @returns {string}
5+
*/
16
function normalizeStatusCode(value) {
27
return value == null ? '' : String(value).trim();
38
}

lib/units/validateBody.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ function isJsonContentType(contentType) {
6060
* Returns a tuple of error and body media type based
6161
* on the given body and normalized headers.
6262
* @param {string} body
63-
* @param {Object} headers
64-
* @param {'real'|'expected'} bodyType
65-
* @returns {[error, bodyType]}
63+
* @param {string} contentType
64+
* @param {'real'|'expected'} httpMessageOrigin
65+
* @returns {[string, MediaType]}
6666
*/
6767
function getBodyType(body, contentType, httpMessageOrigin) {
6868
const hasJsonContentType = isJsonContentType(contentType);
@@ -90,7 +90,7 @@ ${parsingError.message}`
9090
* Returns a tuple of error and schema media type
9191
* based on given body schema.
9292
* @param {string} bodySchema
93-
* @returns {[error, schemaType]}
93+
* @returns {[string, string]}
9494
*/
9595
function getBodySchemaType(bodySchema) {
9696
const jsonSchemaType = mediaTyper.parse('application/schema+json');

lib/units/validateURI.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const APIARY_URI_TYPE = 'text/vnd.apiary.uri';
55
/**
66
* Parses a given query string into an Object.
77
* @param {string} queryString
8+
* @returns {Object<string, string | string[]>}
89
*/
910
const parseQueryString = (queryString) => {
1011
if (!queryString) {

lib/utils/otherwise.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* Accepts a fallback value and returns an ensuring function
3+
* that uses the fallback value in case real one is not provided.
4+
*/
15
const otherwise = (fallbackValue) => (realValue) => {
26
return realValue || fallbackValue;
37
};

lib/validate.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@ function validate(expectedMessage, realMessage) {
1414
};
1515

1616
// Uses strict coercion on real message.
17-
// Strict coercion ensures real message has properties illegible
18-
// for validation with the expected message.
17+
// Strict coercion ensures that real message always has properties
18+
// illegible for validation with the expected message, even if they
19+
// are not present in the real message.
1920
const real = normalize(coerce(realMessage));
2021

2122
// Use weak coercion on expected message.
22-
// This means that only the properties present in expected message
23-
// will be coerced. We don't want to mutate user's assertion.
24-
// However, we want to use the same coercion logic for any coercion type.
23+
// Weak coercion will transform only the properties present in the
24+
// expected message. Properties meant for coercion, but not provided
25+
// in the expected message are left out, as we don't want to mutate
26+
// user's assertion.
2527
const expected = normalize(coerceWeak(expectedMessage));
2628

2729
if (expected.method) {

0 commit comments

Comments
 (0)