0.0.11 • Published 4 years ago

form-my-simple-validation v0.0.11

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

form-my-simple-validation

Build Status Coverage Status Maintainability npm version dependency badge license issues

Table of Contents

Introduction

As a JavaScript web developer, you will always need to validate your forms where applicable, ensuring that each input fields are properly taken care of, to avoid unexpected errors in your application.

Sometimes, validating input fields can be really boring and time consuming. To save your time and avoid boring moment, this package can help take care of some simple validations. All you have to do is: form-my-simple-validation

This npm-package is created for web applications. It can be applied both on the client-side and server-side JavaScript application.

Installation

$ npm install form-my-simple-validation

If you are using yarn, then run the below.

$ yarn add form-my-simple-validation

Usage

Import the package
const { Form } = require('form-my-simple-validation');

// OR,

// if you have a babel configured for your project, use:
import { Form } from 'form-my-simple-validation';
Call the function
const validationResult = Form.validateFields(formType, formSchema, userForm, customeError, allowNullOrUndefinedValue);

if (validationResult.error) {
    console.log(validationResult); // this will console an object
}
Params

The first three parameters are compulsory, while the rest are optional. The table below explains params.

ParamsData-TypesDescription
formType (required)StringThe type of form you want to perform validation on. (Note: The form type should be written as is, in the formSchema)
formSchema (required)ObjectThe schema for validating your form
userForm (required)ObjectUser form i.e an object containing form details, with the keys corresponding to the fields specified in your schema
customError (optional)String or objectThe message you want to return when validation requirement is not met. This field is optional because a default message is returned if customError is not specifield or when set to null.
allowNullOrUndefinedValue (optional)BooleanWhen set to false, an error message is returned when null or undefined is passed as a value to the user form object.

Usage example.

You need to create a schema as a validation requirement for the user-form object, in the format below:

Note: you may create schema in a separate JavaScript file.

formSchema.js file

// Sample form schema for sign-up
const Schema =  {
  form: {
    formType: 'signup',
    Email: { field: 'email', isEmail: true, required: true },
    FullName: { field: 'fullName', required: true, isName: true },
    Phone: { field: 'phoneNumber', required: true, isPhoneNumber: true },
    Password: {
      field: 'password', required: true, minLength: 8, maxLength: 15
    },
  }
};

module.exports = Schema;

// OR

// if you have configured babel in your project, use:
export default Schema;

sample.js file

const { Form } = require('form-my-simple-validation');
const formSchema = require('./path-to-formSchema-js-file')

// OR,

// if you have a babel configured for your project, use:
import { Form } from 'form-my-simple-validation';
import formSchema from './path-to-formSchema-js-file';

// sample user form object
const userForm = {
  fullName: 'Daniel Adek',
  phoneNumber: '+2348100000001',
  email: 'wrong-email.com', // note: email does not meet requirement in the formSchema, email field (isEmail: true) 
  password: '12345678'
}

// Note: the keys in userForm corresponds to the fields in the schema object above e.g email correspond to Schema.form.Email.field

const validationResult = Form.validateFields('signup', formSchema, userForm);

if (validationResult.error) {
    console.log(validationResult); // result is shown below
}

// validation result sample
"error": {
  "error": true,
  "Stacktrace": "ValidationError",
  "metadata": [
    {
      "statusCode": 400,
      "field": "email",
      "target": "SIGNUP"
    }
  ],
  "message": "email is invalid. Email should look like e.g example@mail.com",
  "details": {
  "error": true,
  "operationStatus": "Processs Terminated!"
  }
}

APIs

The following is a table of the options you can use in your schema for validation requirement.

OptionsValues(DataTypes)Description
requiredtrue or false (Boolean)Set true value to ensure that the input field should not be empty and vice versa.
isEmailtrue or false (Boolean)This will ensure that the value entered is a valid email format. e.g not-email-format will not be accepted but email-format@gmail.com will be accepted. (Note: this does not validate valid email address, only deals with format)
isNametrue or false (Boolean)This ensures that special characters like (#,:,0-9,%, e.t.c) should not be included in a name field. Input field allows spaces, a comma, a dot between names, e.g Daniel, Adek, John Doe, but does not accept values like Daniel Adek2, or John? Doe!.
isPhoneNumbertrue or false (Boolean)Input field will accept only a valid phone number format. e.g +2348100000001 but does not accept +234-81-00-0-00-001 (Note: this does not validate valid number, only deals with format)
isIntegertrue or false (Boolean)Input field accepts integer value only.
range{ from: Number, to: Number } (Object)This ensures that the input value for this field is within the range specification in the formSchema
isDecimaltrue or false (Boolean)Input field accepts only decimal values.
isAlphatrue or false (Boolean)Input field only accept alphabets. It does not allow space or special characters.
minLengthNumberValidate the field and ensure that the characters it is not lesser than specifield minimum length.
maxLengthNumberValidate the field and ensure that the characters it is not more than specifield maximum length.
isArraytrue or false (Boolean)This only allows array as the value.
isObjecttrue or false (Boolean)Only allows object as value

License

The MIT License

Copyright © 2019 DanielAdek

0.0.11

4 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.3

4 years ago