-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement better input validation with validation scopes
- Loading branch information
1 parent
95e4957
commit b555d97
Showing
8 changed files
with
219 additions
and
90 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<script setup lang="ts"> | ||
import { defineProps } from "vue"; | ||
import { | ||
ExclamationCircleIcon, | ||
ExclamationTriangleIcon, | ||
} from "@heroicons/vue/20/solid"; | ||
import { type ValidationResult } from "@/composables/validation"; | ||
const props = defineProps<ValidationResult>(); | ||
</script> | ||
|
||
<template> | ||
<div | ||
class="feedback-group text-feedback" | ||
:class=" | ||
type === 'error' | ||
? 'text-error' | ||
: type === 'warning' | ||
? 'text-warning' | ||
: 'text-primary' | ||
" | ||
> | ||
<ExclamationCircleIcon | ||
v-if="type === 'error'" | ||
class="size-icon icon-error" | ||
/> | ||
<ExclamationTriangleIcon | ||
v-else-if="type === 'warning'" | ||
class="size-icon icon-warning" | ||
/> | ||
<span v-if="message"> | ||
{{ message }} | ||
</span> | ||
<template v-if="actions !== undefined"> | ||
<template v-for="(action, index) in actions" :key="action.label"> | ||
<span v-if="actions.length > 1 && index === actions.length - 1" | ||
>or</span | ||
> | ||
<span> | ||
<button @click="() => action.callback()" class="underline"> | ||
{{ action.label }} | ||
</button> | ||
{{ actions.length > 1 && index < actions.length - 1 ? "," : "" }} | ||
</span> | ||
</template> | ||
</template> | ||
</div> | ||
</template> | ||
|
||
<style scoped> | ||
@import "@45drives/houston-common-css/src/index.css"; | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import { z } from "zod"; | ||
import { fromError as ZodValidationErrorFromError } from "zod-validation-error"; | ||
import { | ||
onBeforeMount, | ||
onBeforeUnmount, | ||
onMounted, | ||
onUnmounted, | ||
ref, | ||
computed, | ||
type Ref, | ||
watchEffect, | ||
} from "vue"; | ||
|
||
export type ValidationResultAction = { | ||
label: string; | ||
callback: () => void | PromiseLike<void>; | ||
}; | ||
|
||
export type ValidationResult = ( | ||
| { | ||
type: "success"; | ||
message?: string; | ||
} | ||
| { | ||
type: "error"; | ||
message: string; | ||
} | ||
| { | ||
type: "warning"; | ||
message: string; | ||
} | ||
) & { | ||
actions?: ValidationResultAction[]; | ||
}; | ||
|
||
export type Validator = () => ValidationResult | PromiseLike<ValidationResult>; | ||
|
||
export type ValidationScope = Ref<Ref<ValidationResult>[]>; | ||
|
||
const validationScopeStack = ref<ValidationScope[]>([ref([])]); | ||
|
||
const pushValidationScope = (scope: ValidationScope) => { | ||
validationScopeStack.value = [...validationScopeStack.value, scope]; | ||
}; | ||
const removeValidationScope = (scope: ValidationScope) => { | ||
validationScopeStack.value = validationScopeStack.value.filter( | ||
(s) => s !== scope | ||
); | ||
}; | ||
|
||
const getCurrentValidationScope = () => | ||
validationScopeStack.value[validationScopeStack.value.length - 1]; | ||
|
||
export function useValidationScope() { | ||
const scope: ValidationScope = ref([]); | ||
onBeforeMount(() => { | ||
pushValidationScope(scope); | ||
}); | ||
onUnmounted(() => { | ||
removeValidationScope(scope); | ||
}); | ||
const scopeValid = computed<boolean>(() => | ||
scope.value.every((v) => v.value.type !== "error") | ||
); | ||
return { scope, scopeValid }; | ||
} | ||
|
||
export function useValidator(validator: Validator, scope?: ValidationScope) { | ||
const validationResult = ref<ValidationResult>({ | ||
type: "success", | ||
}); | ||
const triggerUpdate = () => { | ||
const result = validator(); | ||
Promise.resolve(result).then( | ||
(result) => | ||
(validationResult.value = { | ||
...result, | ||
actions: result.actions?.map(({ label, callback }) => ({ | ||
label, | ||
callback: () => | ||
Promise.resolve(callback()).then(() => triggerUpdate()), | ||
})), | ||
}) | ||
); | ||
}; | ||
watchEffect(triggerUpdate); | ||
onMounted(() => { | ||
scope ??= getCurrentValidationScope(); | ||
scope.value = [...scope.value, validationResult]; | ||
}); | ||
onBeforeUnmount(() => { | ||
scope ??= getCurrentValidationScope(); | ||
scope.value = scope.value.filter((r) => r !== validationResult); | ||
}); | ||
return { validationResult, triggerUpdate }; | ||
} | ||
|
||
export function useZodValidator<Z extends z.ZodTypeAny = z.ZodNever>( | ||
schema: Z, | ||
getter: () => z.infer<Z>, | ||
scope?: ValidationScope | ||
) { | ||
const validator: Validator = () => | ||
schema | ||
.safeParseAsync(getter()) | ||
.then((result) => | ||
result.success | ||
? validationSuccess() | ||
: validationError(ZodValidationErrorFromError(result.error).message) | ||
); | ||
return useValidator(validator, scope); | ||
} | ||
|
||
export function validationSuccess( | ||
actions?: ValidationResultAction[] | ||
): ValidationResult { | ||
return { | ||
type: "success", | ||
actions, | ||
}; | ||
} | ||
|
||
export function validationWarning( | ||
message: string, | ||
actions?: ValidationResultAction[] | ||
): ValidationResult { | ||
return { | ||
type: "warning", | ||
message, | ||
actions, | ||
}; | ||
} | ||
|
||
export function validationError( | ||
message: string, | ||
actions?: ValidationResultAction[] | ||
): ValidationResult { | ||
return { | ||
type: "error", | ||
message, | ||
actions, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters