add duplicate name check

This commit is contained in:
proddy
2023-06-24 16:50:38 +02:00
parent 8b8b023665
commit f61c447ea5

View File

@@ -1,5 +1,5 @@
import Schema from 'async-validator'; import Schema from 'async-validator';
import type { AnalogSensor, DeviceValue, Settings } from './types'; import type { AnalogSensor, DeviceValue, ScheduleItem, Settings } from './types';
import type { InternalRuleItem } from 'async-validator'; import type { InternalRuleItem } from 'async-validator';
import { IP_OR_HOSTNAME_VALIDATOR } from 'validators/shared'; import { IP_OR_HOSTNAME_VALIDATOR } from 'validators/shared';
@@ -86,16 +86,26 @@ export const createSettingsValidator = (settings: Settings) =>
}) })
}); });
export const schedulerItemValidation = () => export const uniqueNameValidator = (schedule: ScheduleItem[], o_name?: string) => ({
validator(rule: InternalRuleItem, name: string, callback: (error?: string) => void) {
if ((o_name === undefined || o_name !== name) && schedule.find((si) => si.name === name)) {
callback('Name already in use');
} else {
callback();
}
}
});
export const schedulerItemValidation = (schedule: ScheduleItem[], scheduleItem: ScheduleItem) =>
new Schema({ new Schema({
name: [ name: [
{ {
// TODO validator: add check for unique name
required: true, required: true,
type: 'string', type: 'string',
pattern: /^[a-zA-Z0-9_\\.]{0,15}$/, pattern: /^[a-zA-Z0-9_\\.]{0,15}$/,
message: "Must be <15 characters: alpha numeric, '_' or '.'" message: "Must be <15 characters: alpha numeric, '_' or '.'"
} },
...[uniqueNameValidator(schedule, scheduleItem.o_name)]
], ],
cmd: [ cmd: [
{ required: true, message: 'Command is required' }, { required: true, message: 'Command is required' },