form-my-simple-validation
v0.0.11
Published
[](https://travis-ci.com/DanielAdek/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.
|Params | Data-Types | Description
-------------------|-------------------|--------------
formType (required) | String | The type of form you want to perform validation on. (Note: The form type should be written as is, in the formSchema)|
formSchema (required) | Object | The schema for validating your form |
userForm (required) | Object | User form i.e an object containing form details, with the keys corresponding to the fields specified in your schema |
customError (optional) | String or object | The 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) | Boolean | When 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 [email protected]",
"details": {
"error": true,
"operationStatus": "Processs Terminated!"
}
}
APIs
The following is a table of the options you can use in your schema for validation requirement.
| Options | Values(DataTypes) | Description
------------------------ | --------------------- | -------------------
| required | true or false (Boolean) | Set true value to ensure that the input field should not be empty and vice versa. |
| isEmail | true 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 protected] will be accepted. (Note: this does not validate valid email address, only deals with format) |
| isName | true 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!. |
| isPhoneNumber | true 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) |
| isInteger | true 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 |
| isDecimal | true or false (Boolean)| Input field accepts only decimal values.
| isAlpha | true or false (Boolean) | Input field only accept alphabets. It does not allow space or special characters. |
| minLength | Number | Validate the field and ensure that the characters it is not lesser than specifield minimum length. |
| maxLength | Number | Validate the field and ensure that the characters it is not more than specifield maximum length. |
| isArray | true or false (Boolean) | This only allows array as the value. |
| isObject | true or false (Boolean) | Only allows object as value |
License
Copyright © 2019 DanielAdek
