diff --git a/lib/editor/util/validation.js b/lib/editor/util/validation.js index 486849006..dcb73bf70 100644 --- a/lib/editor/util/validation.js +++ b/lib/editor/util/validation.js @@ -1,5 +1,5 @@ // @flow - +import clone from 'lodash.clonedeep' import moment from 'moment' import validator from 'validator' @@ -22,7 +22,6 @@ export function validate ( const isRequiredButEmpty = required && doesNotExist(value) let reason = 'Required field must not be empty' const agencies = getTableById(tableData, 'agency') - const scheduleExceptions = getTableById(tableData, 'scheduleexception') switch (inputType) { case 'GTFS_ID': const indices = [] @@ -190,24 +189,24 @@ export function validate ( return false case 'EXCEPTION_DATE': // a date cannot be selected more than once (for all exceptions) const dateMap = {} - // FIXME: this declaration of exceptions is redundant - const exceptions: Array = (scheduleExceptions: Array) + // Clone exceptions to avoid mutating table in store. + const scheduleExceptions: Array = clone(getTableById(tableData, 'scheduleexception')) if (entity) { - const exceptionIndex = exceptions.findIndex(se => se.id === entity.id) + const exceptionIndex = scheduleExceptions.findIndex(se => se.id === entity.id) if (exceptionIndex !== -1) { - exceptions.splice(exceptionIndex, 1) + scheduleExceptions.splice(exceptionIndex, 1) } const castedScheduleException: ScheduleException = ((entity: any): ScheduleException) - exceptions.push(castedScheduleException) + scheduleExceptions.push(castedScheduleException) } - for (let i = 0; i < exceptions.length; i++) { - if (exceptions[i].dates) { - exceptions[i].dates.map(d => { + for (let i = 0; i < scheduleExceptions.length; i++) { + if (scheduleExceptions[i].dates) { + scheduleExceptions[i].dates.map(d => { if (typeof dateMap[d] === 'undefined') { dateMap[d] = [] } - dateMap[d].push(exceptions[i].id) + dateMap[d].push(scheduleExceptions[i].id) }) } }