tariffs/src/utils/validate-empty-fields.ts
2022-12-16 03:36:33 +03:00

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];
};