refactor(validation): simplified validation management with helpers
This commit is contained in:
		
							parent
							
								
									b38ab82d05
								
							
						
					
					
						commit
						f54223fb0a
					
				| @ -60,6 +60,7 @@ | ||||
| <script setup lang="ts"> | ||||
| import { useCopy } from '@/composable/copy'; | ||||
| import { useValidation } from '@/composable/validation'; | ||||
| import { isNotThrowing } from '@/utils/boolean'; | ||||
| import { withDefaultOnError } from '@/utils/defaults'; | ||||
| import { | ||||
|   chineseSimplifiedWordList, | ||||
| @ -98,11 +99,7 @@ const passphraseInput = ref(''); | ||||
| const language = ref<keyof typeof languages>('English'); | ||||
| const passphrase = computed({ | ||||
|   get() { | ||||
|     try { | ||||
|       return entropyToMnemonic(entropy.value, languages[language.value]); | ||||
|     } catch (_) { | ||||
|       return passphraseInput.value; | ||||
|     } | ||||
|     return withDefaultOnError(() => entropyToMnemonic(entropy.value, languages[language.value]), passphraseInput.value); | ||||
|   }, | ||||
|   set(value: string) { | ||||
|     passphraseInput.value = value; | ||||
| @ -128,14 +125,7 @@ const mnemonicValidation = useValidation({ | ||||
|   source: passphrase, | ||||
|   rules: [ | ||||
|     { | ||||
|       validator: (value) => { | ||||
|         try { | ||||
|           mnemonicToEntropy(value, languages[language.value]); | ||||
|           return true; | ||||
|         } catch (_) { | ||||
|           return false; | ||||
|         } | ||||
|       }, | ||||
|       validator: (value) => isNotThrowing(() => mnemonicToEntropy(value, languages[language.value])), | ||||
|       message: 'Invalid mnemonic', | ||||
|     }, | ||||
|   ], | ||||
|  | ||||
| @ -60,6 +60,7 @@ | ||||
| <script setup lang="ts"> | ||||
| import { useCopy } from '@/composable/copy'; | ||||
| import { useValidation } from '@/composable/validation'; | ||||
| import { isNotThrowing } from '@/utils/boolean'; | ||||
| import { withDefaultOnError } from '@/utils/defaults'; | ||||
| import { computed, ref } from 'vue'; | ||||
| 
 | ||||
| @ -70,14 +71,7 @@ const encodedValidation = useValidation({ | ||||
|   source: encodeInput, | ||||
|   rules: [ | ||||
|     { | ||||
|       validator: (value) => { | ||||
|         try { | ||||
|           encodeURIComponent(value); | ||||
|           return true; | ||||
|         } catch (_) { | ||||
|           return false; | ||||
|         } | ||||
|       }, | ||||
|       validator: (value) => isNotThrowing(() => encodeURIComponent(value)), | ||||
|       message: 'Impossible to parse this string', | ||||
|     }, | ||||
|   ], | ||||
| @ -92,14 +86,7 @@ const decodeValidation = useValidation({ | ||||
|   source: encodeInput, | ||||
|   rules: [ | ||||
|     { | ||||
|       validator: (value) => { | ||||
|         try { | ||||
|           decodeURIComponent(value); | ||||
|           return true; | ||||
|         } catch (_) { | ||||
|           return false; | ||||
|         } | ||||
|       }, | ||||
|       validator: (value) => isNotThrowing(() => decodeURIComponent(value)), | ||||
|       message: 'Impossible to parse this string', | ||||
|     }, | ||||
|   ], | ||||
|  | ||||
| @ -27,10 +27,11 @@ | ||||
| </template> | ||||
| 
 | ||||
| <script setup lang="ts"> | ||||
| import { computed, ref } from 'vue'; | ||||
| import { SubdirectoryArrowRightRound } from '@vicons/material'; | ||||
| import { useValidation } from '@/composable/validation'; | ||||
| import { isNotThrowing } from '@/utils/boolean'; | ||||
| import { withDefaultOnError } from '@/utils/defaults'; | ||||
| import { SubdirectoryArrowRightRound } from '@vicons/material'; | ||||
| import { computed, ref } from 'vue'; | ||||
| import InputCopyable from '../../components/InputCopyable.vue'; | ||||
| 
 | ||||
| const urlToParse = ref('https://me:pwd@it-tools.tech:3000/url-parser?key1=value&key2=value2#the-hash'); | ||||
| @ -40,14 +41,7 @@ const validation = useValidation({ | ||||
|   source: urlToParse, | ||||
|   rules: [ | ||||
|     { | ||||
|       validator: (value) => { | ||||
|         try { | ||||
|           new URL(value); | ||||
|           return true; | ||||
|         } catch (_) { | ||||
|           return false; | ||||
|         } | ||||
|       }, | ||||
|       validator: (value) => isNotThrowing(() => new URL(value)), | ||||
|       message: 'Invalid url', | ||||
|     }, | ||||
|   ], | ||||
|  | ||||
							
								
								
									
										15
									
								
								src/utils/boolean.test.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								src/utils/boolean.test.ts
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,15 @@ | ||||
| import { describe, expect, it } from 'vitest'; | ||||
| import { isNotThrowing } from './boolean'; | ||||
| 
 | ||||
| describe('boolean utils', () => { | ||||
|   describe('isNotThrowing', () => { | ||||
|     it('should return if the call throws or false otherwise', () => { | ||||
|       expect(isNotThrowing(() => {})).to.eql(true); | ||||
|       expect( | ||||
|         isNotThrowing(() => { | ||||
|           throw new Error(); | ||||
|         }), | ||||
|       ).to.eql(false); | ||||
|     }); | ||||
|   }); | ||||
| }); | ||||
							
								
								
									
										10
									
								
								src/utils/boolean.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								src/utils/boolean.ts
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,10 @@ | ||||
| export { isNotThrowing }; | ||||
| 
 | ||||
| function isNotThrowing(cb: () => unknown): boolean { | ||||
|   try { | ||||
|     cb(); | ||||
|     return true; | ||||
|   } catch (_) { | ||||
|     return false; | ||||
|   } | ||||
| } | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user