11import { defineNuxtModule , addComponent , addImports } from '@nuxt/kit' ;
22import type { NuxtModule } from '@nuxt/schema' ;
3+ import { isPackageExists } from 'local-pkg' ;
4+
5+ interface VeeValidateModuleOptions {
6+ autoImports ?: boolean ;
7+ }
38
49const 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+ }
0 commit comments