mirror of
https://github.com/hay-kot/mealie.git
synced 2025-07-11 23:57:14 -07:00
Some checks are pending
CodeQL / Analyze (push) Waiting to run
Docker Nightly Production / Backend Server Tests (push) Waiting to run
Docker Nightly Production / Frontend Tests (push) Waiting to run
Docker Nightly Production / Build Package (push) Waiting to run
Docker Nightly Production / Build Tagged Release (push) Blocked by required conditions
Docker Nightly Production / Notify Discord (push) Blocked by required conditions
Release Drafter / ✏️ Draft release (push) Waiting to run
Co-authored-by: Michael Genson <71845777+michael-genson@users.noreply.github.com> Co-authored-by: Kuchenpirat <24235032+Kuchenpirat@users.noreply.github.com>
41 lines
1.1 KiB
TypeScript
41 lines
1.1 KiB
TypeScript
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>,
|
|
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 };
|
|
};
|