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

Commit 681b032

Browse files
feat: removes "callback" argument of "validate"
BREAKING CHANGE: "gavel.validate()" no longer accepts a callback as an argument. Please use the "validate()" function as follows: const result = gavel.validate(expected, real)
1 parent f631116 commit 681b032

File tree

5 files changed

+63
-156
lines changed

5 files changed

+63
-156
lines changed

lib/next/validate.js

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,11 @@ const { validateMessage } = require('./validateMessage');
33
/**
44
* Validates the given HTTP messages pair and returns
55
* a legacy-compliant validation results.
6-
* @param {Object<string, key>} real
76
* @param {Object<string, key>} expected
8-
* @param {(error: Error, result: Object<string, key>) => void} callback
7+
* @param {Object<string, key>} real
98
*/
10-
function validate(expected, real, callback) {
11-
let result;
12-
13-
try {
14-
result = validateMessage(expected, real);
15-
} catch (error) {
16-
if (callback) {
17-
callback(error, null);
18-
}
19-
20-
return result;
21-
}
22-
23-
if (callback) {
24-
callback(null, {
25-
version: '2',
26-
...result
27-
});
28-
}
29-
30-
return result;
9+
function validate(expected, real) {
10+
return validateMessage(expected, real);
3111
}
3212

3313
module.exports = validate;

test/cucumber/step_definitions/validation_errors_thens.js

Lines changed: 43 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@ const { assert } = require('chai');
33

44
module.exports = function() {
55
this.Then(/^Gavel will set some error for "([^"]*)"$/, function(
6-
component,
6+
rawComponentName,
77
callback
88
) {
9-
return this.validate((error, result) => {
10-
if (error) {
11-
callback(new Error(`Error during validation: ${error}`));
12-
}
13-
14-
component = this.toCamelCase(component);
9+
try {
10+
const result = this.validate();
11+
const component = this.toCamelCase(rawComponentName);
1512
const componentValidation = result.fields[component];
1613
const { errors } = componentValidation;
1714
const errorsCount = errors.length;
@@ -25,18 +22,18 @@ module.exports = function() {
2522
}
2623

2724
return callback();
28-
});
25+
} catch (error) {
26+
callback(new Error(`Error during validation: ${error}`));
27+
}
2928
});
3029

3130
this.Then(/^Gavel will NOT set any errors for "([^"]*)"$/, function(
32-
component,
31+
rawComponentName,
3332
callback
3433
) {
35-
return this.validate((error, result) => {
36-
if (error) {
37-
callback(new Error(`Error during validation: ${error}`));
38-
}
39-
component = this.toCamelCase(component);
34+
try {
35+
const result = this.validate();
36+
const component = this.toCamelCase(rawComponentName);
4037
const componentValidation = result.fields[component];
4138
const { errors } = componentValidation;
4239
const errorsCount = errors.length;
@@ -55,80 +52,50 @@ module.exports = function() {
5552
}
5653

5754
return callback();
58-
});
55+
} catch (error) {
56+
callback(new Error(`Error during validation: ${error}`));
57+
}
5958
});
6059

6160
this.Then(/^field "([^"]*)" is( NOT)? valid$/, function(
6261
fieldName,
6362
isNotValid,
6463
callback
6564
) {
66-
return this.validate((error, result) => {
67-
assert.property(
68-
result.fields,
69-
fieldName,
70-
`Expected to have "${fieldName}" field in the validation result, but got none.`
71-
);
72-
73-
assert.propertyVal(
74-
result.fields[fieldName],
75-
'isValid',
76-
!isNotValid,
77-
`Expected "result.fields.${fieldName}" to be valid, but it's not.`
78-
);
79-
80-
return callback();
81-
});
65+
const result = this.validate();
66+
assert.property(
67+
result.fields,
68+
fieldName,
69+
`Expected to have "${fieldName}" field in the validation result, but got none.`
70+
);
71+
72+
assert.propertyVal(
73+
result.fields[fieldName],
74+
'isValid',
75+
!isNotValid,
76+
`Expected "result.fields.${fieldName}" to be valid, but it's not.`
77+
);
78+
79+
return callback();
8280
});
8381

84-
// TODO REMOVE THIS, no longer in spec
85-
// this.Then(/^Gavel will set "([^"]*)" to "([^"]*)" for "([^"]*)"$/, function(
86-
// propName,
87-
// expectedValue,
88-
// componentName,
89-
// callback
90-
// ) {
91-
// return this.validate((error, result) => {
92-
// console.log({ componentName, propName, expectedValue });
93-
// console.log({ result });
94-
95-
// assert.property(
96-
// result.field,
97-
// componentName,
98-
// `Expected validation result to have property "${componentName}", but got "${Object.keys(
99-
// result
100-
// ).join('", "')}"`
101-
// );
102-
103-
// assert.propertyVal(
104-
// result.field[componentName],
105-
// propName,
106-
// this.toBoolean(expectedValue)
107-
// );
108-
109-
// return callback();
110-
// });
111-
// });
112-
11382
this.Then(/^Request or Response is NOT valid$/, function(callback) {
114-
return this.validate((error, result) => {
115-
if (result.isValid) {
116-
callback(
117-
new Error('Request or Response is valid and should NOT be valid.')
118-
);
119-
}
120-
return callback();
121-
});
83+
const result = this.validate();
84+
if (result.isValid) {
85+
callback(
86+
new Error('Request or Response is valid and should NOT be valid.')
87+
);
88+
}
89+
return callback();
12290
});
12391

