diff --git a/src/api/index.js b/src/api/index.js index f85bb9c..ff6a422 100755 --- a/src/api/index.js +++ b/src/api/index.js @@ -58,6 +58,17 @@ export default ({ config, controller }) => { ) }) + // api.get('/:sheet/:tab/:resource/validate', (req, res) => { + // const { sheet, tab, resource } = req.params + // controller + // .retrieve(sheet, tab, resource) + // .then(data => res.json(data)) + // .catch(err => + // res.status(err.status || 404) + // .send({ error: err.message }) + // ) + // }) + // ERROR routes. Note that it is important that these come AFTER routes // like /update, so that the regex does not greedily match these routes. diff --git a/src/blueprinters/deeprows.js b/src/blueprinters/deeprows.js index 1e41db1..d9c2d27 100644 --- a/src/blueprinters/deeprows.js +++ b/src/blueprinters/deeprows.js @@ -1,4 +1,5 @@ import { fmtObj } from '../lib/util' +import { getColumnValidation } from '../lib/validation' /** * Each resource item is an object with values labelled according @@ -35,7 +36,6 @@ export default (data) => { } } }) - // generate the value for deep labels using the structure created data.forEach((row, idx) => { if (idx === 0) return @@ -61,6 +61,8 @@ export default (data) => { // move values for flat labels over from base structure.__flat.forEach(label => { + const validatedLabel = getColumnValidation(label, baseRow[label]) + console.log(validatedLabel) deepRow[label] = baseRow[label] }) if (!Object.keys(deepRow).every(k => deepRow[k] === '')) { diff --git a/src/lib/validation.js b/src/lib/validation.js index 307537c..5d120fa 100644 --- a/src/lib/validation.js +++ b/src/lib/validation.js @@ -1,35 +1,20 @@ import moment from 'moment'; const DATE_FORMAT = "MM/DD/YYYY"; -const TIME_REGEX = "^([01]\d|2[0-3]):?([0-5]\d)$" - -export const validateLongitude = value => { - return isFinite(value) && Math.abs(value) <= 180; -} - -export const validateLatitude = value => { - return isFinite(lat) && Math.abs(lat) <= 90; -} - -export const validateDate = date => { - return moment(date, DATE_FORMAT, true).isValid(); -} - -export const validateTime = time => { - return TIME_REGEX.test(time); -} +const TIME_REGEX = new RegExp("^([01]\d|2[0-3]):?([0-5]\d)$") -export const getColumnValidator = colName => { +export const getColumnValidation = (colName, value) => { switch(colName) { - case 'longitue': - return validateLongitude; + case 'longitude': + return isFinite(value) && Math.abs(value) <= 180; case 'latitude': - return validateLatitude; + return isFinite(value) && Math.abs(value) <= 90; case 'date': - return validateDate; + return moment(value, DATE_FORMAT, true).isValid(); case 'time': - return validateTime; - default: () => return true; + return TIME_REGEX.test(value); + default: + return true } } \ No newline at end of file