Skip to content

Commit

Permalink
Review feedback - safe string handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jscheffl committed Jan 4, 2025
1 parent 59c6e8c commit 7ddb8f6
Show file tree
Hide file tree
Showing 10 changed files with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const isFieldBool = (fieldType: string) => fieldType === "boolean";
export const FlexibleFormFieldBool = ({ name, param }: FlexibleFormElementProps) => (
<Switch
colorPalette="blue"
defaultChecked={param.value as boolean}
defaultChecked={Boolean(param.value)}
id={`element_${name}`}
name={`element_${name}`}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const isFieldDate = (fieldType: string, fieldSchema: ParamSchema) =>

export const FlexibleFormFieldDate = ({ name, param }: FlexibleFormElementProps) => (
<Input
defaultValue={param.value as string}
defaultValue={typeof param.value === "string" ? param.value : undefined}
id={`element_${name}`}
name={`element_${name}`}
placeholder="yyyy-mm-dd"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const isFieldDateTime = (fieldType: string, fieldSchema: ParamSchema) =>

export const FlexibleFormFieldDateTime = ({ name, param }: FlexibleFormElementProps) => (
<Input
defaultValue={param.value as string}
defaultValue={typeof param.value === "string" ? param.value : undefined}
id={`element_${name}`}
name={`element_${name}`}
placeholder="yyyy-mm-ddThh:mm"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ import type { ParamSchema } from "src/queries/useDagParams";

import type { FlexibleFormElementProps } from ".";

export const isFieldDropdown = (fieldType: string, fieldSchema: ParamSchema) => {
const enumTypes = ["string", "number", "integer"];
const enumTypes = ["string", "number", "integer"];

return enumTypes.includes(fieldType) && Array.isArray(fieldSchema.enum);
};
export const isFieldDropdown = (fieldType: string, fieldSchema: ParamSchema) =>
enumTypes.includes(fieldType) && Array.isArray(fieldSchema.enum);

const labelLookup = (key: string, valuesDisplay: Record<string, string> | null): string => {
if (valuesDisplay && typeof valuesDisplay === "object") {
Expand All @@ -51,7 +50,7 @@ export const FlexibleFormFieldDropdown = ({ name, param }: FlexibleFormElementPr
return (
<Select.Root
collection={selectOptions}
defaultValue={[param.value as string]}
defaultValue={[String(param.value)]}
id={`element_${name}`}
name={`element_${name}`}
ref={contentRef}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const isFieldMultilineText = (fieldType: string, fieldSchema: ParamSchema

export const FlexibleFormFieldMultilineText = ({ name, param }: FlexibleFormElementProps) => (
<Textarea
defaultValue={param.value as string}
defaultValue={typeof param.value === "string" ? param.value : String(param.value)}
id={`element_${name}`}
name={`element_${name}`}
rows={6}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const isFieldNumber = (fieldType: string) => {
export const FlexibleFormFieldNumber = ({ name, param }: FlexibleFormElementProps) => (
<NumberInputRoot
allowMouseWheel
defaultValue={param.value as string}
defaultValue={String(param.value)}
id={`element_${name}`}
max={param.schema.maximum ?? undefined}
min={param.schema.minimum ?? undefined}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import type { FlexibleFormElementProps } from ".";
export const FlexibleFormFieldString = ({ name, param }: FlexibleFormElementProps) => (
<>
<Input
defaultValue={param.value as string}
defaultValue={String(param.value)}
id={`element_${name}`}
list={param.schema.examples ? `list_${name}` : undefined}
maxLength={param.schema.maxLength ?? undefined}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ export const isFieldStringArray = (fieldType: string, fieldSchema: ParamSchema)

export const FlexibleFormFieldStringArray = ({ name, param }: FlexibleFormElementProps) => (
<Textarea
defaultValue={(param.value as Array<string>).join("\n")}
defaultValue={
Array.isArray(param.value) ? (param.value as Array<string>).join("\n") : String(param.value)
}
id={`element_${name}`}
name={`element_${name}`}
rows={6}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const isFieldTime = (fieldType: string, fieldSchema: ParamSchema) =>

export const FlexibleFormFieldTime = ({ name, param }: FlexibleFormElementProps) => (
<Input
defaultValue={param.value as string}
defaultValue={typeof param.value === "string" ? param.value : undefined}
id={`element_${name}`}
name={`element_${name}`}
placeholder="hh:mm"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ export const isHidden = (fieldSchema: ParamSchema) => Boolean(fieldSchema.const)
/** Render a "const" field where user can not change data as hidden */
export const FlexibleFormHidden = ({ name, param }: FlexibleFormElementProps) => (
<VisuallyHidden asChild>
<input id={`element_${name}`} name={`element_${name}`} type="hidden" value={param.value as string} />
<input id={`element_${name}`} name={`element_${name}`} type="hidden" value={String(param.value)} />
</VisuallyHidden>
);

0 comments on commit 7ddb8f6

Please sign in to comment.