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

Commit

Permalink
fix: adjusts namespaces and call signatures of "isValid" utils
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-zakharchenko committed Jun 17, 2019
1 parent 96a63ea commit 8dd1349
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
27 changes: 18 additions & 9 deletions lib/units/isValid.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
function isValidComponent(errors) {
/**
* Concludes HTTP message's component validity based on the given
* list of validation errors.
* @param {Object<string, any>} errors
* @returns {boolean}
*/
function isValidField({ errors }) {
return errors.length === 0;
}

// Returns a boolean indicating whether a given validation result
// concludes two HTTP messages as valid (matching).
// Separated into its own util only to be used in both next and legacy API.
function isValid(validationResult) {
return Object.values(validationResult.fields).every((resultGroup) => {
return isValidComponent(resultGroup.errors);
});
/**
* Returns a boolean indicating the given validation result as valid.
* @param {Object<string, any>} validationResult
* @returns {boolean}
*/
function isValidResult(validationResult) {
return Object.values(validationResult.fields).every(isValidField);
}

module.exports = { isValid, isValidComponent };
module.exports = {
isValidResult,
isValidField
};
4 changes: 2 additions & 2 deletions lib/units/validateBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const mediaTyper = require('media-typer');
const contentTypeUtils = require('content-type');

const { TextDiff, JsonExample, JsonSchema } = require('../validators');
const { isValidComponent } = require('./isValid');
const { isValidField } = require('./isValid');

function isPlainText(mediaType) {
return mediaType.type === 'text' && mediaType.subtype === 'plain';
Expand Down Expand Up @@ -214,7 +214,7 @@ function validateBody(expected, real) {
errors.push(...validationErrors);

return {
isValid: isValidComponent(errors),
isValid: isValidField({ errors }),
validator: ValidatorClass && ValidatorClass.name,
realType: mediaTyper.format(realType),
expectedType: mediaTyper.format(expectedType),
Expand Down
4 changes: 2 additions & 2 deletions lib/units/validateHeaders.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { HeadersJsonExample } = require('../validators');
const { isValidComponent } = require('./isValid');
const { isValidField } = require('./isValid');

const APIARY_JSON_HEADER_TYPE = 'application/vnd.apiary.http-headers+json';

Expand Down Expand Up @@ -42,7 +42,7 @@ and expected data media type
}

return {
isValid: isValidComponent(errors),
isValid: isValidField({ errors }),
validator: validator && 'HeadersJsonExample',
realType,
expectedType,
Expand Down
4 changes: 2 additions & 2 deletions lib/validate.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const isset = require('./utils/isset');
const { coerce, coerceWeak } = require('./units/coerce');
const { normalize } = require('./units/normalize');
const { isValid } = require('./units/isValid');
const { isValidResult } = require('./units/isValid');
const { validateMethod } = require('./units/validateMethod');
const { validateURI } = require('./units/validateURI');
const { validateStatusCode } = require('./units/validateStatusCode');
Expand Down Expand Up @@ -47,7 +47,7 @@ function validate(expectedMessage, realMessage) {
}

// Indicates the validity of the real message
result.isValid = isValid(result);
result.isValid = isValidResult(result);

return result;
}
Expand Down

0 comments on commit 8dd1349

Please sign in to comment.