Throwing exceptions for undefined inputs

UI Components for Angular
Post Reply
rzvjerkovic
Posts: 1
Joined: 11 Jan 2018, 16:07

16 Jan 2019, 18:13

Hello there,

I was just building primeng component wrappers for my application. When I wrapped p-calendar I forwarded inputs from my components to p-calendar components, like so:

Code: Select all

<p-calendar [placeholder]="placeholder"
    [selectionMode]="selectionMode"
    [timeOnly]="timeOnly"
    ... etc ...
    [showTime]="showTime"
     [hourFormat]="hourFormat"
    (onSelect)="onValueSelected($event)"></p-calendar>
Everything worked fine for most of the inputs, but selectionMode and hourFormat inputs only accept three and two possible input values respectively ('single' | 'multiple' | 'range' and '12' | '24').

So, by not adding redundant inputs in my component,

Code: Select all

<my-calendar placeholder="'placeholder'"></my-calendar>
Angular will pass undefined to p-calendar to both selectionMode and hourFormat.

What happens is that calendar will not work properly and no error will be thrown.
Same happens for

Code: Select all

 [selectionMode]="'someKindOfTypo'"
What do prime developers think about adding sanity checks for inputs like those?
I can see developers wasting time for a single typo which can easily be errored:

Code: Select all

    private _selectionMode: string;

    @Input() get selectionMode(): string {
        return this._selectionMode;
    }
    set selectionMode(_selectionMode: string) {
        if (!["single", "multiple", "range"].includes(_selectionMode)) { // perhaps add an enum or list on top of the file
            throw "Invalid selection mode";
        }
        this._selectionMode = _selectionMode;
    }
Of course I solved that easily by adding default input value, but it still won't catch any typos and alike.

Thanks for the answer!

Post Reply

Return to “PrimeNG”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 4 guests