in apps/newsletters-ui/src/app/components/SchemaForm/util.ts [62:111]
function fieldValueIsRightType(value: FieldValue, field: FieldDef): boolean {
const innerZod = recursiveUnwrap(field.zod);
if (innerZod instanceof ZodEnum) {
if (field.zod.isOptional() && typeof value === 'undefined') {
return true;
}
const options = isStringArray(innerZod.options)
? innerZod.options
: undefined;
return options?.includes(value as string) ?? false;
}
if (innerZod instanceof ZodDate) {
return value instanceof Date;
}
if (innerZod instanceof ZodArray && innerZod.element instanceof ZodString) {
return isStringArray(value);
}
if (innerZod instanceof ZodArray && innerZod.element instanceof ZodObject) {
// TODO - use field.recordSchema to validate each item?
return isPrimitiveRecordArray(value);
}
if (innerZod instanceof ZodObject) {
if (field.zod.isOptional() && typeof value === 'undefined') {
return true;
}
// TODO - use field.recordSchema to validate item?
// But then can'tedit one value unless all the others in the item are valid
return isPrimitiveRecord(value);
}
switch (typeof value) {
case 'undefined':
return field.zod.isOptional();
case 'string':
return innerZod instanceof ZodString;
case 'number':
return innerZod instanceof ZodNumber;
case 'boolean':
return innerZod instanceof ZodBoolean;
default:
return false;
}
}