1
0
Fork 0
mirror of https://github.com/mealie-recipes/mealie.git synced 2025-07-19 05:09:40 +02:00
mealie/frontend/composables/use-validators.ts

42 lines
1.1 KiB
TypeScript
Raw Normal View History

import type { RequestResponse } from "~/lib/api/types/non-generated";
import type { ValidationResponse } from "~/lib/api/types/response";
import { required, email, whitespace, url, minLength, maxLength } from "~/lib/validators";
export const validators = {
required,
email,
whitespace,
url,
minLength,
maxLength,
};
/**
* useAsyncValidator us a factory function that returns an async function that
* when called will validate the input against the backend database and set the
* error messages when applicable to the ref.
*/
export const useAsyncValidator = (
value: Ref<string>,
2022-05-21 21:22:02 +02:00
validatorFunc: (v: string) => Promise<RequestResponse<ValidationResponse>>,
validatorMessage: string,
errorMessages: Ref<string[]>,
) => {
const valid = ref(false);
const validate = async () => {
errorMessages.value = [];
const { data } = await validatorFunc(value.value);
if (!data?.valid) {
valid.value = false;
errorMessages.value.push(validatorMessage);
return;
}
valid.value = true;
};
return { validate, valid };
};