Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ module.exports = function(options) {

const actionDescriptionObj = {
_index: options.index
|| `${indexPrefix}${utils.assembleField(parsedRecord, options.indexField, separator)}`,
|| `${indexPrefix}${utils.assembleField(parsedRecord, options.indexField, separator, true)}`,
_type: options.type
|| utils.assembleField(parsedRecord, options.typeField, separator),
_id: id
Expand Down
19 changes: 15 additions & 4 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ const joi = require('joi');

const errors = require('./errors');

const replaceInvalidChars = (value) =>
value
.replace(/[\\\/\*\?\"\<\>\| \,]/g, '-')
.replace(/#/g, '-')
.replace(/^[_\-+]/, '')
.toLowerCase()

module.exports = {
validate(value, schema, options) {
const validationResult = joi.validate(
Expand All @@ -27,7 +34,7 @@ module.exports = {
return validationResult.value;
},

getField(parsedRecord, path) {
getField(parsedRecord, path, isIndex) {
const value = [parsedRecord.Keys, parsedRecord.NewImage, parsedRecord.OldImage]
.reduce((acc, entry) => {
return acc === undefined
Expand All @@ -39,13 +46,17 @@ module.exports = {
throw new errors.FieldNotFoundError(parsedRecord, path);
}

if (isIndex) {
return replaceInvalidChars(value);
}

return value;
},

assembleField(parsedRecord, paths, separator) {
assembleField(parsedRecord, paths, separator, isIndex) {
if (Array.isArray(paths)) {
return paths.map(path => this.getField(parsedRecord, path)).join(separator);
return paths.map(path => this.getField(parsedRecord, path, isIndex)).join(separator);
}
return this.getField(parsedRecord, paths);
return this.getField(parsedRecord, paths, isIndex);
}
};