mirror of
https://github.com/bellingcat/datasheet-server.git
synced 2026-06-12 05:18:31 +03:00
Added validation buttons per row in datasheet UI; need to configure correct validations per data field
This commit is contained in:
@@ -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
|
// ERROR routes. Note that it is important that these come AFTER routes
|
||||||
// like /update, so that the regex does not greedily match these routes.
|
// like /update, so that the regex does not greedily match these routes.
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { fmtObj } from '../lib/util'
|
import { fmtObj } from '../lib/util'
|
||||||
|
import { getColumnValidation } from '../lib/validation'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Each resource item is an object with values labelled according
|
* 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
|
// generate the value for deep labels using the structure created
|
||||||
data.forEach((row, idx) => {
|
data.forEach((row, idx) => {
|
||||||
if (idx === 0) return
|
if (idx === 0) return
|
||||||
@@ -61,6 +61,8 @@ export default (data) => {
|
|||||||
|
|
||||||
// move values for flat labels over from base
|
// move values for flat labels over from base
|
||||||
structure.__flat.forEach(label => {
|
structure.__flat.forEach(label => {
|
||||||
|
const validatedLabel = getColumnValidation(label, baseRow[label])
|
||||||
|
console.log(validatedLabel)
|
||||||
deepRow[label] = baseRow[label]
|
deepRow[label] = baseRow[label]
|
||||||
})
|
})
|
||||||
if (!Object.keys(deepRow).every(k => deepRow[k] === '')) {
|
if (!Object.keys(deepRow).every(k => deepRow[k] === '')) {
|
||||||
|
|||||||
@@ -1,35 +1,20 @@
|
|||||||
import moment from 'moment';
|
import moment from 'moment';
|
||||||
|
|
||||||
const DATE_FORMAT = "MM/DD/YYYY";
|
const DATE_FORMAT = "MM/DD/YYYY";
|
||||||
const TIME_REGEX = "^([01]\d|2[0-3]):?([0-5]\d)$"
|
const TIME_REGEX = new RegExp("^([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);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
export const getColumnValidator = colName => {
|
export const getColumnValidation = (colName, value) => {
|
||||||
switch(colName) {
|
switch(colName) {
|
||||||
case 'longitue':
|
case 'longitude':
|
||||||
return validateLongitude;
|
return isFinite(value) && Math.abs(value) <= 180;
|
||||||
case 'latitude':
|
case 'latitude':
|
||||||
return validateLatitude;
|
return isFinite(value) && Math.abs(value) <= 90;
|
||||||
case 'date':
|
case 'date':
|
||||||
return validateDate;
|
return moment(value, DATE_FORMAT, true).isValid();
|
||||||
case 'time':
|
case 'time':
|
||||||
return validateTime;
|
return TIME_REGEX.test(value);
|
||||||
default: () => return true;
|
default:
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user