refactor(validation): simplified validation system
This commit is contained in:
		
							parent
							
								
									7570ad9656
								
							
						
					
					
						commit
						77b5b0cab5
					
				| @ -20,15 +20,25 @@ export function isFalsyOrHasThrown(cb: () => ValidatorReturnType): boolean { | |||||||
|   } |   } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | type ValidationAttrs = { | ||||||
|  |   feedback: string; | ||||||
|  |   validationStatus: string | undefined; | ||||||
|  | }; | ||||||
|  | 
 | ||||||
| export function useValidation<T>({ source, rules }: { source: Ref<T>; rules: UseValidationRule<T>[] }) { | export function useValidation<T>({ source, rules }: { source: Ref<T>; rules: UseValidationRule<T>[] }) { | ||||||
|   const state = reactive<{ |   const state = reactive<{ | ||||||
|     message: string; |     message: string; | ||||||
|     status: undefined | 'error'; |     status: undefined | 'error'; | ||||||
|     isValid: boolean; |     isValid: boolean; | ||||||
|  |     attrs: ValidationAttrs; | ||||||
|   }>({ |   }>({ | ||||||
|     message: '', |     message: '', | ||||||
|     status: undefined, |     status: undefined, | ||||||
|     isValid: false, |     isValid: false, | ||||||
|  |     attrs: { | ||||||
|  |       validationStatus: undefined, | ||||||
|  |       feedback: '', | ||||||
|  |     }, | ||||||
|   }); |   }); | ||||||
| 
 | 
 | ||||||
|   watch( |   watch( | ||||||
| @ -45,6 +55,8 @@ export function useValidation<T>({ source, rules }: { source: Ref<T>; rules: Use | |||||||
|       } |       } | ||||||
| 
 | 
 | ||||||
|       state.isValid = state.status !== 'error'; |       state.isValid = state.status !== 'error'; | ||||||
|  |       state.attrs.feedback = state.message; | ||||||
|  |       state.attrs.validationStatus = state.status; | ||||||
|     }, |     }, | ||||||
|     { immediate: true }, |     { immediate: true }, | ||||||
|   ); |   ); | ||||||
|  | |||||||
| @ -20,11 +20,7 @@ | |||||||
|   </n-card> |   </n-card> | ||||||
| 
 | 
 | ||||||
|   <n-card title="Base64 to string"> |   <n-card title="Base64 to string"> | ||||||
|     <n-form-item |     <n-form-item label="Base64 string to decode" v-bind="b64Validation.attrs"> | ||||||
|       label="Base64 string to decode" |  | ||||||
|       :validation-status="b64Validation.status" |  | ||||||
|       :feedback="b64Validation.message" |  | ||||||
|     > |  | ||||||
|       <n-input v-model:value="base64Input" type="textarea" placeholder="Your base64 string..." rows="5" /> |       <n-input v-model:value="base64Input" type="textarea" placeholder="Your base64 string..." rows="5" /> | ||||||
|     </n-form-item> |     </n-form-item> | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user