AlexJPotter / fluentvalidation-ts

A TypeScript-first library for building strongly-typed validation rules
Apache License 2.0
87 stars 6 forks source link

[Bug] Expression produces a union type that is too complex to represent #38

Closed celluj34 closed 8 months ago

celluj34 commented 1 year ago

Hello;

I am using your library to validate that a date fits between a min and max date. We are actually using a string to back the date because we don't care about time or timezones, just the literal date. Here's our "fake" date class:

type Digit = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0;
type NonZeroDigit = Exclude<Digit, 0>;

type ISODateYear = `20${Digit}${Digit}`;
type ISODateMonth = `0${NonZeroDigit}` | `1${0 | 1 | 2}`;
type ISODateDay = `0${NonZeroDigit}` | `${1 | 2}${Digit}` | `3${0 | 1}`;

// type ISODate = "2000-01-01" | "2000-01-02" | "2000-01-03" | "2000-01-04" | "2000-01-05" | "2000-01-06" | "2000-01-07" | "2000-01-08" | "2000-01-09" | "2000-01-10" | "2000-01-11" | "2000-01-12" | ... 37187 more ... | "2099-12-31"
export type ISODate = `${ISODateYear}-${ISODateMonth}-${ISODateDay}`;

If I have a rule on a property with such a type, we are getting an error:

    // Expression produces a union type that is too complex to represent.
    this.ruleFor("myDate").notNull().notEmpty().must(fitIntoSqlDateTime);

How is your library composing types such that I would get this error? What can I do to work around it?

celluj34 commented 1 year ago

For now I have worked around this by using ruleForTransformed and just calling x => x.toString()

AlexJPotter commented 8 months ago

Hi @celluj34 - the types under the hood have got rather complicated, and could do with a bit of tidying up. For now I'll close this since you have an appropriate workaround, but thanks for bringing this to my attention 😃

celluj34 commented 8 months ago

Not a problem - it's worked for me so hopefully it helps some poor souls from google 😀