Skip to content

Commit

Permalink
Merge e6f72db into a39dbce
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-zakharchenko committed May 24, 2019
2 parents a39dbce + e6f72db commit f1872c2
Show file tree
Hide file tree
Showing 7 changed files with 285 additions and 267 deletions.
10 changes: 6 additions & 4 deletions lib/mixins/validatable-http-message.js
Expand Up @@ -3,14 +3,13 @@ const mediaTyper = require('media-typer');
const contentType = require('content-type');
const isset = require('../utils/isset');
const validators = require('../validators');
const { isValid } = require('../next/units/isValid');

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

class Validatable {
validate() {
this.validation = {
version: '2'
};
this.validation = {};
this.lowercaseHeaders();

if (this.headers && this.expected && this.expected.headers) {
Expand All @@ -28,7 +27,10 @@ class Validatable {
this.validateStatusCode();
}

return this.validation;
return Object.assign({}, this.validation, {
version: '2',
isValid: isValid(this.validation)
});
}

isValidatable() {
Expand Down
24 changes: 20 additions & 4 deletions lib/next/test/integration/validateMessage.test.js
Expand Up @@ -17,7 +17,11 @@ describe('validateMessage', () => {
});

it('contains all validatable keys', () => {
assert.hasAllKeys(result, ['headers', 'body']);
assert.hasAllKeys(result, ['isValid', 'headers', 'body']);
});

it('has "isValid" set to true', () => {
assert.propertyVal(result, 'isValid', true);
});

describe('headers', () => {
Expand Down Expand Up @@ -86,7 +90,11 @@ describe('validateMessage', () => {
});

it('contains all validatable keys', () => {
assert.hasAllKeys(result, ['headers', 'body']);
assert.hasAllKeys(result, ['isValid', 'headers', 'body']);
});

it('has "isValid" set to false', () => {
assert.propertyVal(result, 'isValid', false);
});

describe('method', () => {
Expand Down Expand Up @@ -168,7 +176,11 @@ describe('validateMessage', () => {
});

it('contains all validatable keys', () => {
assert.hasAllKeys(result, ['statusCode', 'headers', 'body']);
assert.hasAllKeys(result, ['isValid', 'statusCode', 'headers', 'body']);
});

it('sets "isValid" to true', () => {
assert.propertyVal(result, 'isValid', true);
});

describe('statusCode', () => {
Expand Down Expand Up @@ -262,7 +274,11 @@ describe('validateMessage', () => {
});

it('contains all validatable keys', () => {
assert.hasAllKeys(result, ['statusCode', 'headers']);
assert.hasAllKeys(result, ['isValid', 'statusCode', 'headers']);
});

it('has "isValid" as false', () => {
assert.propertyVal(result, 'isValid', false);
});

describe('statusCode', () => {
Expand Down
11 changes: 11 additions & 0 deletions lib/next/units/isValid.js
@@ -0,0 +1,11 @@
// 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.
// TODO Move to "validateMessage" after legacy "gavel.validate()" removal.
function isValid(validationResult) {
return Object.values(validationResult).every((resultGroup) => {
return resultGroup.results.every((result) => result.severity !== 'error');
});
}

module.exports = { isValid };
3 changes: 3 additions & 0 deletions lib/next/validateMessage.js
@@ -1,5 +1,6 @@
const isset = require('../utils/isset');
const { normalize } = require('./units/normalize');
const { isValid } = require('./units/isValid');
const { validateStatusCode } = require('./units/validateStatusCode');
const { validateHeaders } = require('./units/validateHeaders');
const { validateBody } = require('./units/validateBody');
Expand All @@ -24,6 +25,8 @@ function validateMessage(realMessage, expectedMessage) {
results.body = validateBody(real, expected);
}

results.isValid = isValid(results);

return results;
}

Expand Down
15 changes: 10 additions & 5 deletions lib/validate.js
Expand Up @@ -39,19 +39,24 @@ function getValidatableObject(real, expected, type) {
}

function isValid(real, expected, type, cb) {
console.warn(`\
Usage of "isValid" is deprecated and will be removed in the future releases of Gavel.
Use "isValid" property of the validation result instead:
const { validate } = require('gavel');
const result = validate(real, expected, 'request');
const { isValid } = result;\
`);

return proxy(getValidatableObject(real, expected, type), 'isValid', cb);
}

function isValidatable(real, expected, type, cb) {
return proxy(getValidatableObject(real, expected, type), 'isValidatable', cb);
}

function validate(real, expected, type, cb) {
return proxy(getValidatableObject(real, expected, type), 'validate', cb);
}

module.exports = {
validate,
isValid,
isValidatable
};

0 comments on commit f1872c2

Please sign in to comment.