Skip to content

Commit

Permalink
minNumberOfChoices in multichoice election is optional
Browse files Browse the repository at this point in the history
  • Loading branch information
marcvelmer committed Nov 6, 2024
1 parent f4514c3 commit 179c92b
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/types/election/multichoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import {
getElectionMetadataTemplate,
} from '../metadata';
import { Vote } from '../vote';
import invariant from 'tiny-invariant';

export interface IMultiChoiceElectionParameters extends IElectionParameters {
maxNumberOfChoices: number;
minNumberOfChoices: number;
minNumberOfChoices?: number;
canRepeatChoices?: boolean;
canAbstain?: boolean;
}
Expand All @@ -32,7 +33,7 @@ export class MultiChoiceElection extends UnpublishedElection {
public constructor(params: IMultiChoiceElectionParameters) {
super(params);
this.maxNumberOfChoices = params.maxNumberOfChoices;
this.minNumberOfChoices = params.minNumberOfChoices;
this.minNumberOfChoices = params.minNumberOfChoices ?? null;
this.canRepeatChoices = params.canRepeatChoices ?? false;
this.canAbstain = params.canAbstain ?? false;
}
Expand Down Expand Up @@ -130,6 +131,10 @@ export class MultiChoiceElection extends UnpublishedElection {
}

set maxNumberOfChoices(value: number) {
invariant(
value >= (this.minNumberOfChoices ?? 0),
'Max number of choices must be greater than or equal to min number of choices'
);
this.voteType.maxCount = value;
}

Expand All @@ -138,6 +143,10 @@ export class MultiChoiceElection extends UnpublishedElection {
}

set minNumberOfChoices(value: number) {
invariant(
value <= this.maxNumberOfChoices,
'Min number of choices must be less than or equal to max number of choices'
);
this._minNumberOfChoices = value;
}

Expand Down

0 comments on commit 179c92b

Please sign in to comment.