For data that can be provided e.g. as either a string or number, but which should always be treated as a number, the ability to convert the data before constraints would be nice.
This is so that we can treat the two objects: { num: '42' } and { num: 42 } the same, as the first one allows for conversion to the later. While this can be done with separate rules that works on either string or numbers, if the value should be between e.g. 40 and 50, it becomes cumbersome.
Perhaps something like a FieldAs<int>("num", Must.Be.GreaterThan(39) & Be.LessThan(51)) or Field("num", As.Number(...optional converter...).Must.Be
Alternative could be adding this as a constraint wrapper: Must.Be.LessThan(51).AsNumber() - This call would be required for each single constraint that should try and convert first, on the plus side, this is very easy to add to the framework. (There is the descriptors to consider, but other than that it would be a simple decorator pattern)
For data that can be provided e.g. as either a string or number, but which should always be treated as a number, the ability to convert the data before constraints would be nice.
This is so that we can treat the two objects:
{ num: '42' }and{ num: 42 }the same, as the first one allows for conversion to the later. While this can be done with separate rules that works on either string or numbers, if the value should be between e.g. 40 and 50, it becomes cumbersome.Perhaps something like a
FieldAs<int>("num", Must.Be.GreaterThan(39) & Be.LessThan(51))orField("num", As.Number(...optional converter...).Must.BeAlternative could be adding this as a constraint wrapper:
Must.Be.LessThan(51).AsNumber()- This call would be required for each single constraint that should try and convert first, on the plus side, this is very easy to add to the framework. (There is the descriptors to consider, but other than that it would be a simple decorator pattern)