12492
return this.Then(/^Request or Response is valid$/, function(callback) {
125-
return this.validate((error, result) => {
126-
if (!result.isValid) {
127-
callback(
128-
new Error('Request or Response is NOT valid and should be valid.')
129-
);
130-
}
131-
return callback();
132-
});
93+
const result = this.validate();
94+
if (!result.isValid) {
95+
callback(
96+
new Error('Request or Response is NOT valid and should be valid.')
97+
);
98+
}
99+
return callback();
133100
});
134101
};

test/cucumber/step_definitions/validators_steps.js

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,14 @@ module.exports = function() {
2626
body: json2
2727
};
2828

29-
return this.validate((error, result) => {
30-
if (error) {
31-
callback(new Error(`Got error during validation:\n${error}`));
32-
}
29+
try {
30+
const result = this.validate();
3331
this.results = JSON.parse(JSON.stringify(result));
34-
35-
return this.validate((error, result) => {
36-
if (error) {
37-
callback(new Error(error));
38-
}
39-
this.booleanResult = result.isValid;
40-
return callback();
41-
});
42-
});
32+
this.booleanResult = result.isValid;
33+
return callback();
34+
} catch (error) {
35+
callback(new Error(`Got error during validation:\n${error}`));
36+
}
4337
}
4438
);
4539

@@ -166,15 +160,14 @@ module.exports = function() {
166160
this.When(/^you perform validation on the HTTP component$/, function(
167161
callback
168162
) {
169-
return this.validate((error, result) => {
170-
if (error) {
171-
callback(new Error(`Error during validation: ${error}`));
172-
}
173-
163+
try {
164+
const result = this.validate();
174165
this.results = result;
175166
this.componentResults = this.results.fields[this.component];
176167
return callback();
177-
});
168+
} catch (error) {
169+
callback(new Error(`Error during validation: ${error}`));
170+
}
178171
});
179172

180173
this.Then(/^validator "([^"]*)" is used for validation$/, function(

test/cucumber/support/world.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ class World {
111111
}
112112
}
113113

114-
validate(callback) {
115-
return gavel.validate(this.expected, this.real, callback);
114+
validate() {
115+
return gavel.validate(this.expected, this.real);
116116
}
117117

118118
parseHeaders(headersString) {

test/unit/validate-test.js

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -46,53 +46,20 @@ describe('Gavel proxies to functions with callbacks', () => {
4646
describe('when I provide data', () => {
4747
['response', 'request'].forEach((variant) => {
4848
describe(`for two cloned ${variant}s`, () => {
49-
let results = null;
50-
let error = null;
49+
const results = validate(baseHttpMessage, cloneHttpMessage);
5150

52-
before((done) => {
53-
validate(baseHttpMessage, cloneHttpMessage, (err, result) => {
54-
error = err;
55-
results = result;
56-
done();
57-
});
58-
});
59-
60-
it('should call the callback without any errors', () =>
61-
assert.isNull(error));
6251
it('should results be an object', () => assert.isObject(results));
6352
});
6453

6554
describe(`for similar ${variant}s`, () => {
66-
let results = null;
67-
let error = null;
68-
69-
before((done) => {
70-
validate(baseHttpMessage, similarHttpMessage, (err, result) => {
71-
error = err;
72-
results = result;
73-
done();
74-
});
75-
});
55+
const results = validate(baseHttpMessage, similarHttpMessage);
7656

77-
it('should call the callback without any errors', () =>
78-
assert.isNull(error));
7957
it('should results be an object', () => assert.isObject(results));
8058
});
8159

8260
describe(`for different ${variant}s`, () => {
83-
let results = null;
84-
let error = null;
85-
86-
before((done) => {
87-
validate(baseHttpMessage, differentHttpMessage, (err, result) => {
88-
error = err;
89-
results = result;
90-
done();
91-
});
92-
});
61+
const results = validate(baseHttpMessage, differentHttpMessage);
9362

94-
it('should call the callback without any errors', () =>
95-
assert.isNull(error));
9663
it('should results be an object', () => assert.isObject(results));
9764
it('should results contain 2 different headers messages (missing content-length, different content-type)', () => {
9865
assert.isObject(results);

0 commit comments

Comments
 (0)