npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

model-generator

v0.7.6

Published

Model generator for applications that need models with typings and validation without the help of typescript

Downloads

14

Readme

Model generator

Model generator is a simple model generator tool that enables you to create models with types and validators.

How to use

install model-generator

Run install command to install model-generator.

npm install model-generator

import model-generator

The module has differend exports. Best practice is to put these in differend variables.

let ModelGeneratorModule = require('model-generator');
let ModelGenerator = ModelGeneratorModule().ModelGenerator;
let Property = ModelGeneratorModule().Property;
let Validators = ModelGeneratorModule().Validators;

###Optional settings Include an object

let settings = {
    /**
     * name: debug
     * default: false
     * requires: Boolean
     */
    debug: true
};

Edit the following line if you want to add settings:

let ModelGenerator = ModelGeneratorModule(settings).ModelGenerator;

Create your model

Create a class and extend it with the ModelGenerator class. Create a constructor and then call super(<=StructureForYourModel=>).


class ExampleModel extends ModelGenerator {
    constructor() {
        super(StructureForYourModel);
    }
}

Structure

Model generator generates a model using a structure. A structure is how your model will be generated. A structure takes an array of type Property. Example structure:


const ExampleModelStructure = {
    // array of all properties you want in your model
    properties: [
        new Property(
            'UserEmail',// property name
            undefined, // default value
            String, // type
            [Validators.Required, Validators.Email] // Array of validators
        ),
        new Property(
            'Test',
            undefined,
            Number,
            []
        )
    ]
};

Property

This module has an exported class Property. Property class can be used in the structure to define a property.

Property takes 4 arguments:

  1. Name : name of the property that needs to be generated
  2. Default value: the default value the property gets when you initialize the object.
  3. Type: Type of property(String , Number ,...)
  4. Validators : An array of all validators that need to run when you set a value on the property.

Property setting and getting

Properties are normal properties that automaticly run all validators and type checking. Use it like you would a normal property


Model.Property = "value"; // setting property runs all validators in the background
let model = Model.Property;

Validator

Validator is a check that needs to happen when a value is set on a property. Validators Need 2 Properties to work:

  • Name: Name of the validator is the name you can use in the validators array. (example : Validators.Email has name 'Email') Must be a string!
  • Validate : This function gets called when the validator is used. This function returns true or throws error.

Default validators

Here is a list of all validators that are available in the Validators array by default:

  • Required : Makes sure the property is required
  • Email : Makes sure the property is an email
  • String : Alphabetic letters with accents
  • DefaultPassword : Length: 8 to 128, with at least 1 lowercase, 1 capital, 1 number and/or symbol. https://goo.gl/dufj6t
  • LettersOnly : Alphabetic letters, lowercase or uppercase
  • NumbersOnly : Only numbers
  • DateTime : SQL format for a date
  • Base64 : Lowercase, uppercase, hyphen and underscore
  • NationalRegistryNumberBE : National registry number for in belgium

Register a custom validator

Need more validators? No problem you can inject your own validator using registerValidator.


let ModelGeneratorModule = require('model-generator');
// create validator
NotNegativeValidator = {
    Name : 'NotNegative',
    Validate : (value) => {
       if(value<=0){
           throw Error("Value can not be negative");
       }
       return true;
    }

}
// register Validator
ModelGeneratorModule.registerValidator(NotNegativeValidator);

When you add a validator it gets added to the ModelGeneratorModule.Validators The example above can be used like this:


New Property(
    'TestNumber',
    0,
    Number,
    [Validators.NotNegative]
)

Get clean model in json format

The generated model is bloated with properties and functions. Sometimes you need to return a model that is a clean javascript object without setters and getters. The getCleanModel is a function set on the generated model that returns the json model.

Casting in model

You can cast a json format into a Model using the exported member Cast. The function will throw error if it fails and return a model if it succeeds.

let Cast = require('./ModelGeneratorModule').Cast;
let Model = <= Insert your model here =>
json = {
    Prop1:1,
    Prop2:'test',
}
Cast(json,Model)

Full example of a model


let ModelGeneratorModule = require('./ModelGeneratorModule');
let ModelGenerator = ModelGeneratorModule.ModelGenerator;
let Property = ModelGeneratorModule.Property;
let Validators = ModelGeneratorModule.Validators;

const ExampleModelStructure = {
    properties: [
        new Property(
            'UserEmail',//property name
            undefined, // default value
            String, // type
            [Validators.Required, Validators.Email] // Array of validators
        ),
        new Property(
            'Test',
            undefined,
            Number,
            []
        )
    ]
};
class ExampleModel extends ModelGenerator {
    constructor() {
        super(ExampleModelStructure);
    }
}