Skip to content

Latest commit

 

History

History
112 lines (102 loc) · 3.95 KB

README.md

File metadata and controls

112 lines (102 loc) · 3.95 KB

image

Data Guard is a TypeScript library for validating and sanitizing data, inspired by Django Rest Serializer. It helps ensure the integrity of your API data by applying validation rules and cleaning unnecessary fields.

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