I created a generic filter by muti selection fields and the value to filter is in input field
- select the property.
- choode operator to filter
- input the vlaues to filter
I need to validate using the relation between the object property and the values
export const FilterItem = <T extends SomeZodObject>(dataType: T) =>
z
.object({
Field: z.string().refine(
d => {
return Object.keys(dataType.keyof().Enum).some(
k => k.toLocaleLowerCase() === d.toLocaleLowerCase(),
);
},
{
path: [],
},
),
Operator: filterOperatorSchema,
Value: z.any(),
})
.refine(
data => {
const _type = Object.entries(dataType.shape).find(
([key]) => key.toLocaleLowerCase() === data.Field.toLocaleLowerCase(),
);
if (
!_type ||
Object.keys(_type).length === 0 ||
!_type[1]?._def.typeName
)
return false;
if (isArray(data.Value)) {
return Array.from(data.Value).every(
v => _type[1].safeParse(v).success,
);
} else {
return _type[1].safeParse(data.Value).success;
}
},
{
message: "Field and Value types not the same",
path: ["Value"],
},
);
value is an input field so, data type is string. how to coerce the input and use it in the custom validation : like numbers is treated as string