refactor: Fix type issues for parameter input components (#9449)

This commit is contained in:
Elias Meire
2024-05-21 15:04:20 +02:00
committed by GitHub
parent cd751e7cc8
commit 711c46f205
36 changed files with 315 additions and 243 deletions

View File

@@ -1118,6 +1118,9 @@ export type NodeParameterValueType =
| NodeParameterValue
| INodeParameters
| INodeParameterResourceLocator
| ResourceMapperValue
| FilterValue
| AssignmentCollectionValue
| NodeParameterValue[]
| INodeParameters[]
| INodeParameterResourceLocator[]
@@ -2374,25 +2377,32 @@ export interface ResourceMapperField {
readOnly?: boolean;
}
export type FieldType =
| 'string'
| 'string-alphanumeric'
| 'number'
| 'dateTime'
| 'boolean'
| 'time'
| 'array'
| 'object'
| 'options'
| 'url'
| 'jwt';
export type ValidationResult = {
valid: boolean;
errorMessage?: string;
newValue?: string | number | boolean | object | null | undefined;
export type FieldTypeMap = {
// eslint-disable-next-line id-denylist
boolean: boolean;
// eslint-disable-next-line id-denylist
number: number;
// eslint-disable-next-line id-denylist
string: string;
'string-alphanumeric': string;
dateTime: string;
time: string;
array: unknown[];
object: object;
options: any;
url: string;
jwt: string;
};
export type FieldType = keyof FieldTypeMap;
export type ValidationResult<T extends FieldType = FieldType> =
| { valid: false; errorMessage: string }
| {
valid: true;
newValue?: FieldTypeMap[T];
};
export type ResourceMapperValue = {
mappingMode: string;
value: { [key: string]: string | number | boolean | null } | null;
@@ -2418,9 +2428,9 @@ export interface FilterOperatorValue {
export type FilterConditionValue = {
id: string;
leftValue: unknown;
leftValue: NodeParameterValue;
operator: FilterOperatorValue;
rightValue: unknown;
rightValue: NodeParameterValue;
};
export type FilterOptionsValue = {

View File

@@ -111,7 +111,13 @@ function parseFilterConditionValues(
};
}
return { ok: true, result: { left: parsedLeftValue.newValue, right: parsedRightValue.newValue } };
return {
ok: true,
result: {
left: parsedLeftValue.valid ? parsedLeftValue.newValue : undefined,
right: parsedRightValue.valid ? parsedRightValue.newValue : undefined,
},
};
}
function parseRegexPattern(pattern: string): RegExp {

View File

@@ -177,14 +177,21 @@ type ValidateFieldTypeOptions = Partial<{
strict: boolean;
parseStrings: boolean;
}>;
// Validates field against the schema and tries to parse it to the correct type
export function validateFieldType<K extends FieldType>(
fieldName: string,
value: unknown,
type: K,
options?: ValidateFieldTypeOptions,
): ValidationResult<K>;
// eslint-disable-next-line complexity
export const validateFieldType = (
export function validateFieldType(
fieldName: string,
value: unknown,
type: FieldType,
options: ValidateFieldTypeOptions = {},
): ValidationResult => {
): ValidationResult {
if (value === null || value === undefined) return { valid: true };
const strict = options.strict ?? false;
const valueOptions = options.valueOptions ?? [];
@@ -308,4 +315,4 @@ export const validateFieldType = (
return { valid: true, newValue: value };
}
}
};
}