mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-06 15:59:52 +03:00
optimizations
This commit is contained in:
@@ -3,35 +3,57 @@ import type { MqttSettingsType } from 'types';
|
||||
|
||||
import { IP_OR_HOSTNAME_VALIDATOR } from './shared';
|
||||
|
||||
// Constants for validation ranges
|
||||
const PORT_MIN = 0;
|
||||
const PORT_MAX = 65535;
|
||||
const KEEP_ALIVE_MIN = 1;
|
||||
const KEEP_ALIVE_MAX = 86400;
|
||||
const HEARTBEAT_MIN = 10;
|
||||
const HEARTBEAT_MAX = 86400;
|
||||
|
||||
// Reusable validator rules
|
||||
const REQUIRED_HOST_VALIDATOR = [
|
||||
{ required: true, message: 'Host is required' },
|
||||
IP_OR_HOSTNAME_VALIDATOR
|
||||
];
|
||||
|
||||
const REQUIRED_BASE_VALIDATOR = [{ required: true, message: 'Base is required' }];
|
||||
|
||||
const PORT_VALIDATOR = [
|
||||
{ required: true, message: 'Port is required' },
|
||||
{
|
||||
type: 'number' as const,
|
||||
min: PORT_MIN,
|
||||
max: PORT_MAX,
|
||||
message: `Port must be between ${PORT_MIN} and ${PORT_MAX}`
|
||||
}
|
||||
];
|
||||
|
||||
const createNumberValidator = (fieldName: string, min: number, max: number) => [
|
||||
{ required: true, message: `${fieldName} is required` },
|
||||
{
|
||||
type: 'number' as const,
|
||||
min,
|
||||
max,
|
||||
message: `${fieldName} must be between ${min} and ${max}`
|
||||
}
|
||||
];
|
||||
|
||||
export const createMqttSettingsValidator = (mqttSettings: MqttSettingsType) =>
|
||||
new Schema({
|
||||
...(mqttSettings.enabled && {
|
||||
host: [
|
||||
{ required: true, message: 'Host is required' },
|
||||
IP_OR_HOSTNAME_VALIDATOR
|
||||
],
|
||||
base: { required: true, message: 'Base is required' },
|
||||
port: [
|
||||
{ required: true, message: 'Port is required' },
|
||||
{ type: 'number', min: 0, max: 65535, message: 'Invalid Port' }
|
||||
],
|
||||
keep_alive: [
|
||||
{ required: true, message: 'Keep alive is required' },
|
||||
{
|
||||
type: 'number',
|
||||
min: 1,
|
||||
max: 86400,
|
||||
message: 'Keep alive must be between 1 and 86400'
|
||||
}
|
||||
],
|
||||
publish_time_heartbeat: [
|
||||
{ required: true, message: 'Heartbeat is required' },
|
||||
{
|
||||
type: 'number',
|
||||
min: 10,
|
||||
max: 86400,
|
||||
message: 'Heartbeat must be between 10 and 86400'
|
||||
}
|
||||
]
|
||||
host: REQUIRED_HOST_VALIDATOR,
|
||||
base: REQUIRED_BASE_VALIDATOR,
|
||||
port: PORT_VALIDATOR,
|
||||
keep_alive: createNumberValidator(
|
||||
'Keep alive',
|
||||
KEEP_ALIVE_MIN,
|
||||
KEEP_ALIVE_MAX
|
||||
),
|
||||
publish_time_heartbeat: createNumberValidator(
|
||||
'Heartbeat',
|
||||
HEARTBEAT_MIN,
|
||||
HEARTBEAT_MAX
|
||||
)
|
||||
})
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user