Giter Site home page Giter Site logo

Comments (5)

markusbohl avatar markusbohl commented on June 23, 2024

I don't know yet. But I'll figure out, if it's doable or not.

from fluent-ts-validator.

markusbohl avatar markusbohl commented on June 23, 2024

Okay. I've implemented a possible solution in a feature-branch as you can see from the commits above.
It would work like this:

enum StringColors {
    Red = "RED",
    Green = "GREEN",
    Blue = "BLUE"
}

enum NumColors {
    Black = 0,
    White = 1
}

class SomeColors {
    color1: StringColors;
    color2: NumColors;
    color3: string;
    color4: number;
}

class ColorValidator extends AbstractValidator<SomeColors> {
    constructor() {
        super();
        this.validateIf(dto => dto.color1) // type StringColors
            .isIn(StringColors);
        this.validateIf(dto => dto.color2) // type NumColors
            .isIn(NumColors);
        this.validateIf(dto => dto.color3) // type string
            .isIn(StringColors);
        this.validateIf(dto => dto.color4) // type number
            .isIn(NumColors);
    }
}

const someColors = new SomeColors();
someColors.color1 = StringColors.Blue;
someColors.color2 = NumColors.Black;
someColors.color3 = "GREEN";
someColors.color4 = 1;

new ColorValidator().validate(someColors); // valid

Since enum members can either be of type number or of type string (since TypeScript 2.4) I think it is useful to use the more general validateIf() method instead of validateIfNumber() or validateIfString(). This makes the validator more robust in case of type changes within the enum (i.e. switching from number to string initializers).

The enum fields as well as string or number fields of your object under validation can now be validated against the members of enums. Actually, this would not only work for the isIn() case but also for the opposite isNotIn() case.

What do you think? Would such an implementation meet your needs? Or do you think, we should do something different? Let me know what you think.

from fluent-ts-validator.

jezzsantos avatar jezzsantos commented on June 23, 2024

Thanks for this @markusbohl I'll try it out shortly.
But in terms of design as you point out, I am happy with which way you want to go.

from fluent-ts-validator.

markusbohl avatar markusbohl commented on June 23, 2024

I've published a release candidate [email protected] on npm. This should make it a bit easier for you to test.

from fluent-ts-validator.

markusbohl avatar markusbohl commented on June 23, 2024

Just released version 3.0.0 with the solution as shown above.

from fluent-ts-validator.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.