scotthovestadt / schema-object

Enforce schema on JavaScript objects, including type, transformation, and validation. Supports extends, sub-schemas, and arrays.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Value post manipulation for all types

danieljuhl opened this issue · comments

This is a suggestion to make post manipulation method no matter the typecast. We have the stringTransform but if there could be a genereal postTransform we could use it on eg. the Number type to round the number.

{
    number: {
        type: Number,
        postTransform: function(value){
            return (Math.round(value*100)/100);
        }
    }
}

stringTransform was written for the use-case specifically of handling strings after typecasting but before the other properties had a chance to touch the value. If the value passed back from stringTransform doesn't meet the other criteria, it will be rejected-- for example, if it's too long.

I can see the value of having a similar property on other types.

My rule-of-thumb is that when a property needs to be referenced within the code that handles the typecasting, transformation, or validation for a specific type and can't be put into the code that every type (even untyped fields) run through, it should be specifically named for that type.

This property would qualify for that. Additionally, it's not something an untyped field would use.

For this reason, I'm going to add it for each individual type with it's own individual name. stringTransform already exists. I'll add numberTransform, booleanTransform, arrayTransform, objectTransform and dateTransform.

Added transformation for number, boolean, and date types.