1.3.4 • Published 1 year ago

easy-validation-js v1.3.4

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

Easy validation js

This is validation library for Javascript . You can use this library server side and front end side.

Badges

Add badges from somewhere like: shields.io

MIT License GPLv3 License AGPL License

Installation

Install easy-validation-js with npm

  npm install easy-validation-js

Features

  • Validate string
  • Validate number
  • Validate boolean
  • Validate array
  • Validate object

Usage/Examples

String and number

const { Easy } = require('easy-validation-js'); // CommonJS
import { Easy } from 'easy-validation-js'; // ESM
// for string data
const str1 = 'hello world';
const str2 = 20;
const myStr = Easy.isString(str2, {
  isRequire: true,
  message: 'string required',
  error: true, // when you enable the error you crashed server;
});
console.log(myStr); // return string required

// for number data
const num1 = 10;
const num2 = 'hello world';

const myNum = Easy.isNumber(num2, {
  isRequire: true,
  message: 'number value required',
});
console.log(myNumber);

Object use cases

const { Obj } = require('easy-validation-js'); // CommonJS
import { Obj } from 'easy-validation-js'; // ESM

const myObj = { data: 'hello world', status: '200' };
// check is object
const isObj = Obj.isObj(myObj, { isRequire: true, message: 'obj required' });

// check object key
isObj.key('data status');

// check object length
isObj.len(2);

// check this key must exist in object
const result = Obj.obj(myObj).keySome('status');
console.log(result); // return true

const result2 = Obj.obj(myObj).keySome('name');
console.log(result2); //return key same required

const result3 = Obj.obj(myObj).keySome('name', {
  message: 'need same key',
  isRequire: true,
  error: false,
});
console.log(result3); //return need same key

// check value type when all of value same type
const myObj = { data: 'hello world', status: 200 };
const result = Obj.obj(myObj).valType('string'); // you can also pass option
console.log(result); // return string type required

// check object with defining schema like this
const myObj = { data: 'hello world', status: 200, isOk: true };
const sch = { data: 'string', status: 'number' };
const result = Obj.obj(myObj).schema(sch); // return true

const sch2 = { data: 'string', status: 'string' };
const result2 = Obj.obj(myObj).schema(sch2); // return type required

// note: schema method don't check additional key type . if you need strictly check you can use schemaStrict method.
const result3 = Obj.obj(myObj).schemaStrict(sch2); //return type required

License

MIT

Contributing

Contributions are always welcome!

See contributing.md for ways to get started.

Please adhere to this project's code of conduct.

1.3.4

1 year ago

1.3.3

1 year ago

1.3.2

1 year ago

1.3.1

1 year ago

1.2.0

2 years ago

1.1.1

2 years ago

1.2.8

2 years ago

1.1.9

2 years ago

1.2.7

2 years ago

1.1.8

2 years ago

1.0.9

2 years ago

1.2.6

2 years ago

1.1.7

2 years ago

1.0.8

2 years ago

1.2.5

2 years ago

1.1.6

2 years ago

1.0.7

2 years ago

1.2.4

2 years ago

1.1.5

2 years ago

1.0.6

2 years ago

1.2.3

2 years ago

1.1.4

2 years ago

1.0.5

2 years ago

1.2.2

2 years ago

1.1.3

2 years ago

1.0.4

2 years ago

1.2.1

2 years ago

1.1.2

2 years ago

1.0.3

2 years ago

1.2.9

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago