2.2.2 • Published 3 years ago

check-types-v2 v2.2.2

Weekly downloads
3
License
MIT License
Repository
github
Last release
3 years ago

check-types-v2

npm version GitHub license Maintenance

New version of check types library. Check on npm and github.

🎉 Version 2.2.x is live 🎉

Check out for changes in the CHANGELOG:

Changelog

Supporting the project

Maintaining a project takes time. To help allocate time, you can Buy Me a Coffee 😉

Install

npm install check-types-v2 --save
yarn add check-types-v2

Example

Following a generic example, full documentation is under construction.

const { Types, CheckTypes, Schema } = require('check-types-v2');
// Get data types helpers
const { String, Array, Number, Boolean, Float } = new Types();
// Call a new instance of CheckTypes only once.
const CheckType = new CheckTypes();

// Set a custom schema to validate data
const customSchema = new Schema({
  firstname: {
    type: String,
    required: true,
    default: null,
    match: /[A-z]/ig
  },
  roles: {
    type: Array,
    of: [String],
    required: true,
    enum: ['GUEST', 'USER', 'ADMIN'],
    default: ['ADMIN']
  },
  role: {
    type: String,
    required: true,
    enum: ['GUEST', 'USER', 'ADMIN'],
    default: 'ADMIN'
  },
  emails: {
    type: Array,
    of: String,
    required: true,
    default: [],
    match: /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ig
  },
  floatNumber: {
    type: Float,
    required: true,
    default: 1.5
  }
});

// Some custom features example
const value1 = CheckType.setValue().Any().Required().Default('test').Enum(['test']).check({ extended: true });
// Array and nested array example
const value2 = CheckType.setValue([1, 'test', true]).String().Array({ of: [String, Number, Boolean] }).check({ extended: true });

// Fake request body
const obj = {
  firstname: "alexdant91@gmail.com",
  username: 'test1',
  emails: ["alexdant91@gmail.com"],
  roles: ['ADMIN'],
  role: 'USER',
  floatNumber: 5.3
};
// Use schema to validate
const value3 = CheckType.setValue(obj).setSchema(customSchema).check({ strict: false, extended: true });

// Outputs
console.log(value1); // Will output: test
console.log(value2); // Will output: [ 1, 'test', true ]

console.log(value3);
/**
 * Will output:
 * {
 *   firstname: 'alexdant91@gmail.com',
 *   username: 'test1',
 *   emails: [ 'alexdant91@gmail.com' ],
 *   roles: [ 'ADMIN' ],
 *   role: 'USER',
 *   floatNumber: 5.3
 * }
 */

Examples

Check the examples directory. New examples will added soon.

To do

This project is under active maintenance. New features soon.

To fix

  • Fix errors handler, it takes the same error multiple times
  • Fix setValue, it may cause issue on schema processing

Improved

  • Integrate <Float> and <Int> validation type

Coming Soon Features

  • Integrate <Class> validation type
  • Integrate <Array>: { of: <Schema> } validation type
  • Integrate separate class for better error handling
  • Create middleware helpers compatible with express.js framework

Generic Task

  • Write complete documentation
  • Write examples for each function
2.2.2

3 years ago

2.2.1

4 years ago

2.2.0

4 years ago

2.1.0

4 years ago

2.0.7

4 years ago

2.0.9

4 years ago

2.0.8

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.5

4 years ago

2.0.4

4 years ago

2.0.6

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago