Comments (5)
I don't know yet. But I'll figure out, if it's doable or not.
from fluent-ts-validator.
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.
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.
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.
Just released version 3.0.0 with the solution as shown above.
from fluent-ts-validator.
Related Issues (12)
- Special pre-built validation rules for iterables HOT 4
- How to validate dependant properties HOT 1
- fulfills(validator: Validatable<TProperty>) not collects the ValidationResult
- How to get custom validator error messages HOT 4
- Is asynchronous custom validation supported?
- Nesting validators via fulfills - property names (paths) not nested HOT 1
- How to validate optional parameters? HOT 3
- Group Validations HOT 2
- whenNotEmpty() NOT available after validateIfString() HOT 2
- Problems with version 2.1.1 HOT 5
- Can't run tests or coverage
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fluent-ts-validator.