Skip to content

91ahmed/JsInspector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Js-Inspector

Javascript package for data validation, allow javascript developers to validate the application incoming data with a variety of validation rules.

# Install via npm

npm i jsinspector-js

# Example

// Import package
const JsInspector = require('jsinspector-js')

// Create new instance from JsInspector
const inspect = new JsInspector()

// Add your validation rules
inspect.data('ahmed')
       .field('username')
       .alpha()
       .required()

inspect.data('ahmed@gmail.com')
       .field('email')
       .email()
       .required()

inspect.data('12345678')
       .field('password')
       .max(30).min(8)
       .required()

// Check whether the validation process is successful or not
if(inspect.valid()){
   // (true - success) the data is valid
} else {
   // (false - fail) the data is not valid

   // Display errors
   console.log(inspect.errors())
}

# Available Rules

required()

The data must be not empty and it considered empty if the value is null or empty string or array.

numeric()

Should only contain whole numbers (0,1,2,3..) without negative, fractions or decimals.

integer()

Should only contain integer numbers that can be positive, negative, or zero.

email()

Must be a valid email address.

decimal()

The data under validation must be numeric contains whole and a fractional.

alpha()

The data must contain only alphabetic characters (letters).

alphas()

Similar to alpha() but accepts spaces (letters, spaces).

alnum()

The value must contain only alphanumeric characters (letters, numbers).

alnums()

Similar to alnum() but accepts spaces (letters, numbers, spaces).

url()

Must be a valid URL.

date()

Must be a valid date corresponding to this format ☛ (yyyy-mm-dd).

max() min()

max() The field under validation must be less than or equal to a maximum value.

min() The field under validation must have a minimum value.

pattern()

This method allows you to write your own Regex pattern.

// Example
const inspect = new JsInspector()

inspect.data('ahmed').field('fieldname').pattern(/^[A-Za-z ]*$/)

match()

The data should match the given value.

// Example
const inspect = new JsInspector()

inspect.data('1d23').match('1d23')

# Other Methods

errors()

This method returns error messages as an object.

// Example
const inspect = new JsInspector()

inspect.data('hello').email().required()

/** Display errors **/
console.log(inspect.errors())

show()

Displays the error message within a specific element. It takes one parameter the element (id).

<!-- element with ID to display incoming errors -->
<p id="error-msg"></p>
// Js Example
const inspect = new JsInspector()

inspect.data('hello').email().required().show('error-msg')

valid()

Check whether the validation process is successful or not. This method return boolean (ture) for success (false) for fail.

// Example
const inspect = new JsInspector()

inspect.data('notvalidemail').email().required()

if(inspect.valid()){
   // (true) success
} else {
   // (false) fail
}

data()

Specify the data to be validated. It takes one parameter (data)

data('value') // @param mixed, (data)

field()

Specify the field name. The class uses the value in this method for error messages, so it's very important to specify to get meaningful error messages.

It takes one parameter (fieldname)

field('username') // @param string, (input name)

About

Javascript package for data validation, to validate the application incoming data with a variety of validation rules.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published