Skip to content

Commit

Permalink
add duplicate name check
Browse files Browse the repository at this point in the history
  • Loading branch information
proddy committed Jun 24, 2023
1 parent 8b8b023 commit f61c447
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions interface/src/project/validators.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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 { IP_OR_HOSTNAME_VALIDATOR } from 'validators/shared';

Expand Down Expand Up @@ -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({
name: [
{
// TODO validator: add check for unique name
required: true,
type: 'string',
pattern: /^[a-zA-Z0-9_\\.]{0,15}$/,
message: "Must be <15 characters: alpha numeric, '_' or '.'"
}
},
...[uniqueNameValidator(schedule, scheduleItem.o_name)]
],
cmd: [
{ required: true, message: 'Command is required' },
Expand Down

0 comments on commit f61c447

Please sign in to comment.