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

@omer_bhatti/data-guard

v0.2.4

Published

Validate any JSON by defined set of rules and get cleaned JSON inspired by Django Rest Framework Serializer

Downloads

30

Readme

Features

  • Flexible Validation Rules: Validate various data types including strings, integers, floats, booleans, arrays, and dates.
  • Nested Rules: Apply validation rules to nested structures and arrays.
  • Customizable: Define your own validation rules and customize the behavior.
  • TypeScript Support: Strong typing ensures better development experience and fewer runtime errors.

Installation

You can install Data Guard via npm:

npm install @omerbhatti/data-guard

Usage

Here's a basic example of how to use Data Guard:

  1. Define Your Rules Create a set of validation rules for your API data:
import {
    StringRule, IntegerRule, FloatRule, BooleanRule, ArrayRule, DateRule, JSONRule, FileRule
} from '@omerbhatti/data-guard';

const USER_API_RULES = {
  username: new StringRule(true, null, 20, new RegExp('^[a-zA-Z0-9]+$')),
  age: new IntegerRule(false),
  salary: new FloatRule(true, 2),
  active: new BooleanRule(),
  ids: new ArrayRule(new ArrayRule(new ArrayRule(new FloatRule()))),
  startDate: new DateRule(true),
  endDate: new DateRule(true, new Date('2021-01-01'), new Date('2021-01-30')),
  status: new StringRule(true, null, null, null, ['draft', 'pending', 'in progress']),
  dict: new JSONRule(true, {
    email: new StringRule(false, null, 20, new RegExp('^[a-z]+@(gmail|xyz).com$')),
    age: new IntegerRule(false),
  }),
  data: new JSONRule(false, {}),
	image: new FileRule(true, 2048 * 2048 * 5, ['image/png', 'image/jpeg']),
};
  1. Create a Validator Instance
import Validator from '@omerbhatti/data-guard';

const validator = new Validator(USER_API_RULES, {
  username: 'test_user',
  extra: 'data: [1, 2, 3, 4]',
  dangerousField: 'Virus Code Here !!!',
  salary: '12000.2378787878',
  active: true,
  ids: [[1, 2, 3, 4], [1, 2, 3], [1]],
  startDate: '2024-09-05',
  endDate: new Date('2021-01-30'),
  status: 'draft',
  dict: {
    email: '[email protected]',
    age: 23,
  },
  image: new File([fs.readFileSync('./test.png')], 'test.png', { type: 'image/png' }),
});
  1. Validate Data
try {
  console.log("errors: " + validator.validate(true));  // Outputs validation results
  console.log("cleanedData: " + validator.cleanedData());   // Outputs cleaned data
} catch (exc) {
  console.error(exc);  // Handle validation errors
}

Output

errors: {}
cleanedData: {
  username: 'testuser',
  salary: 12000.24,
  active: true,
  ids: [ [ 1, 2, 3, 4 ], [ 1, 2, 3 ], [ 1 ] ],
  startDate: 2024-09-05T00:00:00.000Z,
  endDate: 2021-01-30T00:00:00.000Z,
  status: 'draft',
  dict: { email: '[email protected]', age: 23 },
  image: File {
    size: 1409,
    type: 'image/png',
    name: 'test.png',
    lastModified: 1725876450393
  },
}

API

Validation Rules

StringRule: Validates strings with optional length and regex constraints. IntegerRule: Validates integers. FloatRule: Validates floating-point numbers with optional precision. BooleanRule: Validates boolean values. ArrayRule: Validates arrays with optional nested rules. DateRule: Validates date values with optional min and max constraints. JSONRule: Validates JSON objects with optional schema validation. FileRule: Validates file type / size and other validations

Contact

For any inquiries, please reach out to [email protected]

Collaboration

  • Pull a branch from master named username/what-to-do
  • Do the required changes
  • Push the changes and create a pull request