Skip to content

Commit

Permalink
Merge 837df08 into 7a892a2
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-zakharchenko committed May 7, 2019
2 parents 7a892a2 + 837df08 commit ee8cfa3
Show file tree
Hide file tree
Showing 12 changed files with 451 additions and 442 deletions.
17 changes: 17 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
extends: ['airbnb-base', 'prettier'],
plugins: ['import'],
env: {
mocha: true,
node: true
},
rules: {
'consistent-return': 'off',
'class-methods-use-this': 'off',
'no-plusplus': 'off',

// Temporary overrides. Logic to be rewritten.
// TODO https://github.com/apiaryio/gavel.js/issues/150
'no-param-reassign': 'off'
}
};
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ before_install:
install:
- 'npm install --no-save'
script:
- 'npm run ci:lint || true' # TODO https://github.com/apiaryio/gavel.js/issues/146
- 'npm run ci:lint'
- 'npm run ci:test'
after_success:
- 'npm run ci:release'
61 changes: 28 additions & 33 deletions lib/mixins/validatable-http-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class Validatable {
this.validation.headers.realType === expectsJson &&
this.validation.headers.expectedType === expectsJson
) {
return (this.validation.headers.validator = 'HeadersJsonExample');
this.validation.headers.validator = 'HeadersJsonExample';
} else {
this.validation.headers.validator = null;

Expand Down Expand Up @@ -201,16 +201,16 @@ and expected data media type \
);
}

const contentType = this.headers && this.headers['content-type'];
const headersContentType = this.headers && this.headers['content-type'];

