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

modelgun

v0.1.9

Published

Generator of data model file and util.

Downloads

8

Readme

Modelgun 👾👾👾 🔫 - Data model and util auto-generation tool for TypeScript.

npm npm

Overview

This is a tool to generate model classes and related utils with validations. What you should do is just define model structures and validation rules. Currently following files are generated by Modelgun.

  • model: A class to represent a model which validation methods. For creating models with validations and holding model data and converting to plain JavaScript Objects.
  • parser: Parser to convert any object to models. For checking API json payload etc.
  • faker (alpha): Fake value generation tool for tests.

What is Modelgun🔫 originally?

Modelguns are Japanese replica or toy guns, which are usually made of zinc alloys or plastic materials. https://en.wikipedia.org/wiki/Modelguns

Motivation

We write data validation code so many times to build a system. But the work is very boring, time consuming and can be done by robots. What human should do is just design the structures of models and defines the validation rules. This tool make you free from the boring works and your life will be easier 😃.

How to

1. Install

npm

npm install --save-dev modelgun

yarn

yarn add --dev modelgun

2. Define models

Modelgun uses toml as the format of definition files. Use [MODEL_NAME].model.toml format for file names. [MODEL_NAME].model.ts, [MODEL_NAME].parser.ts and [MODEL_NAME].faker.ts (alpha) are generated for each model definition. See Model definition file spec section for more details.

3. Run

To run Modelgun generator, use following commands. Modelgun processes definition files in a specified directory and generates files in the same directory. (* Currently recursive checking of deep directories is not supported.)

In scripts in package.json, use

modelgun gen path/to/dir

To call script directory, use

./node_modules/modelgun/bin/cli.js gen path/to/dir

Examples

Example model definition files and generated files are stored here.

Supported languages

Currently only TypeScript is supported. If number of users increase, will consider to write JavaScript version.

Model definition file spec

Top level options

Model setting parameters.

description

Description about the model.

description = "This is the data structure to "

header

Header is placed at the top of auto-generated model file. You can write anything like constants, import statements and functions. This was introduced for custom validations. But you may find more cases. Please let me know if you find some.

header = "
// Importing
import anyFunc from \"./path/to/file\"

function customValidator (value: string) {
  ...
}
"

Generator

  • parser: Whether to generate a parser file.
  • faker: Whether to generate a faker file.
[generator]
parser = false
faker = true

Properties

props Defines model properties. Property name follows after props.

type

Type of property. Please use supported types.

[props.id]
type = "uuid"

[props.name]
type = "string"

[props.age]
type = "int"

Supported types

  • boolean
  • float
  • int
  • string
  • uuid
  • url
  • email

null

Modelgun supports nullable for non-array properties. Specify null option like below.

[props.name]
type = "string"
null = true

Referring another model

Use "ref:[MODEL_NAME]"

TODO: Let users to use file path to a model.

validation

Depending on types of properties, modelgun auto generates validation code in model class. For some types, modelgun uses validator.js. Please add the npm package when you use types which requires it (e.g. uuid, email). Additionally, you can specify detailed validation.

Number

  • min
  • max
  • candidates

String

  • regex
  • maxLength
  • minLength
  • candidates

Custom validator for any types

You can use your custom validator in files in your project. For those case, use header to import your functions or write directly in the generated model file. Throw InvalidPropertyError defined in the auto-generated model file for error cases.

header = "
import nameValidator from \"./path/to/file\"

function ageValidator (value: number) {
  if (value === 1000) {
    InvalidPropertyError(\"1000 years old is unbelievable.\")
  }
}
"

[props.name]
type = "string"
customValidator = "nameValidator"

[props.age]
type = "number"
customValidator = "ageValidator"