samchon / typia

Super-fast/easy runtime validators and serializers via transformation
https://typia.io/
MIT License
4.59k stars 157 forks source link

Feature Request: Comment tag for decimal #554

Closed kakasoo closed 1 year ago

kakasoo commented 1 year ago
  /**
   * this means position value of images
   */
  @Column('numeric', { name: 'position', precision: 6, scale: 5, default: 0 })
  position?: number | `${number}` | null;

My image type has decimal values called "position" as follows. When I created a fake image using "typia.Random()", postgres kept spitting "numeric field overflow" errors. I now know the reason for this error. "Typia.Random()" generates random values using type and comment tags, and the problem is the decimal point. Endless decimal places ignore DB constraints. "Typia.Random()" produced a number below a decimal point that was too long. This was a value that ignored precision 6 and scale 5 of the DB column. Come to think of it, it's natural, but we need a way to stop this.

So I would like to suggest the following. ( I looked up the issue of this project, but I don't think a similar feature has been proposed yet. ) . .

  /**
   * this means position value of images
   * @type decimal(6,5)
   */
  @Column('numeric', { name: 'position', precision: 6, scale: 5, default: 0 })
  position?: number | `${number}` | null;
  /**
   * this means position value of images
   * @precision 6
   * @scale 5
   */
  @Column('numeric', { name: 'position', precision: 6, scale: 5, default: 0 })
  position?: number | `${number}` | null;

This is a suggestion, and even if it is not this way, I would welcome any way to limit the numbers below the decimal point.

samchon commented 1 year ago

Not possible because JS does not support the decimal type.

Instead, I will consider custom random generator which can get special custom comment tags.

kakasoo commented 1 year ago

I think I have a misunderstanding about this library. Is the validation logic of this library purely JS spec? Including typia.random method?

OK, Thanks for answering. 😅

samchon commented 1 year ago

Adding decimal validation is not possible, but random() would be customizable.

I will provide comment tags info into the IRandomGenerator, then you can customize it.

kakasoo commented 1 year ago

As you said, writing test codes will be very, very easy. That's the feature I wanted! Today I learned more deeply how to use typia.

samchon commented 1 year ago

Also about the validation, no way to check the decimal number exactly in the floating number system.

samchon commented 1 year ago

If you want to specify number range to be randomly created, utilize those tags:

kakasoo commented 1 year ago

Wait, wait, wait. What if you use your @pattern to verify the number string? Even if it's not a number, is this possible?

samchon commented 1 year ago

Yes, possible only when string

kakasoo commented 1 year ago

Then I will change the type to number string and then use the method of verifying with pattern. It is unfortunate that decimal verification is not possible, but using patterns seems like a good idea. It's a separate question, is the @pattern impossible for the string literal type?

samchon commented 1 year ago

Use such tags, then everything would be fine

kakasoo commented 1 year ago

You are a genius!