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

Commit

Permalink
chore: introduces "errors[n].location" for body validation results
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-zakharchenko committed Jul 3, 2019
1 parent 9ac35da commit 61e3db8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 30 deletions.
9 changes: 8 additions & 1 deletion lib/units/validateBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const mediaTyper = require('media-typer');
const contentTypeUtils = require('content-type');

const { TextDiff, JsonExample, JsonSchema } = require('../validators');
const isset = require('../utils/isset');
const { isValidField } = require('./isValid');

function isPlainText(mediaType) {
Expand Down Expand Up @@ -156,9 +157,15 @@ function getBodyValidator(realType, expectedType) {
*/
function validateBody(expected, actual) {
const values = {
expected: expected.body,
actual: actual.body
};

// Prevent assigning { expected: undefined }.
// Also ignore "bodySchema" as the expected value.
if (isset(expected.body)) {
values.expected = expected.body;
}

const errors = [];
const realBodyType = typeof actual.body;
const hasEmptyRealBody = actual.body === '';
Expand Down
44 changes: 18 additions & 26 deletions lib/validators/json-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,11 @@ const jsonSchemaOptions = {
singleError: false,
messages: {
minLength: (prop, val, validator) =>
`The ${prop} property must be at least ${validator} characters long (currently ${
val.length
} characters long).`,
`The ${prop} property must be at least ${validator} characters long (currently ${val.length} characters long).`,
maxLength: (prop, val, validator) =>
`The ${prop} property must not exceed ${validator} characters (currently${
val.length
} characters long).`,
`The ${prop} property must not exceed ${validator} characters (currently${val.length} characters long).`,
length: (prop, val, validator) =>
`The ${prop} property must be exactly ${validator} characters long (currently ${
val.length
} characters long).`,
`The ${prop} property must be exactly ${validator} characters long (currently ${val.length} characters long).`,
format: (prop, val, validator) =>
`The ${prop} property must be ${getArticle(
validator[0]
Expand All @@ -72,13 +66,9 @@ const jsonSchemaOptions = {
pattern: (prop, val, validator) =>
`The ${prop} value (${val}) does not match the ${validator} pattern.`,
maxItems: (prop, val, validator) =>
`The ${prop} property must not contain more than ${validator} items (currently contains ${
val.length
} items).`,
`The ${prop} property must not contain more than ${validator} items (currently contains ${val.length} items).`,
minItems: (prop, val, validator) =>
`The ${prop} property must contain at least ${validator} items (currently contains ${
val.length
} items).`,
`The ${prop} property must contain at least ${validator} items (currently contains ${val.length} items).`,
divisibleBy: (prop, val, validator) =>
`The ${prop} property is not divisible by ${validator} (current value is ${JSON.stringify(
val
Expand Down Expand Up @@ -138,9 +128,7 @@ class JsonSchema {
const validationResult = tv4.validateResult(this.schema, metaSchema);
if (!validationResult.valid) {
throw new errors.JsonSchemaNotValid(
`JSON schema is not valid draft ${this.jsonSchemaVersion}! ${
validationResult.error.message
} at path "${validationResult.error.dataPath}"`
`JSON schema is not valid draft ${this.jsonSchemaVersion}! ${validationResult.error.message} at path "${validationResult.error.dataPath}"`
);
}
}
Expand Down Expand Up @@ -230,23 +218,27 @@ class JsonSchema {

const results = Array.from({ length: data.length }, (_, index) => {
const item = data[index];
const { message, property } = item;
let pathArray = [];

if (item.property === null) {
if (property === null) {
pathArray = [];
} else if (
Array.isArray(item.property) &&
item.property.length === 1 &&
[null, undefined].includes(item.property[0])
Array.isArray(property) &&
property.length === 1 &&
[null, undefined].includes(property[0])
) {
pathArray = [];
} else {
pathArray = item.property;
pathArray = property;
}

return {
pointer: jsonPointer.compile(pathArray),
message: item.message
message,
location: {
pointer: jsonPointer.compile(pathArray),
property
}
};
});

Expand Down Expand Up @@ -314,9 +306,9 @@ class JsonSchema {
const pointer = jsonPointer.compile(pathArray);

amandaCompatibleError[index] = {
message: `At '${pointer}' ${error.message}`,
property: pathArray,
attributeValue: true,
message: `At '${pointer}' ${error.message}`,
validatorName: 'error'
};
}
Expand Down
1 change: 0 additions & 1 deletion test/cucumber/steps/fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const { expect } = chai;
module.exports = function() {
this.Then(/^field "([^"]*)" equals:$/, function(fieldName, expectedJson) {
const expected = jhp.parse(expectedJson);

expect(this.result.fields[fieldName]).to.deep.equal(expected);
});
};
4 changes: 2 additions & 2 deletions test/cucumber/steps/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ module.exports = function() {
this.Given(
/^you expect "body" field to match the following "([^"]*)":$/,
function(bodyType, value) {
switch (bodyType) {
case 'JSON schema':
switch (bodyType.toLowerCase()) {
case 'json schema':
this.expected.bodySchema = value;
break;
default:
Expand Down

0 comments on commit 61e3db8

Please sign in to comment.