Skip to content

Commit 89b3f65

Browse files
schema check
1 parent c2c6378 commit 89b3f65

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

airflow-core/src/airflow/ui/src/components/FlexibleForm/FieldMultiType.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,19 @@ import { paramPlaceholder, useParamStore } from "src/queries/useParamStore";
2222

2323
import type { FlexibleFormElementProps } from ".";
2424

25+
const matchesSchemaType = (parsed: unknown, types: Array<string | undefined>): boolean => {
26+
const valueType = Array.isArray(parsed) ? "array" : parsed === null ? "null" : typeof parsed;
27+
28+
// "integer" is a JSON Schema distinction; both map to JS "number"
29+
return types.some((type) => type === valueType || (type === "integer" && valueType === "number"));
30+
};
31+
2532
export const FieldMultiType = ({ name, namespace = "default", onUpdate }: FlexibleFormElementProps) => {
2633
const { disabled, paramsDict, setParamsDict } = useParamStore(namespace);
2734
const param = paramsDict[name] ?? paramPlaceholder;
2835

36+
const schemaTypes = Array.isArray(param.schema.type) ? param.schema.type : [param.schema.type];
37+
2938
// Display objects as pretty-printed JSON, plain strings as-is.
3039
const displayValue =
3140
param.value !== null && typeof param.value === "object"
@@ -39,7 +48,11 @@ export const FieldMultiType = ({ name, namespace = "default", onUpdate }: Flexib
3948
paramsDict[name].value = null;
4049
} else {
4150
try {
42-
paramsDict[name].value = JSON.parse(value) as JSON;
51+
const parsed = JSON.parse(value) as JSON;
52+
53+
// Only store the parsed value if its type is valid per the schema;
54+
// otherwise fall back to the raw string (e.g. "45" stays a string for type=["string","object"]).
55+
paramsDict[name].value = matchesSchemaType(parsed, schemaTypes) ? parsed : value;
4356
} catch {
4457
paramsDict[name].value = value;
4558
}

airflow-core/src/airflow/ui/src/components/FlexibleForm/FieldSelector.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ const isFieldStringArray = (fieldType: string, fieldSchema: ParamSchema) =>
9191

9292
const isFieldMultiType = (fieldSchema: ParamSchema) =>
9393
Array.isArray(fieldSchema.type) &&
94-
fieldSchema.type.filter((type) => type !== "null").length > 1;
94+
fieldSchema.type.filter((type) => type !== "null").length > 1 &&
95+
!Array.isArray(fieldSchema.enum) &&
96+
!Array.isArray(fieldSchema.examples);
9597

9698
const isFieldTime = (fieldType: string, fieldSchema: ParamSchema) =>
9799
fieldType === "string" && fieldSchema.format === "time";

0 commit comments

Comments
 (0)