This repository has been archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: uses ["path", "array"] structure for "location.property" value
- Loading branch information
1 parent
860746d
commit 5bc782e
Showing
3 changed files
with
123 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
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' | ||
]); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |