Files
pterodactyl-panel/resources/scripts/components/admin/databases/databaseHostForm.ts
T
6c0413e39b Start 2.0 development
Co-authored-by: Anthony <anthony@infraly.co>
Co-authored-by: Sky Mulley <daniel.mulley@outlook.com>
2026-10-05 18:10:18 -04:00

77 lines
2.4 KiB
TypeScript

import type { AdminDatabaseHost } from '@/api/admin/database-hosts/queries';
import { type NumberInputValue, requiredNumber, submittedNumber } from '@/components/admin/numberInput';
export interface DatabaseHostFormValues {
name: string;
host: string;
port: NumberInputValue;
username: string;
password: string;
nodeId: string;
}
type TextValidator = ({ value }: { value: string }) => string | undefined;
export const newDatabaseHostFormValues = (): DatabaseHostFormValues => ({
name: '',
host: '',
port: 3306,
username: '',
password: '',
nodeId: '',
});
export const databaseHostFormValues = (host: AdminDatabaseHost): DatabaseHostFormValues => ({
name: host.attributes.name,
host: host.attributes.host,
port: host.attributes.port,
username: host.attributes.username,
password: '',
nodeId: host.attributes.node_id === null ? '' : String(host.attributes.node_id),
});
const validateName: TextValidator = ({ value }) =>
value.length < 1
? 'A name must be provided.'
: value.length > 191
? 'The name must not exceed 191 characters.'
: undefined;
const validateHost: TextValidator = ({ value }) =>
value.length < 1
? 'A host must be provided.'
: !/^[\w\-.]+$/.test(value)
? 'The host must be a valid hostname or IP address.'
: undefined;
const validateUsername: TextValidator = ({ value }) =>
value.length < 1
? 'A username must be provided.'
: value.length > 32
? 'The username must not exceed 32 characters.'
: undefined;
const validateRequiredPassword: TextValidator = ({ value }) =>
value.length >= 1 ? undefined : 'A password must be provided.';
export const databaseHostValidators = {
name: { onChange: validateName },
host: { onChange: validateHost },
port: {
onChange: requiredNumber('A port must be provided.', (value) =>
value >= 1 && value <= 65535 ? undefined : 'The port must be between 1 and 65535.'
),
},
username: { onChange: validateUsername },
requiredPassword: { onChange: validateRequiredPassword },
};
export const databaseHostBodyFromFormValues = (values: DatabaseHostFormValues) => ({
name: values.name,
host: values.host,
port: submittedNumber(values.port),
username: values.username,
password: values.password || undefined,
node_id: values.nodeId === '' ? null : Number(values.nodeId),
});