-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experiment with grouping errors in array
Fix eslint Comment on code Separate validation logic to lib Unit tests Code formatting Use const for immutables and arrays Multiline condition statement with brackets Fix whitespace Added unit tests for argument validations Simplify validateHeaders by using lodash reduce Fix formatting after code review Use arrow function Removed redundant unit tests. Added jsdoc Removed argument validations. Minor comment fix.
- Loading branch information
Showing
3 changed files
with
90 additions
and
12 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
...nagement/sections/indices/add_data_steps/parse_csv_step/lib/__tests__/validate_headers.js
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,39 @@ | ||
import validateHeaders from '../validate_headers'; | ||
import expect from 'expect.js'; | ||
|
||
describe('validateHeaders', function () { | ||
|
||
describe('basic validations', function () { | ||
|
||
it('should return empty array if fields are valid', function () { | ||
const errors = validateHeaders(['aa', 'bb', 'cc', 'dd', 'ee']); | ||
expect(errors).to.eql([]); | ||
}); | ||
|
||
it('should return empty array if it contains white space field name', function () { | ||
const errors = validateHeaders(['aa', 'bb', ' ', ' ', 'ee']); | ||
expect(errors.length).to.be(0); | ||
}); | ||
|
||
it('should return an error if contains blank field name', function () { | ||
const errors = validateHeaders(['aa', 'bb', 'cc', '', 'ee']); | ||
expect(errors.length).to.be(1); | ||
expect(errors[0]).eql({ type: 'blank', positions: [4] }); | ||
}); | ||
|
||
it('should return an error if fields contain duplicate field name', function () { | ||
const errors = validateHeaders(['aa', 'bb', 'cc', 'dd', 'aa']); | ||
expect(errors.length).to.be(1); | ||
expect(errors[0]).eql({ type: 'duplicate', positions: [1, 5], fieldName: 'aa' }); | ||
}); | ||
|
||
it('should return multiple errors if fields contain duplicate and blank field name', function () { | ||
const errors = validateHeaders(['aa', 'bb', '', 'dd', 'aa', '', 'aa', 'dd', 'hh']); | ||
expect(errors.length).to.be(3); | ||
expect(errors[0]).eql({ type: 'duplicate', positions: [1, 5, 7], fieldName: 'aa' }); | ||
expect(errors[1]).eql({ type: 'blank', positions: [3, 6] }); | ||
expect(errors[2]).eql({ type: 'duplicate', positions: [4, 8], fieldName: 'dd' }); | ||
}); | ||
}); | ||
|
||
}); |
43 changes: 43 additions & 0 deletions
43
.../public/management/sections/indices/add_data_steps/parse_csv_step/lib/validate_headers.js
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,43 @@ | ||
import _ from 'lodash'; | ||
|
||
/** | ||
* @typedef error | ||
* @type Object | ||
* @property {String} fieldName The field name with error. | ||
* @property {Number[]} positions The field positions with error. One-based. | ||
* @property {String} type The type of error. Eg: duplicate, blank | ||
*/ | ||
|
||
/** | ||
* Check for errors with header fields for csv import preview. | ||
* Returns an empty array if no error is found. | ||
* | ||
* @param {String[]} fields An array of field names | ||
* @returns {error[]} errors An array of error objects | ||
*/ | ||
export default function validateHeaders(fields) { | ||
const errors = []; | ||
|
||
const fieldsWithPositions = _.reduce(fields, (result, field, index) => { | ||
(result[field] || (result[field] = [])).push(index + 1); | ||
return result; | ||
}, {}); | ||
|
||
_.forEach(fieldsWithPositions, (positions, field) => { | ||
if (_.isEmpty(field)) { | ||
errors.push({ | ||
positions: positions, | ||
type: 'blank' | ||
}); | ||
} | ||
else if (positions.length > 1) { | ||
errors.push({ | ||
fieldName: field, | ||
positions: positions, | ||
type: 'duplicate' | ||
}); | ||
} | ||
}); | ||
|
||
return errors; | ||
} |
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