diff --git a/lib/utils/to-gavel-result.js b/lib/utils/to-gavel-result.js deleted file mode 100644 index 80c49240..00000000 --- a/lib/utils/to-gavel-result.js +++ /dev/null @@ -1,31 +0,0 @@ -const jsonPointer = require('json-pointer'); - -function splitProperty(property) { - return property.split(/\.|\[|\]/).filter(Boolean); -} - -function reduceProperties(acc, property) { - return acc.concat(splitProperty(property)); -} - -/** - * Converts legacy (Amanda/TV4) error messages - * to the Gavel-compliant structure. - */ -function toGavelResult(legacyErrors) { - return Array.from({ length: legacyErrors.length }, (_, index) => { - const item = legacyErrors[index]; - const propertyPath = item.property.reduce(reduceProperties, []); - const pointer = jsonPointer.compile(propertyPath); - - return { - message: item.message, - location: { - pointer, - property: propertyPath - } - }; - }); -} - -module.exports = toGavelResult; diff --git a/lib/utils/tv4-to-headers-message.js b/lib/utils/tv4-to-headers-message.js deleted file mode 100644 index 1f49f3d5..00000000 --- a/lib/utils/tv4-to-headers-message.js +++ /dev/null @@ -1,23 +0,0 @@ -const caseless = require('caseless'); - -module.exports = (message, expectedHeaders) => { - let newMessage; - - if (message.includes('Missing required property:')) { - const headerName = message.split('Missing required property: ')[1]; - newMessage = `Header '${headerName}' is missing`; - } else if (message.includes('No enum match for: ')) { - const splitted = message.split('\' No enum match for: "'); - const headerName = splitted[0].replace(/^At '\//, ''); - const headerValue = splitted[1].replace(/"$/, ''); - - const expected = caseless(expectedHeaders).get(headerName); - newMessage = `Header '${headerName}' has value '${headerValue}' instead of '${expected}'`; - } else { - throw new Error( - "Unknown tv4 error message can't convert to headers message." - ); - } - - return newMessage; -}; diff --git a/lib/utils/warn-on-deprecation.js b/lib/utils/warn-on-deprecation.js deleted file mode 100644 index caca86fb..00000000 --- a/lib/utils/warn-on-deprecation.js +++ /dev/null @@ -1,16 +0,0 @@ -/* eslint-disable no-console */ - -/** - * Outputs a deprecation message. - * Supports silencing the messages based on the environmental variable. - * @param {string} message - */ -module.exports = function warnOnDeprecation(message) { - const shouldOutputMessage = - typeof process === 'undefined' || - !process.env.SUPPRESS_DEPRECATION_WARNINGS; - - if (shouldOutputMessage) { - console.warn(`DEPRECATED: ${message}`); - } -}; diff --git a/test/unit/utils/to-gavel-result.test.js b/test/unit/utils/to-gavel-result.test.js deleted file mode 100644 index 69c539e8..00000000 --- a/test/unit/utils/to-gavel-result.test.js +++ /dev/null @@ -1,87 +0,0 @@ -const { expect } = require('chai'); -const toGavelResult = require('../../../lib/utils/to-gavel-result'); - -describe('toGavelResult', () => { - // Currently TV4 output is coerced to Amanda format. - // Then Amanda format is coerced to Gavel public API. - describe('given Amanda errors', () => { - let coercedErrors; - - before(() => { - coercedErrors = toGavelResult({ - length: 2, - 0: { - property: ['users', 'username'], - propertyValue: 123, - attributeName: 'type', - attributeValue: 'string', - message: 'Amanda error message' - }, - 1: { - property: ['friends[2]', 'online'], - propertyValue: false, - attributeName: 'type', - attributeValue: 'boolean', - message: 'Arbitrary error message about "online"' - } - }); - }); - - describe('given coerced to Gavel-compliant error', () => { - it('should return 2 errors', () => { - expect(coercedErrors).to.have.lengthOf(2); - }); - - describe('given in-object error', () => { - it('should preserve the original error message', () => { - expect(coercedErrors[0]).to.have.property( - 'message', - 'Amanda error message' - ); - }); - - describe('should produce "location" property', () => { - it('should have "pointer"', () => { - expect(coercedErrors[0]).to.have.nested.property( - 'location.pointer', - '/users/username' - ); - }); - - it('should have "property"', () => { - expect(coercedErrors[0].location.property).to.deep.equal([ - 'users', - 'username' - ]); - }); - }); - }); - - describe('given in-array error', () => { - it('should preserve the original error message', () => { - expect(coercedErrors[1]).to.have.property( - 'message', - 'Arbitrary error message about "online"' - ); - }); - - describe('should produce "location" property', () => { - it('should have "pointer"', () => { - expect(coercedErrors[1]).to.have.nested.property( - 'location.pointer', - '/friends/2/online' - ); - }); - - it('should have "property"', () => { - expect(coercedErrors[1].location.property).to.deep.equal([ - 'friends', - '2', - 'online' - ]); - }); - }); - }); - }); - }); -}); diff --git a/test/unit/utils/tv4-to-headers-message.test.js b/test/unit/utils/tv4-to-headers-message.test.js deleted file mode 100644 index 68cac17c..00000000 --- a/test/unit/utils/tv4-to-headers-message.test.js +++ /dev/null @@ -1,36 +0,0 @@ -const { assert } = require('chai'); - -const tv4ToHeadersMessage = require('../../../lib/utils/tv4-to-headers-message'); -const fixtures = require('../../fixtures'); - -describe('tv4ToHeadersMessages()', () => { - const expectedHeaders = fixtures.sampleHeaders; - describe('when message for missing header', () => { - it('should return message with right text', () => { - const tv4Message = "At '/header2' Missing required property: header2"; - const message = tv4ToHeadersMessage(tv4Message, expectedHeaders); - assert.equal(message, "Header 'header2' is missing"); - }); - }); - - describe('when message for different value', () => { - it('should return message with right text', () => { - const tv4Message = - 'At \'/content-type\' No enum match for: "application/fancy-madiatype"'; - const message = tv4ToHeadersMessage(tv4Message, expectedHeaders); - assert.equal( - message, - `Header 'content-type' has value 'application/fancy-madiatype' instead of 'application/json'` - ); - }); - }); - - describe('when unknonw message', () => { - it('should throw an error', () => { - const fn = () => { - tv4ToHeadersMessage('String does not match pattern: {pattern}'); - }; - assert.throws(fn); - }); - }); -});