-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(dashboard): sanitize imports (#530)
- Loading branch information
1 parent
3b2c47d
commit a4fed0e
Showing
3 changed files
with
92 additions
and
104 deletions.
There are no files selected for viewing
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
72 changes: 72 additions & 0 deletions
72
dashboard/src/scenes/data-import-export/importSanitizer.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,72 @@ | ||
import { dayjsInstance } from '../../services/date'; | ||
|
||
const sanitizeString = (value) => { | ||
if (!value) return null; | ||
if (typeof value === 'string') return value; | ||
if (typeof `${value}` === 'string') return `${value}`; | ||
return null; | ||
}; | ||
|
||
const sanitizeNumber = (value) => { | ||
if (!isNaN(value)) return value; | ||
if (!isNaN(parseInt(value, 10))) return parseInt(value, 10); | ||
return null; | ||
}; | ||
|
||
const sanitizeDate = (value) => { | ||
// https://stackoverflow.com/a/643827/5225096 | ||
if (typeof value?.getMonth === 'function' || value instanceof dayjsInstance) return value; | ||
if (!isNaN(new Date(value).getMonth())) return new Date(value); | ||
return null; | ||
}; | ||
|
||
const sanitizeYesNo = (value) => { | ||
value = sanitizeString(value); | ||
if (!value) return null; | ||
if (['Oui', 'Non'].includes(value)) return value; | ||
if (value === 'No') return 'Non'; | ||
if (value === 'Yes') return 'Oui'; | ||
return null; | ||
}; | ||
|
||
const sanitizeEnum = (value, possibleValues = []) => { | ||
value = sanitizeString(value); | ||
if (!value) return null; | ||
if (possibleValues.includes(value)) return value; | ||
return null; | ||
}; | ||
|
||
const sanitizeMultiChoice = (value, possibleValues = []) => { | ||
// value is either string or array | ||
if (!Array.isArray(value)) { | ||
value = sanitizeString(value); | ||
if (!value) return null; | ||
value = value.split(','); | ||
} | ||
value = value.filter((value) => possibleValues.includes(value)); | ||
if (value.length) return value; | ||
return null; | ||
}; | ||
|
||
const sanitizeBoolean = (value) => { | ||
if (typeof value === 'undefined') return null; | ||
// We have to handle the case where value is a string (cf: import XLSX users). | ||
if (typeof value === 'string') { | ||
if (['true', 'oui', 'yes'].includes(value.toLowerCase())) return true; | ||
if (['false', 'non', 'no'].includes(value.toLowerCase())) return false; | ||
} | ||
return Boolean(value); | ||
}; | ||
|
||
export function sanitizeFieldValue(field, { v: rawValue, w: formattedText }) { | ||
if (field.type === 'text') return sanitizeString(rawValue); | ||
if (field.type === 'textarea') return sanitizeString(rawValue); | ||
if (field.type === 'number') return sanitizeNumber(rawValue); | ||
if (field.type === 'date') return sanitizeDate(formattedText); | ||
if (field.type === 'date-with-time') return sanitizeDate(formattedText); | ||
if (field.type === 'yes-no') return sanitizeYesNo(rawValue); | ||
if (field.type === 'enum') return sanitizeEnum(rawValue, field.options); | ||
if (field.type === 'multi-choice') return sanitizeMultiChoice(rawValue, field.options); | ||
if (field.type === 'boolean') return sanitizeBoolean(rawValue); | ||
return rawValue; | ||
} |
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