get required schema fields name into array mongoose typescript

The solution for “get required schema fields name into array mongoose typescript” can be found here. The following code will assist you in solving the problem.

const getRequiredFromSchemas =  } }>(
schema: T
): string[] => {
let requiredFields: string[] = [];
Object.keys(schema.paths).forEach((key: any) => {
const required = schema.paths[key as keyof typeof schema.paths]?.isRequired;
if (required) {
requiredFields.push(key);
}
});
return requiredFields;
};

Thank you for using DeclareCode; We hope you were able to resolve the issue.

More questions on [categories-list]

0
inline scripts encapsulated in