-
Notifications
You must be signed in to change notification settings - Fork 0
/
useValidation.ts
37 lines (33 loc) · 1.23 KB
/
useValidation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { ref,reactive } from "vue"
import * as yup from "yup"
export default function useValidation(objectSchema:yup.ObjectShape){
// console.log('schema',objectSchema)
let result:{[key:string]: string}={}
Object.entries(objectSchema).forEach(([configName, config]) => {
result[configName] = '';
});
let values = ref<{[key:string]: string}>(Object.assign({},result))
let errors = ref<{[key:string]: string}>(Object.assign({},result))
const FormSchema = yup.object().shape(objectSchema) as yup.Schema
const validate=async(field: string)=> {
(errors.value as any)[field] = "";
try{
await FormSchema.validateAt(field,values.value);
}catch (e:any){
(errors.value as any)[field] = e.message;
}
}
// // validate all info boxes
const validateAll=async()=> {
try{
await FormSchema.validate(values.value, { abortEarly: false })
return true
}catch (err:any){
err.inner.forEach((error: any) => {
errors.value = { ...errors.value, [error.path]: error.message };
});
return false
}
}
return { values,errors,validate,validateAll }
}