if (this.isJsonContentType(contentType)) {
if (this.isJsonContentType(headersContentType)) {
try {
jph.parse(this.body);
this.validation.body.realType = contentType;
this.validation.body.realType = headersContentType;
} catch (error) {
const message = {
message: `\
Real body 'Content-Type' header is '${contentType}' \
Real body 'Content-Type' header is '${headersContentType}' \
but body is not a parseable JSON:\n${error.message}\
`,
severity: 'error'
Expand All @@ -229,7 +229,6 @@ but body is not a parseable JSON:\n${error.message}\
}

setBodyExpectedType() {
let error, message;
this.validation.body.expectedType = null;

if (this.validation.body.results == null) {
Expand All @@ -241,25 +240,21 @@ but body is not a parseable JSON:\n${error.message}\
try {
const parsed = jph.parse(this.expected.bodySchema);

if (typeof parsed !== 'object' || Array.isArray(parsed)) {
message = {
message: `\
Can't validate. Expected body JSON Schema 'Content-Type' is
not a parseable JSON:\n${error.message}\
`,
severity: 'error'
};
this.validation.body.results.push(message);
} else {
if (typeof parsed === 'object') {
this.validation.body.expectedType = 'application/schema+json';
} else {
// Explicit empty error to be caught by the underlying "catch" block.
// Introduced to remove nasty shadowing and local variables mutations.
// TODO https://github.com/apiaryio/gavel.js/issues/150
throw new Error('');
}
} catch (error) {
message = {
message:
"Can't validate. Expected body JSON Schema is not a parseable JSON",
this.validation.body.results.push({
message: `Can't validate. Expected body JSON Schema is not a parseable JSON:\n${
error.message
}`,
severity: 'error'
};
this.validation.body.results.push(message);
});
}
} else {
this.validation.body.expectedType = 'application/schema+json';
Expand All @@ -275,22 +270,19 @@ not a parseable JSON:\n${error.message}\
jph.parse(this.expected.body);
this.validation.body.expectedType = expectedContentType;
} catch (error) {
message = {
this.validation.body.results.push({
message: `\
Can't validate. Expected body 'Content-Type' is '${expectedContentType}' \
but body is not a parseable JSON:\n${error.message}\
`,
severity: 'error'
};

this.validation.body.results.push(message);
});
}
} else {
try {
jph.parse(this.expected.body);
this.validation.body.expectedType = 'application/json';
} catch (error3) {
error = error3;
} catch (error) {
this.validation.body.expectedType = 'text/plain';
}
}
Expand All @@ -312,9 +304,9 @@ but body is not a parseable JSON:\n${error.message}\
if (errorsLength === 0) {
if (this.isJsonContentType(this.validation.body.realType)) {
if (this.validation.body.expectedType === 'application/schema+json') {
return (this.validation.body.validator = 'JsonSchema');
this.validation.body.validator = 'JsonSchema';
} else if (this.isJsonContentType(this.validation.body.expectedType)) {
return (this.validation.body.validator = 'JsonExample');
this.validation.body.validator = 'JsonExample';
} else {
this.validation.body.results.push({
message: `Can't validate real media type '${
Expand Down Expand Up @@ -358,9 +350,12 @@ but body is not a parseable JSON:\n${error.message}\
*/
runBodyValidator() {
if (this.validation.body.validator === null) {
return (this.validation.body.rawData = null);
this.validation.body.rawData = null;
} else {
let expected, real, results;
let expected;
let real;
let results;

const ValidatorClass = validators[this.validation.body.validator];

if (this.validation.body.validator === 'JsonSchema') {
Expand Down Expand Up @@ -433,8 +428,8 @@ but body is not a parseable JSON:\n${error.message}\
const { type } = contentType.parse(`${contentTypeValue}`);
const parsed = mediaTyper.parse(type);
return (
(parsed.type === 'application' && parsed.subtype === 'json')
|| parsed.suffix === 'json'
(parsed.type === 'application' && parsed.subtype === 'json') ||
parsed.suffix === 'json'
);
} catch (e) {
// The Content-Type value is basically a user input, it can be any
Expand Down
2 changes: 1 addition & 1 deletion lib/model/http-request.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { Validatable } = require('../mixins/validatable-http-message');
const clone = require('clone');
const { Validatable } = require('../mixins/validatable-http-message');

class HttpRequest extends Validatable {
/**
Expand Down
2 changes: 1 addition & 1 deletion lib/model/http-response.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { Validatable } = require('../mixins/validatable-http-message');
const clone = require('clone');
const { Validatable } = require('../mixins/validatable-http-message');

class HttpResponse extends Validatable {
/**
Expand Down
4 changes: 1 addition & 3 deletions lib/utils/get-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@ const nativeJsObjects = {
'[object Object]': 'object'
};

objectToString = Object.prototype.toString;

function stringifyType(any) {
return Object.prototype.toString.call(any);
}

function getType(any) {
const type = stringifyType(any);

return nativeJsObjects.hasOwnProperty(type)
return Object.prototype.hasOwnProperty.call(nativeJsObjects, type)
? nativeJsObjects[type]
: typeof any;
}
Expand Down
7 changes: 4 additions & 3 deletions lib/utils/schema-v4-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class SchemaV4Generator {
const schemaDict = {};

if (firstLevel) {
schemaDict['$schema'] = SCHEMA_VERSION;
schemaDict.$schema = SCHEMA_VERSION;
schemaDict.id = '#';
}

Expand Down Expand Up @@ -143,7 +143,8 @@ class SchemaV4Generator {
if (schemaType === 'object' && hasKeys) {
schemaDict.required = [];
schemaDict.properties = {};
for (let prop of Object.keys(baseObject || {})) {

Object.keys(baseObject || {}).forEach((prop) => {
const value = baseObject[prop];
getSchemaForObjectProperties = {
baseObject: value,
Expand All @@ -155,7 +156,7 @@ class SchemaV4Generator {
getSchemaForObjectProperties
);
schemaDict.required.push(prop);
}
});
} else if (schemaType === 'array' && baseObject.length > 0) {
schemaDict.items = [];

Expand Down
18 changes: 8 additions & 10 deletions lib/validators/json-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ const jsonSchemaOptions = {
`The ${prop} property must be ${getArticle(
validator[0]
)} ${validator} (current value is ${JSON.stringify(val)})."`,
except: (prop, val, validator) =>
`The ${prop} property must not be ${val}.`,
except: (prop, val) => `The ${prop} property must not be ${val}.`,
minimum: (prop, val, validator) =>
`The minimum value of the ${prop} must be ${validator} (current value is ${JSON.stringify(
val
Expand All @@ -82,8 +81,7 @@ const jsonSchemaOptions = {
`The ${prop} property is not divisible by ${validator} (current value is ${JSON.stringify(
val
)}).`,
uniqueItems: (prop, val, validator) =>
`All items in the ${prop} property must be unique.`
uniqueItems: (prop) => `All items in the ${prop} property must be unique.`
}
};

Expand All @@ -102,7 +100,7 @@ class JsonSchema {
this.data = JSON.parse(this.data);
} catch (error) {
const outError = new errors.DataNotJsonParsableError(
'JSON validator: body: ' + error.message
`JSON validator: body: ${error.message}`
);
outError.data = this.data;
throw outError;
Expand All @@ -114,7 +112,7 @@ class JsonSchema {
this.schema = JSON.parse(this.schema);
} catch (error) {
const outError = new errors.SchemaNotJsonParsableError(
'JSON validator: schema: ' + error.message
`JSON validator: schema: ${error.message}`
);
outError.schema = this.schema;
throw outError;
Expand Down Expand Up @@ -190,9 +188,9 @@ class JsonSchema {
return new ValidationErrors(this.output);
}

const hasSameData = deepEqual(this.data, this._dataUsed, { strict: true });
const hasSameData = deepEqual(this.data, this.usedData, { strict: true });
const hasSameSchema = hasSameData
? deepEqual(this.schema, this._schemaUsed, { strict: true })
? deepEqual(this.schema, this.usedSchema, { strict: true })
: true;

if (!hasSameData || !hasSameSchema) {
Expand All @@ -203,8 +201,8 @@ class JsonSchema {
}

validatePrivate() {
this._dataUsed = this.data;
this._schemaUsed = this.schema;
this.usedData = this.data;
this.usedSchema = this.schema;

switch (this.jsonSchemaVersion) {
case 'v3':
Expand Down
6 changes: 3 additions & 3 deletions lib/validators/text-diff.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const errors = require('../errors');
const DiffMatchPatch = require('googlediff');
const errors = require('../errors');

class TextDiff {
constructor(real, expected) {
Expand Down Expand Up @@ -45,9 +45,9 @@ class TextDiff {
);
this.output = dmp.patch_toText(patch);
return this.output;
} else {
throw error;
}

throw error;
}
}

Expand Down

0 comments on commit ee8cfa3

Please sign in to comment.