refactor DonationBar: enforce build-failing prop validation

- Move validation from prop validators to runtime checks with process.exit(1)
to ensure invalid props crash dev/build processes during SSR rendering.
This commit is contained in:
mahula 2025-11-12 11:07:31 +01:00
parent 3405e46723
commit b48af9b219
2 changed files with 138 additions and 13 deletions

View File

@ -24,16 +24,141 @@ import { usePageLang, useRouteLocale } from "vuepress/client"
const stripSlashes = s => s.replace(/^\/+|\/+$/g, '');
const locale = stripSlashes(useRouteLocale().value) || 'de'
const lang = usePageLang().value || 'de-DE' // Ref<string>, e.g. "de-DE" or "en-US"
const lang = usePageLang().value || 'de-DE'
const props = defineProps({
currentValue: { type: Number, required: true},
target: { type: Number, required: true},
startDate: { type: Date, required: true},
endDate: { type: Date, required: true},
asOfDate: { type: Date, required: true},
const props = defineProps({
currentValue: {
type: Number,
required: true
},
target: {
type: Number,
required: true
},
startDate: {
type: String,
required: true
},
endDate: {
type: String,
required: true
},
asOfDate: {
type: String,
required: true
},
})
if (typeof props.currentValue !== 'number') {
const error = `[DonationBar] Prop "currentValue" must be a number, received: ${typeof props.currentValue} (${props.currentValue})`
console.error(error)
if (typeof process !== 'undefined') process.exit(1)
throw new Error(error)
}
if (isNaN(props.currentValue)) {
const error = `[DonationBar] Prop "currentValue" must be a valid number, received: NaN`
console.error(error)
if (typeof process !== 'undefined') process.exit(1)
throw new Error(error)
}
if (!isFinite(props.currentValue)) {
const error = `[DonationBar] Prop "currentValue" must be a finite number, received: ${props.currentValue}`
console.error(error)
if (typeof process !== 'undefined') process.exit(1)
throw new Error(error)
}
if (props.currentValue < 0) {
const error = `[DonationBar] Prop "currentValue" must be >= 0, received: ${props.currentValue}`
console.error(error)
if (typeof process !== 'undefined') process.exit(1)
throw new Error(error)
}
if (typeof props.target !== 'number') {
const error = `[DonationBar] Prop "target" must be a number, received: ${typeof props.target} (${props.target})`
console.error(error)
if (typeof process !== 'undefined') process.exit(1)
throw new Error(error)
}
if (isNaN(props.target)) {
const error = `[DonationBar] Prop "target" must be a valid number, received: NaN`
console.error(error)
if (typeof process !== 'undefined') process.exit(1)
throw new Error(error)
}
if (!isFinite(props.target)) {
const error = `[DonationBar] Prop "target" must be a finite number, received: ${props.target}`
console.error(error)
if (typeof process !== 'undefined') process.exit(1)
throw new Error(error)
}
if (props.target <= 0) {
const error = `[DonationBar] Prop "target" must be > 0, received: ${props.target}`
console.error(error)
if (typeof process !== 'undefined') process.exit(1)
throw new Error(error)
}
if (typeof props.startDate !== 'string') {
const error = `[DonationBar] Prop "startDate" must be a string, received: ${typeof props.startDate}`
console.error(error)
if (typeof process !== 'undefined') process.exit(1)
throw new Error(error)
}
const startDateRegex = /^\d{4}-\d{2}-\d{2}$/
if (!startDateRegex.test(props.startDate)) {
const error = `[DonationBar] Prop "startDate" must be in ISO 8601 format (YYYY-MM-DD), received: ${props.startDate}`
console.error(error)
if (typeof process !== 'undefined') process.exit(1)
throw new Error(error)
}
if (isNaN(new Date(props.startDate).getTime())) {
const error = `[DonationBar] Prop "startDate" has invalid date value: ${props.startDate}`
console.error(error)
if (typeof process !== 'undefined') process.exit(1)
throw new Error(error)
}
if (typeof props.endDate !== 'string') {
const error = `[DonationBar] Prop "endDate" must be a string, received: ${typeof props.endDate}`
console.error(error)
if (typeof process !== 'undefined') process.exit(1)
throw new Error(error)
}
const endDateRegex = /^\d{4}-\d{2}-\d{2}$/
if (!endDateRegex.test(props.endDate)) {
const error = `[DonationBar] Prop "endDate" must be in ISO 8601 format (YYYY-MM-DD), received: ${props.endDate}`
console.error(error)
if (typeof process !== 'undefined') process.exit(1)
throw new Error(error)
}
if (isNaN(new Date(props.endDate).getTime())) {
const error = `[DonationBar] Prop "endDate" has invalid date value: ${props.endDate}`
console.error(error)
if (typeof process !== 'undefined') process.exit(1)
throw new Error(error)
}
if (typeof props.asOfDate !== 'string') {
const error = `[DonationBar] Prop "asOfDate" must be a string, received: ${typeof props.asOfDate}`
console.error(error)
if (typeof process !== 'undefined') process.exit(1)
throw new Error(error)
}
const asOfDateRegex = /^\d{4}-\d{2}-\d{2}$/
if (!asOfDateRegex.test(props.asOfDate)) {
const error = `[DonationBar] Prop "asOfDate" must be in ISO 8601 format (YYYY-MM-DD), received: ${props.asOfDate}`
console.error(error)
if (typeof process !== 'undefined') process.exit(1)
throw new Error(error)
}
if (isNaN(new Date(props.asOfDate).getTime())) {
const error = `[DonationBar] Prop "asOfDate" has invalid date value: ${props.asOfDate}`
console.error(error)
if (typeof process !== 'undefined') process.exit(1)
throw new Error(error)
}
const title = computed(() => {
switch (locale) {
case 'de':

View File

@ -25,9 +25,9 @@ Hilf mit deiner Spende, dass Beiträge in Gruppen angepinnt werden können.
<DonationBar
:currentValue="180"
:target="1200"
:startDate="new Date('2025-11-05')"
:endDate="new Date('2026-01-02')"
:asOfDate="new Date('2025-11-10')"
startDate="2025-11-05"
endDate="2026-01-02"
asOfDate="2025-11-10"
/>
### Worum geht es
@ -60,9 +60,9 @@ Also auch über eine Spende von dir.
<DonationBar
:currentValue="180"
:target="1200"
:startDate="new Date('2025-11-05')"
:endDate="new Date('2026-01-02')"
:asOfDate="new Date('2025-11-10')"
startDate="2025-11-05"
endDate="2026-01-02"
asOfDate="2025-11-10"
/>
### Spenden