Seeking to extract data from an API and verify if all fields are strings, but if they are missing I aim to assign default values. My intention was to utilize the yup
library to validate the object accordingly, ensuring that the returned function is properly typed.
import { v4 } from "uuid";
import { array, object, string } from "yup";
let mySchema = array(
object({
id: string().default(() => v4()),
title: string().default(""),
description: string().default(""),
courseVersions: array(
object({
courseCodeId: string().default(""),
courseName: string().default(""),
})
).default([]),
})
);
export default function validateCourses(originalObject: any) {
const cleanObject = mySchema.someFunction(originalObject); // Hope yup has a function
console.log({ originalObject, cleanObject });
return cleanObject;
}