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

nodejs-schema-rules

v1.0.5-alpha

Published

The CLI tool automatically generates basic validation rules for popular libraries such as JOI, ValidatorJS and @vinejs/vine based on your database table schema!

Downloads

22

Readme

NodeJS Schema Rules

NPM Downloads npm License: MIT

The CLI tool automatically generates basic validation rules for popular libraries such as JOI, ValidatorJS and @vinejs/vine rules based on your database table schema. These rules serve as a convenient starting point, allowing you to refine and enhance them to suit your specific needs.

Installation

Installing nodejs-schema-rules globally to access ndVr CLI

npm install nodejs-schema-rules -g
yarn add global nodejs-schema-rules

Then run ndVr init for initialization of schema.config.js file then modify as your requirement.

ndVr init

Modify the schema.config.js

require("dotenv").config();
const schemaConfig = {
  defaultDatabase: 'sqlite',
  databases: {
    postgres: {
      host: 'localhost',
      port: 5432,
      user: 'postgres',
      password: '123456',
      database: 'testing'
    },
    mysql: {
      host: 'localhost',
      port: 3306,
      user: 'root',
      password: '123456',
      database: 'schema_builder'
    },
    sqlite: { database: './schema_builder.db' }
  },
  skipColumns: [ 'created_at', 'updated_at', 'deleted_at' ],
  validationSchemaType: 'joi'
};
module.exports = schemaConfig;

Usage

The ndVr joi -t my_table -db mysql -c column1,column2 command generates validation rules for a specified database table and its columns. It creates a validation rules based on the chosen validation libraries like joi, "validatorjs", "vine". The generated rules can be used to enforce data integrity and validate incoming requests in your application.

Options:

  • -db, --database: Specify the type of database (e.g., mysql, postgres, sqlite).
  • -t, --table: Specify the name of the database table for which rules should be generated.
  • -c, --columns: Specify the column names of the table to generate rules for.
  • -h, --help: Display help for the command.

Examples:

  • Generate rules for a MySQL table named users with columns id and name:

    ndVr joi -t users -db mysql -c id,name
  • Generate rules for a PostgreSQL table named users with a validation library validatorJs:

    ndVr validatorJs -t users -db mysql -c id,name

as same as for sqlite.

Let's say you've the table structure:

CREATE TABLE data_types (
    id INTEGER PRIMARY KEY,
    name TEXT,
    age INTEGER,
    height REAL,
    is_student BOOLEAN,
    birthdate DATE,
    registration_timestamp TIMESTAMP,
    description BLOB
    created_at TIMESTAMP,
    updated_at TIMESTAMP
);

Generate rules for a whole table

Now if you run:

ndVr joi -db sqlite -t data_types

You'll get:

🚀 Schema Base Validation rules for "joi" generated! 🚀
Copy and paste these rules into your validation location, such as controller, form request, or any applicable place 😊
______________________________________________________________________________________________________________________


{ 
  name: Joi.string().required(),
  age: Joi.integer().min(-9223372036854775808).max(9223372036854775807).required(),
  height: Joi.number().required(),
  is_student: Joi.required(),
  birthdate: Joi.date().required(),
  registration_timestamp: Joi.required(),
  description: Joi.required(), 
}

Generate rules for specific columns

You can also explicitly specify the columns:

ndVr joi -db sqlite -t data_types -c name,age

Which gives you:


🚀 Schema Base Validation rules for "joi" generated! 🚀
Copy and paste these rules into your validation location, such as controller, form request, or any applicable place 😊
______________________________________________________________________________________________________________________

{ 
  name: Joi.string().required(),
  age: Joi.integer().min(-9223372036854775808).max(9223372036854775807).required(), 
}

Always skip columns

To always skip columns add it in the schema-config.js file, under skipColumns attribute.

skipColumns: (process.env.SKIP_COLUMNS || 'created_at,updated_at,deleted_at').split(',')

Supported Drivers

Supported database drivers are MySQL, PostgreSQL, and SQLite.

Validation rules may vary based on the selected driver due to differences in supported data types and range specifications.

Testing

yarn test

Author

👤 Md Tasmidur Rahman [email protected] (https://tasmidur.netlify.app)

🤝 Contributing

Contributions, issues and feature requests are welcome!Feel free to check issues page

Show your support

Give a ⭐️ if this project helped you!

License

The MIT License (MIT). Please see License File for more information.