fix: correctly validate number inputs against null/NaN bypass in admin modals
This commit is contained in:
@@ -50,11 +50,31 @@ import { Button } from '$lib/components/ui/button';
|
||||
}
|
||||
|
||||
function validateNoticeDurationField() {
|
||||
formErrors.notice_duration = (formData.notice_duration_hours < 0 || isNaN(formData.notice_duration_hours)) ? 'Must be a positive number' : '';
|
||||
const val = formData.notice_duration_hours;
|
||||
if (val === null || val === undefined || val === '') {
|
||||
formErrors.notice_duration = 'Must be a positive number';
|
||||
return;
|
||||
}
|
||||
const num = Number(val);
|
||||
if (isNaN(num) || num < 0 || !Number.isInteger(num)) {
|
||||
formErrors.notice_duration = 'Must be a positive whole number';
|
||||
} else {
|
||||
formErrors.notice_duration = '';
|
||||
}
|
||||
}
|
||||
|
||||
function validateExpiryField() {
|
||||
formErrors.expiry = (formData.expiry_months < 1 || isNaN(formData.expiry_months)) ? 'Must be at least 1 month' : '';
|
||||
const val = formData.expiry_months;
|
||||
if (val === null || val === undefined || val === '') {
|
||||
formErrors.expiry = 'Must be at least 1 month';
|
||||
return;
|
||||
}
|
||||
const num = Number(val);
|
||||
if (isNaN(num) || num < 1 || !Number.isInteger(num)) {
|
||||
formErrors.expiry = 'Must be a whole number of at least 1 month';
|
||||
} else {
|
||||
formErrors.expiry = '';
|
||||
}
|
||||
}
|
||||
|
||||
function validateAllFields() {
|
||||
@@ -68,10 +88,14 @@ import { Button } from '$lib/components/ui/button';
|
||||
!formErrors.name &&
|
||||
!formErrors.notice_duration &&
|
||||
!formErrors.expiry &&
|
||||
formData.notice_duration_hours >= 0 &&
|
||||
!isNaN(formData.notice_duration_hours) &&
|
||||
formData.expiry_months >= 1 &&
|
||||
!isNaN(formData.expiry_months)
|
||||
formData.notice_duration_hours !== null &&
|
||||
formData.notice_duration_hours !== undefined &&
|
||||
formData.notice_duration_hours !== '' &&
|
||||
Number(formData.notice_duration_hours) >= 0 &&
|
||||
formData.expiry_months !== null &&
|
||||
formData.expiry_months !== undefined &&
|
||||
formData.expiry_months !== '' &&
|
||||
Number(formData.expiry_months) >= 1
|
||||
);
|
||||
|
||||
async function fetchData() {
|
||||
|
||||
@@ -40,6 +40,9 @@
|
||||
let serviceErrors = $state<Record<string, string>>({});
|
||||
|
||||
function validatePrice(price: any): string {
|
||||
if (price === null || price === undefined || price === '') {
|
||||
return 'Price is required';
|
||||
}
|
||||
const numPrice = parseFloat(price);
|
||||
if (isNaN(numPrice)) {
|
||||
return 'Price must be a valid number';
|
||||
@@ -57,20 +60,24 @@
|
||||
return '';
|
||||
}
|
||||
|
||||
function validateDuration(value: number, field: string): string {
|
||||
if (isNaN(value)) {
|
||||
function validateDuration(value: any, field: string): string {
|
||||
if (value === null || value === undefined || value === '') {
|
||||
return 'Field cannot be empty';
|
||||
}
|
||||
const num = Number(value);
|
||||
if (isNaN(num)) {
|
||||
return 'Must be a valid number';
|
||||
}
|
||||
|
||||
if (!Number.isInteger(value)) {
|
||||
if (!Number.isInteger(num)) {
|
||||
return 'Must be a whole number';
|
||||
}
|
||||
|
||||
if (field === 'duration_minutes' && value <= 0) {
|
||||
if (field === 'duration_minutes' && num <= 0) {
|
||||
return 'Duration must be greater than 0';
|
||||
}
|
||||
|
||||
if (field === 'minimum_age_required' && (value < 0 || value > 100)) {
|
||||
if (field === 'minimum_age_required' && (num < 0 || num > 100)) {
|
||||
return 'Must be between 0 and 100';
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user