Generic Object Property Formatter and Validator using Typescript
09 May 2023
Updated: 03 September 2023
At times there’s a need for transforming specific object properties from an input format to some specific output, this can be done using the following code in some generic means
The Concept of a Schema
The concept of a schema to be used for formatting some data can be defined as follows
An Interpreter
The implementation of an interpreter that satisfies the above can done as follows:
Transformers
We can define some general transformers, for example:
Usage
For the sake of example we can define a data type to use
Using as a Transformer
We can use the above by defining the transformer and using it to transform the input data according to the defined schema
Using as a Validator
Using the same principal as above we can use it for validaing data. Validation can be done by throwing in the transformer function
Exploration
It may be worth taking the concept and expanding it into a more fully fledged library with a way to handle validations more simply or to provide more builtin transformers and validators but for the moment it can be stated that there are enough javascript libraries for validation and the formatter alone is likely of limited value but may be worth exploring further if gaps in the existing technology are found
Regardless of the utility of the particular type above I think the concept and types give some insight into how one can build out a library as the ones discussed above