Skip to content

Commit 70760b9

Browse files
committed
feat: expose toTyped schema and show helpful logs to the user
1 parent dde2fc4 commit 70760b9

File tree

3 files changed

+95
-16
lines changed

3 files changed

+95
-16
lines changed

packages/nuxt/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@
3636
"dist/*.d.ts"
3737
],
3838
"dependencies": {
39-
"vee-validate": "^4.8.5",
40-
"@nuxt/kit": "^3.4.0"
39+
"@nuxt/kit": "^3.4.0",
40+
"local-pkg": "^0.4.3",
41+
"vee-validate": "^4.8.5"
4142
},
4243
"devDependencies": {
4344
"@nuxt/eslint-config": "^0.1.1",

packages/nuxt/src/module.ts

Lines changed: 89 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { defineNuxtModule, addComponent, addImports } from '@nuxt/kit';
22
import type { NuxtModule } from '@nuxt/schema';
3+
import { isPackageExists } from 'local-pkg';
4+
5+
interface VeeValidateModuleOptions {
6+
autoImports?: boolean;
7+
}
38

49
const components = ['Field', 'Form', 'ErrorMessage', 'FieldArray'];
510

@@ -25,26 +30,96 @@ const composables = [
2530
'useValidateForm',
2631
];
2732

28-
export default defineNuxtModule({
33+
export default defineNuxtModule<VeeValidateModuleOptions>({
2934
meta: {
3035
name: 'vee-valiate',
3136
configKey: 'veeValidate',
3237
},
38+
defaults: {
39+
autoImports: true,
40+
},
3341
setup(options, nuxt) {
34-
composables.forEach(composable => {
35-
addImports({
36-
name: composable,
37-
as: composable,
38-
from: 'vee-validate',
42+
if (options.autoImports) {
43+
composables.forEach(composable => {
44+
addImports({
45+
name: composable,
46+
as: composable,
47+
from: 'vee-validate',
48+
});
3949
});
40-
});
4150

42-
components.forEach(component => {
43-
addComponent({
44-
name: component,
45-
export: component,
46-
filePath: 'vee-validate',
51+
components.forEach(component => {
52+
addComponent({
53+
name: component,
54+
export: component,
55+
filePath: 'vee-validate',
56+
});
4757
});
48-
});
58+
}
59+
60+
const usingYup = checkForYup(options);
61+
if (!usingYup) {
62+
checkForZod(options);
63+
}
4964
},
50-
}) as NuxtModule;
65+
}) as NuxtModule<VeeValidateModuleOptions>;
66+
67+
function checkForZod(options: VeeValidateModuleOptions) {
68+
if (isPackageExists('zod') && !isPackageExists('@vee-validate/zod')) {
69+
console.log(
70+
'You seem to be using zod, but you have not installed @vee-validate/zod. Please install it to use zod with vee-validate.'
71+
);
72+
return true;
73+
}
74+
75+
if (isPackageExists('@vee-validate/zod') && !isPackageExists('zod')) {
76+
console.log(
77+
'You seem to be using @vee-validate/zod, but you have not installed zod. Please install it to use zod with vee-validate.'
78+
);
79+
return true;
80+
}
81+
82+
if (isPackageExists('@vee-validate/zod') && isPackageExists('zod')) {
83+
if (options.autoImports) {
84+
addImports({
85+
name: 'toTypedSchema',
86+
as: 'toTypedSchema',
87+
from: '@vee-validate/zod',
88+
});
89+
}
90+
91+
return true;
92+
}
93+
94+
return false;
95+
}
96+
97+
function checkForYup(options: VeeValidateModuleOptions) {
98+
if (isPackageExists('yup') && !isPackageExists('@vee-validate/yup')) {
99+
console.log(
100+
'You seem to be using yup, but you have not installed @vee-validate/yup. Please install it to use yup with vee-validate.'
101+
);
102+
return true;
103+
}
104+
105+
if (isPackageExists('@vee-validate/yup') && !isPackageExists('yup')) {
106+
console.log(
107+
'You seem to be using @vee-validate/yup, but you have not installed yup. Please install it to use yup with vee-validate.'
108+
);
109+
return true;
110+
}
111+
112+
if (isPackageExists('@vee-validate/yup') && isPackageExists('yup')) {
113+
if (options.autoImports) {
114+
addImports({
115+
name: 'toTypedSchema',
116+
as: 'toTypedSchema',
117+
from: '@vee-validate/yup',
118+
});
119+
}
120+
121+
return true;
122+
}
123+
124+
return false;
125+
}

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)