16 lines
575 B
TypeScript
16 lines
575 B
TypeScript
import type { ObjectWithRequiredFields } from "@/types/object-with-required-fields";
|
|
|
|
type KeyValue = Record<string, unknown>;
|
|
type Keys<T> = Array<keyof T>;
|
|
type ValidateEmptyFields<T extends KeyValue> = [ObjectWithRequiredFields<T> | undefined, Error | null];
|
|
|
|
export const validateEmptyFields = <T extends KeyValue>(record: T, keys: Keys<T> = []): ValidateEmptyFields<T> => {
|
|
for (const key of keys) {
|
|
if (!record[key]) {
|
|
return [undefined, new Error(`field <${key as string}> is empty`)];
|
|
}
|
|
}
|
|
|
|
return [record as ObjectWithRequiredFields<T>, null];
|
|
};
|