question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Required @Input()

See original GitHub issue

Which @angular/* package(s) are relevant/related to the feature request?

No response

Description

Inputs in Angular arent required at all and bugs are cached while manually testing the code. We could write for every component checks if the object will be present onInit if its null etc. From my personall experience this leads to bigger and less maintainable code structure.

Proposed solution

I suggest angulars takes a look to add either a new pipe @Required - which I have an example for, or extend the existing one @Input to additionally provide a object with meta data if its required, what value are allowed etc.

@RequiredInputs()
@Component({
...
})
export class MyComponent {
   readonly data$ = new ReplaySubject<string[] | undefined>();

  @Input() @Required() set data(data: string[] | undefined) { // By default undefined, null and NaN are forbidden. To change behavior just pass config.
    this.data$.next(data);
  }
}

Alternatives considered

My code currently works for observables but the exact same behavior can be built for basic input fields.

For more see those files: https://github.com/muhamedkarajic/angular-playground/tree/main/src/app/shared/decorators

P.S:

In order to extend existing decorator @Input() we should make the current string be of an string | object type. If an object is passed inside the object can be the proper input identifier and if inside that object there is no identifier it would (as currently) use the property name. If there isn’t any value passed or a string is passed (as now) it would fall back to the current behavior - that way nothing would break.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
pkozlowski-opensourcecommented, Aug 10, 2022

@thw0rted we want to centralize the discussion about required inputs in one place, regardless of the proposed solution. Generally speaking we would like to have an issue that focuses on the problem statement as there are multiple approaches and we are not settling on any of the options just yet.

2reactions
atscottcommented, Jul 25, 2022

@muhamedkarajic The proposed solution may be different but the feature request is the same: Provide a way to define required inputs. How this gets implemented may or may not be any of the proposed solutions. That said, we really want to consolidate all discussions rather than having 4 different issues open for each feature request.

Read more comments on GitHub >

github_iconTop Results From Across the Web

HTML input required Attribute - W3Schools
The required attribute is a boolean attribute. When present, it specifies that an input field must be filled out before submitting the form....
Read more >
HTML attribute: required - HTML: HyperText Markup Language
The Boolean required attribute, if present, indicates that the user must specify a value for the input before the owning form can be ......
Read more >
HTML | <input> required Attribute - GeeksforGeeks
The HTML required Attribute is a Boolean attribute which is used to specify that the input element must be filled out before submitting...
Read more >
Make directive @Input required - angular - Stack Overflow
Allows re-using rules that you've already used for your Angular forms. Usage: export class MyComponent { @Input() propOne: string; @ ...
Read more >
Required @Input() properties - Tim Deschryver
Required @Input() properties ... This week I got asked how to make a component's input property required. Without giving it much thought I...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found