Inspired by pydantic, this is an object class modeler. This library holds 2 main things:
- The
createModel
function which takes 2 arguments (listed below) and returns aModel
class with a structure based on the passed structure - The
Structs
object with functions that create property structures for creating the desired type and handling of the property
model_name: string
which is just the class name per say, errors will show this namerequest_structure: ModelRequestStructure
the structure of the generated Model
The ModelRequestStructure
is based on the values from the functions in Structs
and functions you declare there. This is essentially the shape of the class you want to make and what methods it should have. All functions passed are attached to the generated class's prototype so all class instances will have these functions as "methods"
You can pass an object with the following keys to change behavior of the property strcture to the struct functions:
default_value:T
set the default value for the propertyrequired:boolean
declare if the property is required to exist among the passed keys when creating a new Model instance Properties, defaults to truenullable:boolean
declare if this property is allowed to benull
, defaults to true
The following are the functions for creating property structures
string
: create astring
property, defaults to non-null empty stringint
: create anumber
property that also checks if the number is an integer withNumber.isInteger
, defaults to0
number
: create anumber
property, defaults to non-null0.0
bool
: create aboolean
property, defaults to non-nullfalse
bigint
: create aBigInt
property, defaults to non-nullBigInt(0)
map
: create aMap<string, T>
property, defaults to non-nullMap<string, any>
model
: create aModel
property, defaults to nullablenull
Just a minor example of what you could do with it though for validating post requests there's better stuff out there if you want a proper robust system
// Model declaration
const PostRequest = createModel('FormResult', {
name: Structs.string(),
// When setting data if in TS pass as the generic whether the property is required or not for proper intellisense, don't know how to handle this properly with typing :(
email: Structs.string<false>({ required: false }),
age: Structs.int(),
type: Structs.string(),
// For adding methods to the class, just add create the function as if here was the class declaration
getIntroduction():string {
return `Hello, my name is ${this.name} and I'm ${this.age} years old.`
}
});
// Theoretical post request
app.post('/post', (req, res) => {
let posted:PostRequest;
try {
posted = new PostRequest(req.body);
} catch (e) {
// Response sents how the sent body differs from the required params
res.send(e.message);
return;
}
/*
Use posted data for something
*/
res.send(posted.getIntroduction());
});