-
Notifications
You must be signed in to change notification settings - Fork 156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Validate props in retrospective post (aider assisted) #1973
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry by the drive by review, but since I handled something similar not long ago, I thought I would jump in.
const metricsConfigs: Array<Metric> = props.post.props?.metricsConfigs ? JSON.parse(props.post.props.metricsConfigs) : []; | ||
const metricsData: Array<RunMetricData> = props.post.props?.metricsData ? JSON.parse(props.post.props.metricsData) : []; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This validation is probably not enough. The following inputs should break this:
- A non JSON parseable string value like
"{"
- A non string value like
{}
- A non list JSON string like =
"{}"
If we want to be 100% safe, we would need to add some try catch (or create a "safeJSONParse" function that does the try catch for us), and some type guard.
const parsedMetricsConfig = safeJSONParse(props.post.props?.metricsConfigs);
const parsedMetricsData = safeJSONParse(props.post.props?.metricsData);
const metricsConfigs = isArrayOf(parsedMetricsConfig, isMetric) ? parsedMetricsConfig : [];
const metricsData = isArrayOf(parsedMetricsData, isMetricsData) ? parsedMetricsData : [];
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, just a couple of nitpicks you can ignore.
@@ -132,3 +132,40 @@ export function runCallsSlashCommand(command: string, channelId: string, teamId: | |||
}, | |||
}, window.origin); | |||
} | |||
|
|||
export function safeJSONParse<T>(value: unknown): T | null { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: I like the semantics of undefined
better than null
, but it is just about taste.
@@ -132,3 +132,40 @@ export function runCallsSlashCommand(command: string, channelId: string, teamId: | |||
}, | |||
}, window.origin); | |||
} | |||
|
|||
export function safeJSONParse<T>(value: unknown): T | null { | |||
if (!value || typeof value !== 'string') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this would be enough:
if (!value || typeof value !== 'string') { | |
if (typeof value !== 'string') { |
We can let the case of empty string being handled by JSON.parse
.
Summary
Validate props in the retrospective post
Ticket Link
https://mattermost.atlassian.net/browse/MM-61164
Checklist
[ ] Telemetry updated[ ] Gated by experimental feature flag[ ] Unit tests updated