43 lines
1.0 KiB
TypeScript
43 lines
1.0 KiB
TypeScript
import { Email, Name, Phone, Result, UniqueID } from "..";
|
|
import { UndefinedOr } from "../../../utilities";
|
|
|
|
export const ensureIdIsValid = (value: string) =>
|
|
UniqueID.create(value, { generateOnEmpty: false });
|
|
|
|
export const ensureNameIsValid = (
|
|
value: UndefinedOr<string>,
|
|
label: string = "name",
|
|
) => {
|
|
const valueOrError = Name.create(value, {
|
|
label,
|
|
});
|
|
|
|
return valueOrError.isSuccess
|
|
? Result.ok(valueOrError.object)
|
|
: Result.fail(valueOrError.error);
|
|
};
|
|
|
|
export const ensureEmailIsValid = (
|
|
value: UndefinedOr<string>,
|
|
label: string = "email",
|
|
) => {
|
|
const valueOrError = Email.create(value, {
|
|
label,
|
|
});
|
|
|
|
return valueOrError.isSuccess
|
|
? Result.ok(valueOrError.object)
|
|
: Result.fail(valueOrError.error);
|
|
};
|
|
|
|
export const ensurePhoneIsValid = (
|
|
value: UndefinedOr<string>,
|
|
label: string = "phone",
|
|
) => {
|
|
const valueOrError = Phone.create(value, { label });
|
|
|
|
return valueOrError.isSuccess
|
|
? Result.ok(valueOrError.object)
|
|
: Result.fail(valueOrError.error);
|
|
};
|