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

Commit de99480

Browse files
refactor: adjusts methods and tests to be compliant with the next spec
1 parent 290dec8 commit de99480

File tree

14 files changed

+150
-234
lines changed

14 files changed

+150
-234
lines changed

lib/next/test/integration/validateMessage.test.js

Lines changed: 79 additions & 139 deletions
Large diffs are not rendered by default.

lib/next/test/unit/units/validateBody.test.js

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@ describe('validateBody', () => {
4848
assert.lengthOf(result.errors, 1);
4949
});
5050

51-
it('has "error" severity', () => {
52-
assert.propertyVal(result.errors[0], 'severity', 'error');
53-
});
54-
5551
it('has explanatory message', () => {
5652
assert.propertyVal(
5753
result.errors[0],
@@ -114,10 +110,6 @@ describe('validateBody', () => {
114110
});
115111

116112
describe('produces content-type error', () => {
117-
it('has "error" severity', () => {
118-
assert.propertyVal(res.errors[0], 'severity', 'error');
119-
});
120-
121113
it('has explanatory message', () => {
122114
assert.match(
123115
res.errors[0].message,
@@ -185,10 +177,6 @@ describe('validateBody', () => {
185177
});
186178

187179
describe('produces error', () => {
188-
it('has "error" severity', () => {
189-
assert.propertyVal(res.errors[0], 'severity', 'error');
190-
});
191-
192180
it('has explanatory message', () => {
193181
assert.match(
194182
res.errors[0].message,
@@ -242,10 +230,6 @@ describe('validateBody', () => {
242230
assert.lengthOf(res.errors, 1);
243231
});
244232

245-
it('with "error" severity', () => {
246-
assert.propertyVal(res.errors[0], 'severity', 'error');
247-
});
248-
249233
it('with explanatory message', () => {
250234
assert.hasAnyKeys(res.errors[0], 'message');
251235
assert.propertyVal(
@@ -305,10 +289,6 @@ describe('validateBody', () => {
305289
assert.lengthOf(res.errors, 1);
306290
});
307291

308-
it('has "error" severity', () => {
309-
assert.propertyVal(res.errors[0], 'severity', 'error');
310-
});
311-
312292
it('has explanatory message', () => {
313293
assert.propertyVal(
314294
res.errors[0],
@@ -375,10 +355,6 @@ describe('validateBody', () => {
375355
assert.lengthOf(res.errors, 1);
376356
});
377357

378-
it('has "error" severity', () => {
379-
assert.propertyVal(res.errors[0], 'severity', 'error');
380-
});
381-
382358
it('has explanatory message', () => {
383359
assert.propertyVal(
384360
res.errors[0],

lib/next/test/unit/units/validateHeaders.test.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,6 @@ describe('validateHeaders', () => {
8989
describe('for each missing header', () => {
9090
missingHeaders.forEach((headerName, index) => {
9191
describe(headerName, () => {
92-
it('has "error" severity', () => {
93-
assert.propertyVal(res.errors[index], 'severity', 'error');
94-
});
95-
9692
it('has pointer to header name', () => {
9793
assert.propertyVal(
9894
res.errors[index],
@@ -141,10 +137,6 @@ describe('validateHeaders', () => {
141137
assert.lengthOf(res.errors, 1);
142138
});
143139

144-
it('has "error" severity', () => {
145-
assert.propertyVal(res.errors[0], 'severity', 'error');
146-
});
147-
148140
it('has explanatory message', () => {
149141
assert.propertyVal(
150142
res.errors[0],

lib/next/test/unit/units/validateMethod.test.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,6 @@ describe('validateMethod', () => {
5050
assert.lengthOf(result.errors, 1);
5151
});
5252

53-
it('has "error" severity', () => {
54-
assert.propertyVal(result.errors[0], 'severity', 'error');
55-
});
56-
5753
it('has explanatory message', () => {
5854
assert.propertyVal(
5955
result.errors[0],
@@ -88,10 +84,6 @@ describe('validateMethod', () => {
8884
assert.lengthOf(result.errors, 1);
8985
});
9086

91-
it('has "error" severity', () => {
92-
assert.propertyVal(result.errors[0], 'severity', 'error');
93-
});
94-
9587
it('has explanatory message', () => {
9688
assert.propertyVal(
9789
result.errors[0],

lib/next/test/unit/units/validateStatusCode.test.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,6 @@ describe('validateStatusCode', () => {
5656
assert.lengthOf(result.errors, 1);
5757
});
5858

59-
it('has "error" severity', () => {
60-
assert.propertyVal(result.errors[0], 'severity', 'error');
61-
});
62-
6359
it('has explanatory message', () => {
6460
assert.propertyVal(
6561
result.errors[0],

lib/next/validate.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,26 @@ const { validateMessage } = require('./validateMessage');
88
* @param {(error: Error, result: Object<string, key>) => void} callback
99
*/
1010
function validate(real, expected, callback) {
11-
let results;
11+
let result;
1212

1313
try {
14-
results = validateMessage(real, expected);
14+
result = validateMessage(real, expected);
1515
} catch (error) {
1616
if (callback) {
1717
callback(error, null);
1818
}
1919

20-
return results;
20+
return result;
2121
}
2222

2323
if (callback) {
2424
callback(null, {
2525
version: '2',
26-
...results
26+
...result
2727
});
2828
}
2929

30-
return results;
30+
return result;
3131
}
3232

3333
module.exports = validate;

lib/next/validateMessage.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ const { validateHeaders } = require('./units/validateHeaders');
88
const { validateBody } = require('./units/validateBody');
99

1010
function validateMessage(realMessage, expectedMessage) {
11-
const results = {
12-
field: {}
11+
const result = {
12+
fields: {}
1313
};
1414

1515
// Uses strict coercion on real message.
@@ -24,25 +24,25 @@ function validateMessage(realMessage, expectedMessage) {
2424
const expected = normalize(coerceWeak(expectedMessage));
2525

2626
if (expected.method) {
27-
results.field.method = validateMethod(real, expected);
27+
result.fields.method = validateMethod(real, expected);
2828
}
2929

3030
if (expected.statusCode) {
31-
results.field.statusCode = validateStatusCode(real, expected);
31+
result.fields.statusCode = validateStatusCode(real, expected);
3232
}
3333

3434
if (expected.headers) {
35-
results.field.headers = validateHeaders(real, expected);
35+
result.fields.headers = validateHeaders(real, expected);
3636
}
3737

3838
if (isset(expected.body) || isset(expected.bodySchema)) {
39-
results.field.body = validateBody(real, expected);
39+
result.fields.body = validateBody(real, expected);
4040
}
4141

4242
// Indicates the validity of the real message
43-
results.isValid = isValid(results);
43+
result.isValid = isValid(result);
4444

45-
return results;
45+
return result;
4646
}
4747

4848
module.exports = { validateMessage };

lib/validators/json-schema.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,7 @@ class JsonSchema {
246246

247247
return {
248248
pointer: jsonPointer.compile(pathArray),
249-
message: item.message,
250-
severity: 'error'
249+
message: item.message
251250
};
252251
});
253252

lib/validators/text-diff.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ class TextDiff {
6262

6363
return [
6464
{
65-
message: 'Real and expected data does not match.',
66-
severity: 'error'
65+
// TODO Improve the message to contain real and expected data
66+
message: 'Real and expected data does not match.'
6767
}
6868
];
6969
}

test/cucumber/step_definitions/validation_errors_thens.js

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = function() {
1212
}
1313

1414
component = this.toCamelCase(component);
15-
const componentValidation = result.field[component];
15+
const componentValidation = result.fields[component];
1616
const { errors } = componentValidation;
1717
const errorsCount = errors.length;
1818

@@ -37,7 +37,7 @@ module.exports = function() {
3737
callback(new Error(`Error during validation: ${error}`));
3838
}
3939
component = this.toCamelCase(component);
40-
const componentValidation = result.field[component];
40+
const componentValidation = result.fields[component];
4141
const { errors } = componentValidation;
4242
const errorsCount = errors.length;
4343

@@ -58,34 +58,58 @@ module.exports = function() {
5858
});
5959
});
6060

61-
this.Then(/^Gavel will set "([^"]*)" to "([^"]*)" for "([^"]*)"$/, function(
62-
propName,
63-
expectedValue,
64-
componentName,
61+
this.Then(/^field "([^"]*)" is( NOT)? valid$/, function(
62+
fieldName,
63+
isNotValid,
6564
callback
6665
) {
6766
return this.validate((error, result) => {
68-
console.log({ componentName, propName, expectedValue });
69-
console.log({ result });
70-
7167
assert.property(
72-
result.field,
73-
componentName,
74-
`Expected validation result to have property "${componentName}", but got "${Object.keys(
75-
result
76-
).join('", "')}"`
68+
result.fields,
69+
fieldName,
70+
`Expected to have "${fieldName}" field in the validation result, but got none.`
7771
);
7872

7973
assert.propertyVal(
80-
result.field[componentName],
81-
propName,
82-
this.toBoolean(expectedValue)
74+
result.fields[fieldName],
75+
'isValid',
76+
!isNotValid,
77+
`Expected "result.fields.${fieldName}" to be valid, but it's not.`
8378
);
8479

8580
return callback();
8681
});
8782
});
8883

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+
89113
this.Then(/^Request or Response is NOT valid$/, function(callback) {
90114
return this.validate((error, result) => {
91115
if (result.isValid) {

0 commit comments

Comments
 (0)