@vee-validate/nuxt
Version:
Official vee-validate nuxt module
155 lines (152 loc) • 4.53 kB
JavaScript
import { defineNuxtModule, addImports, addComponent, logger, resolveModule } from '@nuxt/kit';
import { isPackageExists } from 'local-pkg';
const components = ["ErrorMessage", "Field", "FieldArray", "Form"];
const composables = [
"useField",
"useFieldArray",
"useFieldError",
"useFieldValue",
"useForm",
"useFormContext",
"useFormErrors",
"useFormValues",
"useIsFieldDirty",
"useIsFieldTouched",
"useIsFieldValid",
"useIsFormDirty",
"useIsFormTouched",
"useIsFormValid",
"useIsSubmitting",
"useIsValidating",
"useResetForm",
"useSubmitCount",
"useSubmitForm",
"useValidateField",
"useValidateForm"
];
const schemaProviderResolvers = {
zod: "@vee-validate/zod",
yup: "@vee-validate/yup",
valibot: "@vee-validate/valibot"
};
const module = defineNuxtModule({
meta: {
name: "vee-validate",
configKey: "veeValidate"
},
defaults: {
autoImports: true,
componentNames: {}
},
setup(options, nuxt) {
addMjsAlias("vee-validate", "vee-validate", nuxt);
prepareVeeValidate(nuxt);
if (options.autoImports) {
composables.forEach((composable) => {
addImports({
name: composable,
as: composable,
from: "vee-validate"
});
});
components.forEach((component) => {
addComponent({
name: options.componentNames?.[component] ?? component,
export: component,
filePath: "vee-validate"
});
});
}
if (options.typedSchemaPackage === "none") {
return;
}
if (options.typedSchemaPackage === "yup") {
checkForYup(options, nuxt);
return;
}
if (options.typedSchemaPackage === "zod") {
checkForZod(options, nuxt);
return;
}
if (options.typedSchemaPackage === "valibot") {
checkForValibot(options, nuxt);
return;
}
if (!checkForYup(options, nuxt)) {
if (!checkForZod(options, nuxt)) {
checkForValibot(options, nuxt);
}
}
}
});
function checkSchemaResolverDependencies(pkgName) {
const makeMsg = (installed, uninstalled) => `You seem to be using ${installed}, but you have not installed ${uninstalled}. Please install it to use ${pkgName} with vee-validate.`;
const resolverPkg = schemaProviderResolvers[pkgName];
if (isPackageExists(pkgName) && !isPackageExists(resolverPkg)) {
logger.warn(makeMsg(pkgName, resolverPkg));
return true;
}
if (isPackageExists(resolverPkg) && !isPackageExists(pkgName)) {
logger.warn(makeMsg(resolverPkg, pkgName));
return true;
}
}
function checkForValibot(options, nuxt) {
checkSchemaResolverDependencies("valibot");
if (isPackageExists("@vee-validate/valibot") && isPackageExists("valibot")) {
logger.info("Using valibot with vee-validate");
if (options.autoImports) {
addImports({
name: "toTypedSchema",
as: "toTypedSchema",
from: "@vee-validate/valibot"
});
}
addMjsAlias("@vee-validate/valibot", "vee-validate-valibot", nuxt);
return true;
}
return false;
}
function checkForZod(options, nuxt) {
checkSchemaResolverDependencies("zod");
if (isPackageExists("@vee-validate/zod") && isPackageExists("zod")) {
logger.info("Using zod with vee-validate");
if (options.autoImports) {
addImports({
name: "toTypedSchema",
as: "toTypedSchema",
from: "@vee-validate/zod"
});
}
addMjsAlias("@vee-validate/zod", "vee-validate-zod", nuxt);
return true;
}
return false;
}
function checkForYup(options, nuxt) {
checkSchemaResolverDependencies("yup");
if (isPackageExists("@vee-validate/yup") && isPackageExists("yup")) {
logger.info("Using yup with vee-validate");
if (options.autoImports) {
addImports({
name: "toTypedSchema",
as: "toTypedSchema",
from: "@vee-validate/yup"
});
}
addMjsAlias("@vee-validate/yup", "vee-validate-yup", nuxt);
return true;
}
return false;
}
function addMjsAlias(pkgName, fileName, nuxt) {
nuxt.options.alias[pkgName] = nuxt.options.alias[pkgName] || resolveModule(`${pkgName}/dist/${fileName}.mjs`, {
paths: [nuxt.options.rootDir, import.meta.url]
});
}
function prepareVeeValidate(nuxt) {
nuxt.options.vite.optimizeDeps = nuxt.options.vite.optimizeDeps || {};
nuxt.options.vite.optimizeDeps.exclude = nuxt.options.vite.optimizeDeps.exclude || [];
nuxt.options.vite.optimizeDeps.exclude.push("vee-validate", "@vee-validate/rules");
}
export { module as default };