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.

Feature Request: @Default Annotation

See original GitHub issue

Currently it is very unhandy to set a default value for properties in case of a validation failure. You have to evaluate the ValidationError and set the default value on your own. It would be nice to have a new annotation, something like @Default(‘en-GB’), which allows the user to set a default value.

Instead of

@IsNotEmpty()
locale: string;

const localeErrors = validationErrors.find(error => error.property === 'locale');
if (localeErrors) {
    initialQueryParameters.locale = 'en-GB';
}

you could simply write

@Default('en-GB')
@IsNotEmpty()
locale: string;

which sets a default value in case of an ocuring error.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

8reactions
driescroonscommented, Mar 20, 2019

For those interested; this is the decorator I quickly threw together using the Transform function.

import { Transform } from 'class-transformer';

const defaultValueDecorator = (defaultValue: any) => {
  return Transform((target: any) => target || defaultValue);
};

export default defaultValueDecorator;

Example form my paginationQueryValidator (for lists)

@Exclude()
class PaginationQueryValidator {
  @Expose()
  @numeric()
  @defaultValueDecorator(0)
  public offset: number;

  @Expose()
  @numeric()
  @defaultValueDecorator(20)
  public limit: number;
}

export default PaginationQueryValidator;
0reactions
lock[bot]commented, Oct 23, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Feature request: Set default annotation values #3840 - GitHub
There are many annotations which provide a default value out of the box, but only of a few the default value can be...
Read more >
[Feature request] Default values not used in annotations
Hi,. It seems there is an option called default - you could support this its working, but skipper of course overwrites my changes....
Read more >
Feature request: add option to set default color for annotations ...
Annotations in evince are yellow by default. When marking up PDFs with a yellow background, however, it is more appropriate to use a ......
Read more >
Annotations Feature Request - GdPicture Imaging Forums
Hi, I would like to add a feature request. Allow setting default annotation font family, font size, and annotation text so that when...
Read more >
Ability to change default annotation color - Evernote Forum
I'd love the abiliy to change the default color for the annotation tools such as the text tool and arrow tool. I'd also...
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