mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-06 07:49:52 +03:00
optimizations
This commit is contained in:
@@ -11,273 +11,210 @@ import type {
|
||||
TemperatureSensor
|
||||
} from './types';
|
||||
|
||||
export const GPIO_VALIDATOR = {
|
||||
// Helper to create GPIO validator from invalid ranges
|
||||
const createGPIOValidator = (
|
||||
invalidRanges: Array<number | [number, number]>,
|
||||
maxValue: number
|
||||
) => ({
|
||||
validator(
|
||||
_rule: InternalRuleItem,
|
||||
value: number,
|
||||
callback: (error?: string) => void
|
||||
) {
|
||||
if (
|
||||
value &&
|
||||
(value === 1 ||
|
||||
(value >= 6 && value <= 11) ||
|
||||
value === 20 ||
|
||||
value === 24 ||
|
||||
(value >= 28 && value <= 31) ||
|
||||
value > 40 ||
|
||||
value < 0)
|
||||
) {
|
||||
callback('Must be an valid GPIO port');
|
||||
} else {
|
||||
if (!value) {
|
||||
callback();
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const GPIO_VALIDATORR = {
|
||||
validator(
|
||||
_rule: InternalRuleItem,
|
||||
value: number,
|
||||
callback: (error?: string) => void
|
||||
if (value < 0 || value > maxValue) {
|
||||
callback('Must be an valid GPIO port');
|
||||
return;
|
||||
}
|
||||
|
||||
for (const range of invalidRanges) {
|
||||
if (typeof range === 'number') {
|
||||
if (value === range) {
|
||||
callback('Must be an valid GPIO port');
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
const [start, end] = range;
|
||||
if (value >= start && value <= end) {
|
||||
callback('Must be an valid GPIO port');
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
callback();
|
||||
}
|
||||
});
|
||||
|
||||
export const GPIO_VALIDATOR = createGPIOValidator(
|
||||
[[6, 11], 1, 20, 24, [28, 31]],
|
||||
40
|
||||
);
|
||||
|
||||
export const GPIO_VALIDATORC3 = createGPIOValidator([[11, 19]], 21);
|
||||
|
||||
export const GPIO_VALIDATORS2 = createGPIOValidator(
|
||||
[
|
||||
[19, 20],
|
||||
[22, 32]
|
||||
],
|
||||
40
|
||||
);
|
||||
|
||||
export const GPIO_VALIDATORS3 = createGPIOValidator(
|
||||
[
|
||||
[19, 20],
|
||||
[22, 37],
|
||||
[39, 42]
|
||||
],
|
||||
48
|
||||
);
|
||||
|
||||
const GPIO_FIELD_NAMES = [
|
||||
'led_gpio',
|
||||
'dallas_gpio',
|
||||
'pbutton_gpio',
|
||||
'tx_gpio',
|
||||
'rx_gpio'
|
||||
] as const;
|
||||
|
||||
type ValidationRules = Array<{
|
||||
required?: boolean;
|
||||
message?: string;
|
||||
[key: string]: unknown;
|
||||
}>;
|
||||
|
||||
const createGPIOValidations = (
|
||||
validator: typeof GPIO_VALIDATOR
|
||||
): Record<string, ValidationRules> =>
|
||||
GPIO_FIELD_NAMES.reduce(
|
||||
(acc, field) => {
|
||||
const fieldName = field.replace('_gpio', '');
|
||||
acc[field] = [
|
||||
{ required: true, message: `${fieldName.toUpperCase()} GPIO is required` },
|
||||
validator
|
||||
];
|
||||
return acc;
|
||||
},
|
||||
{} as Record<string, ValidationRules>
|
||||
);
|
||||
|
||||
const PLATFORM_VALIDATORS = {
|
||||
ESP32: GPIO_VALIDATOR,
|
||||
ESP32C3: GPIO_VALIDATORC3,
|
||||
ESP32S2: GPIO_VALIDATORS2,
|
||||
ESP32S3: GPIO_VALIDATORS3
|
||||
} as const;
|
||||
|
||||
export const createSettingsValidator = (settings: Settings) => {
|
||||
const schema: Record<string, ValidationRules> = {};
|
||||
|
||||
// Add GPIO validations for CUSTOM board profiles
|
||||
if (
|
||||
settings.board_profile === 'CUSTOM' &&
|
||||
settings.platform in PLATFORM_VALIDATORS
|
||||
) {
|
||||
if (
|
||||
value &&
|
||||
(value === 1 ||
|
||||
(value >= 6 && value <= 11) ||
|
||||
(value >= 16 && value <= 17) ||
|
||||
value === 20 ||
|
||||
value === 24 ||
|
||||
(value >= 28 && value <= 31) ||
|
||||
value > 40 ||
|
||||
value < 0)
|
||||
) {
|
||||
callback('Must be an valid GPIO port');
|
||||
} else {
|
||||
callback();
|
||||
}
|
||||
Object.assign(
|
||||
schema,
|
||||
createGPIOValidations(
|
||||
PLATFORM_VALIDATORS[settings.platform as keyof typeof PLATFORM_VALIDATORS]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// Syslog validations
|
||||
if (settings.syslog_enabled) {
|
||||
schema.syslog_host = [
|
||||
{ required: true, message: 'Host is required' },
|
||||
IP_OR_HOSTNAME_VALIDATOR
|
||||
];
|
||||
schema.syslog_port = [
|
||||
{ required: true, message: 'Port is required' },
|
||||
{ type: 'number', min: 0, max: 65535, message: 'Invalid Port' }
|
||||
];
|
||||
schema.syslog_mark_interval = [
|
||||
{ required: true, message: 'Mark interval is required' },
|
||||
{ type: 'number', min: 0, max: 10, message: 'Must be between 0 and 10' }
|
||||
];
|
||||
}
|
||||
|
||||
// Modbus validations
|
||||
if (settings.modbus_enabled) {
|
||||
schema.modbus_max_clients = [
|
||||
{ required: true, message: 'Max clients is required' },
|
||||
{ type: 'number', min: 0, max: 50, message: 'Invalid number' }
|
||||
];
|
||||
schema.modbus_port = [
|
||||
{ required: true, message: 'Port is required' },
|
||||
{ type: 'number', min: 0, max: 65535, message: 'Invalid Port' }
|
||||
];
|
||||
schema.modbus_timeout = [
|
||||
{ required: true, message: 'Timeout is required' },
|
||||
{
|
||||
type: 'number',
|
||||
min: 100,
|
||||
max: 20000,
|
||||
message: 'Must be between 100 and 20000'
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
// Shower timer validations
|
||||
if (settings.shower_timer) {
|
||||
schema.shower_min_duration = [
|
||||
{
|
||||
type: 'number',
|
||||
min: 10,
|
||||
max: 360,
|
||||
message: 'Time must be between 10 and 360 seconds'
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
// Shower alert validations
|
||||
if (settings.shower_alert) {
|
||||
schema.shower_alert_trigger = [
|
||||
{
|
||||
type: 'number',
|
||||
min: 1,
|
||||
max: 20,
|
||||
message: 'Time must be between 1 and 20 minutes'
|
||||
}
|
||||
];
|
||||
schema.shower_alert_coldshot = [
|
||||
{
|
||||
type: 'number',
|
||||
min: 1,
|
||||
max: 10,
|
||||
message: 'Time must be between 1 and 10 seconds'
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
// Remote timeout validations
|
||||
if (settings.remote_timeout_en) {
|
||||
schema.remote_timeout = [
|
||||
{
|
||||
type: 'number',
|
||||
min: 1,
|
||||
max: 240,
|
||||
message: 'Timeout must be between 1 and 240 hours'
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
return new Schema(schema);
|
||||
};
|
||||
|
||||
export const GPIO_VALIDATORC3 = {
|
||||
validator(
|
||||
_rule: InternalRuleItem,
|
||||
value: number,
|
||||
callback: (error?: string) => void
|
||||
) {
|
||||
if (value && ((value >= 11 && value <= 19) || value > 21 || value < 0)) {
|
||||
callback('Must be an valid GPIO port');
|
||||
} else {
|
||||
callback();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const GPIO_VALIDATORS2 = {
|
||||
validator(
|
||||
_rule: InternalRuleItem,
|
||||
value: number,
|
||||
callback: (error?: string) => void
|
||||
) {
|
||||
if (
|
||||
value &&
|
||||
((value >= 19 && value <= 20) ||
|
||||
(value >= 22 && value <= 32) ||
|
||||
value > 40 ||
|
||||
value < 0)
|
||||
) {
|
||||
callback('Must be an valid GPIO port');
|
||||
} else {
|
||||
callback();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const GPIO_VALIDATORS3 = {
|
||||
validator(
|
||||
_rule: InternalRuleItem,
|
||||
value: number,
|
||||
callback: (error?: string) => void
|
||||
) {
|
||||
if (
|
||||
value &&
|
||||
((value >= 19 && value <= 20) ||
|
||||
(value >= 22 && value <= 37) ||
|
||||
(value >= 39 && value <= 42) ||
|
||||
value > 48 ||
|
||||
value < 0)
|
||||
) {
|
||||
callback('Must be an valid GPIO port');
|
||||
} else {
|
||||
callback();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const createSettingsValidator = (settings: Settings) =>
|
||||
new Schema({
|
||||
...(settings.board_profile === 'CUSTOM' &&
|
||||
settings.platform === 'ESP32' && {
|
||||
led_gpio: [
|
||||
{ required: true, message: 'LED GPIO is required' },
|
||||
GPIO_VALIDATOR
|
||||
],
|
||||
dallas_gpio: [
|
||||
{ required: true, message: 'GPIO is required' },
|
||||
GPIO_VALIDATOR
|
||||
],
|
||||
pbutton_gpio: [
|
||||
{ required: true, message: 'Button GPIO is required' },
|
||||
GPIO_VALIDATOR
|
||||
],
|
||||
tx_gpio: [
|
||||
{ required: true, message: 'Tx GPIO is required' },
|
||||
GPIO_VALIDATOR
|
||||
],
|
||||
rx_gpio: [{ required: true, message: 'Rx GPIO is required' }, GPIO_VALIDATOR]
|
||||
}),
|
||||
...(settings.board_profile === 'CUSTOM' &&
|
||||
settings.platform === 'ESP32C3' && {
|
||||
led_gpio: [
|
||||
{ required: true, message: 'LED GPIO is required' },
|
||||
GPIO_VALIDATORC3
|
||||
],
|
||||
dallas_gpio: [
|
||||
{ required: true, message: 'GPIO is required' },
|
||||
GPIO_VALIDATORC3
|
||||
],
|
||||
pbutton_gpio: [
|
||||
{ required: true, message: 'Button GPIO is required' },
|
||||
GPIO_VALIDATORC3
|
||||
],
|
||||
tx_gpio: [
|
||||
{ required: true, message: 'Tx GPIO is required' },
|
||||
GPIO_VALIDATORC3
|
||||
],
|
||||
rx_gpio: [
|
||||
{ required: true, message: 'Rx GPIO is required' },
|
||||
GPIO_VALIDATORC3
|
||||
]
|
||||
}),
|
||||
...(settings.board_profile === 'CUSTOM' &&
|
||||
settings.platform === 'ESP32S2' && {
|
||||
led_gpio: [
|
||||
{ required: true, message: 'LED GPIO is required' },
|
||||
GPIO_VALIDATORS2
|
||||
],
|
||||
dallas_gpio: [
|
||||
{ required: true, message: 'GPIO is required' },
|
||||
GPIO_VALIDATORS2
|
||||
],
|
||||
pbutton_gpio: [
|
||||
{ required: true, message: 'Button GPIO is required' },
|
||||
GPIO_VALIDATORS2
|
||||
],
|
||||
tx_gpio: [
|
||||
{ required: true, message: 'Tx GPIO is required' },
|
||||
GPIO_VALIDATORS2
|
||||
],
|
||||
rx_gpio: [
|
||||
{ required: true, message: 'Rx GPIO is required' },
|
||||
GPIO_VALIDATORS2
|
||||
]
|
||||
}),
|
||||
...(settings.board_profile === 'CUSTOM' &&
|
||||
settings.platform === 'ESP32S3' && {
|
||||
led_gpio: [
|
||||
{ required: true, message: 'LED GPIO is required' },
|
||||
GPIO_VALIDATORS3
|
||||
],
|
||||
dallas_gpio: [
|
||||
{ required: true, message: 'GPIO is required' },
|
||||
GPIO_VALIDATORS3
|
||||
],
|
||||
pbutton_gpio: [
|
||||
{ required: true, message: 'Button GPIO is required' },
|
||||
GPIO_VALIDATORS3
|
||||
],
|
||||
tx_gpio: [
|
||||
{ required: true, message: 'Tx GPIO is required' },
|
||||
GPIO_VALIDATORS3
|
||||
],
|
||||
rx_gpio: [
|
||||
{ required: true, message: 'Rx GPIO is required' },
|
||||
GPIO_VALIDATORS3
|
||||
]
|
||||
}),
|
||||
...(settings.syslog_enabled && {
|
||||
syslog_host: [
|
||||
{ required: true, message: 'Host is required' },
|
||||
IP_OR_HOSTNAME_VALIDATOR
|
||||
],
|
||||
syslog_port: [
|
||||
{ required: true, message: 'Port is required' },
|
||||
{ type: 'number', min: 0, max: 65535, message: 'Invalid Port' }
|
||||
],
|
||||
syslog_mark_interval: [
|
||||
{ required: true, message: 'Mark interval is required' },
|
||||
{ type: 'number', min: 0, max: 10, message: 'Must be between 0 and 10' }
|
||||
]
|
||||
}),
|
||||
...(settings.modbus_enabled && {
|
||||
modbus_max_clients: [
|
||||
{ required: true, message: 'Max clients is required' },
|
||||
{ type: 'number', min: 0, max: 50, message: 'Invalid number' }
|
||||
],
|
||||
modbus_port: [
|
||||
{ required: true, message: 'Port is required' },
|
||||
{ type: 'number', min: 0, max: 65535, message: 'Invalid Port' }
|
||||
],
|
||||
modbus_timeout: [
|
||||
{ required: true, message: 'Timeout is required' },
|
||||
{
|
||||
type: 'number',
|
||||
min: 100,
|
||||
max: 20000,
|
||||
message: 'Must be between 100 and 20000'
|
||||
}
|
||||
]
|
||||
}),
|
||||
...(settings.shower_timer && {
|
||||
shower_min_duration: [
|
||||
{
|
||||
type: 'number',
|
||||
min: 10,
|
||||
max: 360,
|
||||
message: 'Time must be between 10 and 360 seconds'
|
||||
}
|
||||
]
|
||||
}),
|
||||
...(settings.shower_alert && {
|
||||
shower_alert_trigger: [
|
||||
{
|
||||
type: 'number',
|
||||
min: 1,
|
||||
max: 20,
|
||||
message: 'Time must be between 1 and 20 minutes'
|
||||
}
|
||||
],
|
||||
shower_alert_coldshot: [
|
||||
{
|
||||
type: 'number',
|
||||
min: 1,
|
||||
max: 10,
|
||||
message: 'Time must be between 1 and 10 seconds'
|
||||
}
|
||||
]
|
||||
}),
|
||||
...(settings.remote_timeout_en && {
|
||||
remote_timeout: [
|
||||
{
|
||||
type: 'number',
|
||||
min: 1,
|
||||
max: 240,
|
||||
message: 'Timeout must be between 1 and 240 hours'
|
||||
}
|
||||
]
|
||||
})
|
||||
});
|
||||
|
||||
export const uniqueNameValidator = (schedule: ScheduleItem[], o_name?: string) => ({
|
||||
// Generic unique name validator factory
|
||||
const createUniqueNameValidator = <T extends { name: string }>(
|
||||
items: T[],
|
||||
originalName?: string
|
||||
) => ({
|
||||
validator(
|
||||
_rule: InternalRuleItem,
|
||||
name: string,
|
||||
@@ -285,8 +222,9 @@ export const uniqueNameValidator = (schedule: ScheduleItem[], o_name?: string) =
|
||||
) {
|
||||
if (
|
||||
name !== '' &&
|
||||
(o_name === undefined || o_name.toLowerCase() !== name.toLowerCase()) &&
|
||||
schedule.find((si) => si.name.toLowerCase() === name.toLowerCase())
|
||||
(originalName === undefined ||
|
||||
originalName.toLowerCase() !== name.toLowerCase()) &&
|
||||
items.find((item) => item.name.toLowerCase() === name.toLowerCase())
|
||||
) {
|
||||
callback('Name already in use');
|
||||
} else {
|
||||
@@ -295,19 +233,51 @@ export const uniqueNameValidator = (schedule: ScheduleItem[], o_name?: string) =
|
||||
}
|
||||
});
|
||||
|
||||
// Generic field name validator (for cases where the name field has different property names)
|
||||
const createUniqueFieldNameValidator = <T>(
|
||||
items: T[],
|
||||
getName: (item: T) => string,
|
||||
originalName?: string
|
||||
) => ({
|
||||
validator(
|
||||
_rule: InternalRuleItem,
|
||||
name: string,
|
||||
callback: (error?: string) => void
|
||||
) {
|
||||
if (
|
||||
name !== '' &&
|
||||
(originalName === undefined ||
|
||||
originalName.toLowerCase() !== name.toLowerCase()) &&
|
||||
items.find((item) => getName(item).toLowerCase() === name.toLowerCase())
|
||||
) {
|
||||
callback('Name already in use');
|
||||
} else {
|
||||
callback();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const NAME_PATTERN = {
|
||||
type: 'string' as const,
|
||||
pattern: /^[a-zA-Z0-9_]{0,19}$/,
|
||||
message: "Must be <20 characters: alphanumeric or '_'"
|
||||
};
|
||||
|
||||
const NAME_PATTERN_REQUIRED = {
|
||||
type: 'string' as const,
|
||||
pattern: /^[a-zA-Z0-9_]{1,19}$/,
|
||||
message: "Must be <20 characters: alphanumeric or '_'"
|
||||
};
|
||||
|
||||
export const uniqueNameValidator = (schedule: ScheduleItem[], o_name?: string) =>
|
||||
createUniqueNameValidator(schedule, o_name);
|
||||
|
||||
export const schedulerItemValidation = (
|
||||
schedule: ScheduleItem[],
|
||||
scheduleItem: ScheduleItem
|
||||
) =>
|
||||
new Schema({
|
||||
name: [
|
||||
{
|
||||
type: 'string',
|
||||
pattern: /^[a-zA-Z0-9_]{0,19}$/,
|
||||
message: "Must be <20 characters: alphanumeric or '_'"
|
||||
},
|
||||
...[uniqueNameValidator(schedule, scheduleItem.o_name)]
|
||||
],
|
||||
name: [NAME_PATTERN, uniqueNameValidator(schedule, scheduleItem.o_name)],
|
||||
cmd: [
|
||||
{ required: true, message: 'Command is required' },
|
||||
{
|
||||
@@ -319,65 +289,32 @@ export const schedulerItemValidation = (
|
||||
]
|
||||
});
|
||||
|
||||
export const uniqueCustomNameValidator = (
|
||||
entity: EntityItem[],
|
||||
o_name?: string
|
||||
) => ({
|
||||
export const uniqueCustomNameValidator = (entity: EntityItem[], o_name?: string) =>
|
||||
createUniqueNameValidator(entity, o_name);
|
||||
|
||||
const hexValidator = {
|
||||
validator(
|
||||
_rule: InternalRuleItem,
|
||||
name: string,
|
||||
value: string,
|
||||
callback: (error?: string) => void
|
||||
) {
|
||||
if (
|
||||
(o_name === undefined || o_name.toLowerCase() !== name.toLowerCase()) &&
|
||||
entity.find((ei) => ei.name.toLowerCase() === name.toLowerCase())
|
||||
) {
|
||||
callback('Name already in use');
|
||||
if (!value || isNaN(parseInt(value, 16))) {
|
||||
callback('Is required and must be in hex format');
|
||||
} else {
|
||||
callback();
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
export const entityItemValidation = (entity: EntityItem[], entityItem: EntityItem) =>
|
||||
new Schema({
|
||||
name: [
|
||||
{ required: true, message: 'Name is required' },
|
||||
{
|
||||
type: 'string',
|
||||
pattern: /^[a-zA-Z0-9_]{1,19}$/,
|
||||
message: "Must be <20 characters: alphanumeric or '_'"
|
||||
},
|
||||
...[uniqueCustomNameValidator(entity, entityItem.o_name)]
|
||||
],
|
||||
device_id: [
|
||||
{
|
||||
validator(
|
||||
_rule: InternalRuleItem,
|
||||
value: string,
|
||||
callback: (error?: string) => void
|
||||
) {
|
||||
if (isNaN(parseInt(value, 16))) {
|
||||
callback('Is required and must be in hex format');
|
||||
}
|
||||
callback();
|
||||
}
|
||||
}
|
||||
],
|
||||
type_id: [
|
||||
{
|
||||
validator(
|
||||
_rule: InternalRuleItem,
|
||||
value: string,
|
||||
callback: (error?: string) => void
|
||||
) {
|
||||
if (isNaN(parseInt(value, 16))) {
|
||||
callback('Is required and must be in hex format');
|
||||
}
|
||||
callback();
|
||||
}
|
||||
}
|
||||
NAME_PATTERN_REQUIRED,
|
||||
uniqueCustomNameValidator(entity, entityItem.o_name)
|
||||
],
|
||||
device_id: [hexValidator],
|
||||
type_id: [hexValidator],
|
||||
offset: [
|
||||
{ required: true, message: 'Offset is required' },
|
||||
{ type: 'number', min: 0, max: 255, message: 'Must be between 0 and 255' }
|
||||
@@ -388,33 +325,14 @@ export const entityItemValidation = (entity: EntityItem[], entityItem: EntityIte
|
||||
export const uniqueTemperatureNameValidator = (
|
||||
sensors: TemperatureSensor[],
|
||||
o_name?: string
|
||||
) => ({
|
||||
validator(_rule: InternalRuleItem, n: string, callback: (error?: string) => void) {
|
||||
if (
|
||||
(o_name === undefined || o_name.toLowerCase() !== n.toLowerCase()) &&
|
||||
n !== '' &&
|
||||
sensors.find((ts) => ts.n.toLowerCase() === n.toLowerCase())
|
||||
) {
|
||||
callback('Name already in use');
|
||||
} else {
|
||||
callback();
|
||||
}
|
||||
}
|
||||
});
|
||||
) => createUniqueFieldNameValidator(sensors, (s) => s.n, o_name);
|
||||
|
||||
export const temperatureSensorItemValidation = (
|
||||
sensors: TemperatureSensor[],
|
||||
sensor: TemperatureSensor
|
||||
) =>
|
||||
new Schema({
|
||||
n: [
|
||||
{
|
||||
type: 'string',
|
||||
pattern: /^[a-zA-Z0-9_]{0,19}$/,
|
||||
message: "Must be <20 characters: alphanumeric or '_'"
|
||||
},
|
||||
...[uniqueTemperatureNameValidator(sensors, sensor.o_n)]
|
||||
]
|
||||
n: [NAME_PATTERN, uniqueTemperatureNameValidator(sensors, sensor.o_n)]
|
||||
});
|
||||
|
||||
export const isGPIOUniqueValidator = (sensors: AnalogSensor[]) => ({
|
||||
@@ -434,47 +352,32 @@ export const isGPIOUniqueValidator = (sensors: AnalogSensor[]) => ({
|
||||
export const uniqueAnalogNameValidator = (
|
||||
sensors: AnalogSensor[],
|
||||
o_name?: string
|
||||
) => ({
|
||||
validator(_rule: InternalRuleItem, n: string, callback: (error?: string) => void) {
|
||||
if (
|
||||
(o_name === undefined || o_name.toLowerCase() !== n.toLowerCase()) &&
|
||||
n !== '' &&
|
||||
sensors.find((as) => as.n.toLowerCase() === n.toLowerCase())
|
||||
) {
|
||||
callback('Name already in use');
|
||||
} else {
|
||||
callback();
|
||||
}
|
||||
}
|
||||
});
|
||||
) => createUniqueFieldNameValidator(sensors, (s) => s.n, o_name);
|
||||
|
||||
export const analogSensorItemValidation = (
|
||||
sensors: AnalogSensor[],
|
||||
sensor: AnalogSensor,
|
||||
creating: boolean,
|
||||
platform: string
|
||||
) =>
|
||||
new Schema({
|
||||
n: [
|
||||
{
|
||||
type: 'string',
|
||||
pattern: /^[a-zA-Z0-9_]{0,19}$/,
|
||||
message: "Must be <20 characters: alphanumeric or '_'"
|
||||
},
|
||||
...[uniqueAnalogNameValidator(sensors, sensor.o_n)]
|
||||
],
|
||||
) => {
|
||||
const gpioValidator =
|
||||
platform === 'ESP32S3'
|
||||
? GPIO_VALIDATORS3
|
||||
: platform === 'ESP32S2'
|
||||
? GPIO_VALIDATORS2
|
||||
: platform === 'ESP32C3'
|
||||
? GPIO_VALIDATORC3
|
||||
: GPIO_VALIDATOR;
|
||||
|
||||
return new Schema({
|
||||
n: [NAME_PATTERN, uniqueAnalogNameValidator(sensors, sensor.o_n)],
|
||||
g: [
|
||||
{ required: true, message: 'GPIO is required' },
|
||||
platform === 'ESP32S3'
|
||||
? GPIO_VALIDATORS3
|
||||
: platform === 'ESP32S2'
|
||||
? GPIO_VALIDATORS2
|
||||
: platform === 'ESP32C3'
|
||||
? GPIO_VALIDATORC3
|
||||
: GPIO_VALIDATOR,
|
||||
gpioValidator,
|
||||
...(creating ? [isGPIOUniqueValidator(sensors)] : [])
|
||||
]
|
||||
});
|
||||
};
|
||||
|
||||
export const deviceValueItemValidation = (dv: DeviceValue) =>
|
||||
new Schema({
|
||||
@@ -488,13 +391,14 @@ export const deviceValueItemValidation = (dv: DeviceValue) =>
|
||||
) {
|
||||
if (
|
||||
typeof value === 'number' &&
|
||||
dv.m &&
|
||||
dv.x &&
|
||||
dv.m !== undefined &&
|
||||
dv.x !== undefined &&
|
||||
(value < dv.m || value > dv.x)
|
||||
) {
|
||||
callback('Value out of range');
|
||||
} else {
|
||||
callback();
|
||||
}
|
||||
callback();
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user