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

Commit

Permalink
refactor: adjusts "validateHeaders" to new JsonSchema call signature
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-zakharchenko committed Nov 29, 2019
1 parent cb8c01a commit 6ae63b6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 25 deletions.
6 changes: 3 additions & 3 deletions lib/units/validateHeaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ function validateHeaders(expected, actual) {
expectedType === APIARY_JSON_HEADER_TYPE;

const validator = hasJsonHeaders
? new HeadersJsonExample(values.expected, values.actual)
? new HeadersJsonExample(values.expected)
: null;

// if you don't call ".validate()", it never evaluates any results.
validator && validator.validate();
const validationErrors = validator && validator.validate(values.actual);

if (validator) {
errors.push(...validator.evaluateOutputToResults());
errors.push(...validationErrors);
} else {
errors.push({
message: `\
Expand Down
42 changes: 20 additions & 22 deletions lib/validators/headers-json-example.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const {
} = require('../utils/schema-v4-generator');
const tv4ToHeadersMessage = require('../utils/tv4-to-headers-message');

const prepareHeaders = (headers) => {
const resolveHeaders = (headers) => {
if (typeof headers !== 'object') {
return headers;
}
Expand Down Expand Up @@ -38,42 +38,40 @@ const getSchema = (json) => {
};

class HeadersJsonExample extends JsonSchema {
constructor(expected, actual) {
if (typeof actual !== 'object') {
throw new errors.MalformedDataError('Actual is not an Object');
}

constructor(expected) {
if (typeof expected !== 'object') {
throw new errors.MalformedDataError('Expected is not an Object');
}

const preparedExpected = prepareHeaders(expected);
const preparedActual = prepareHeaders(actual);
const preparedSchema = getSchema(preparedExpected);
const resolvedExpected = resolveHeaders(expected);
const resolvedJsonSchema = getSchema(resolvedExpected);

if (preparedSchema && preparedSchema.properties) {
if (resolvedJsonSchema && resolvedJsonSchema.properties) {
const skippedHeaders = ['date', 'expires'];
skippedHeaders.forEach((headerName) => {
if (preparedSchema.properties[headerName]) {
delete preparedSchema.properties[headerName].enum;
if (resolvedJsonSchema.properties[headerName]) {
delete resolvedJsonSchema.properties[headerName].enum;
}
});
}

super(preparedSchema, preparedActual);
super(resolvedJsonSchema);

this.expected = preparedExpected;
this.actual = preparedActual;
this.schema = preparedSchema;
this.expected = resolvedExpected;
this.jsonSchema = resolvedJsonSchema;
}

validate() {
const result = super.validate();
validate(data) {
if (typeof data !== 'object') {
throw new errors.MalformedDataError('Actual is not an Object');
}

const results = super.validate(data);

if (result.length > 0) {
const resultCopy = clone(result, false);
if (results.length > 0) {
const resultCopy = clone(results, false);

for (let i = 0; i < result.length; i++) {
for (let i = 0; i < results.length; i++) {
resultCopy[i].message = tv4ToHeadersMessage(
resultCopy[i].message,
this.expected
Expand All @@ -83,7 +81,7 @@ class HeadersJsonExample extends JsonSchema {
return resultCopy;
}

return result;
return results;
}
}

Expand Down

0 comments on commit 6ae63b6

Please sign in to comment.