validations-forms
v1.0.15
Published
bookshop validations-forms
Downloads
8
Readme
Description
It is a library that aims to help with form validation in an easy way :)
Installation
npm i -S validations-forms
OR
npm install --save validations-forms
Getting Started
Types Of Languages
| name | Description | Default value | | --------------------------------------------------------------|-------------------------------------|----------------| | | error messages in spanish language | ✅ | | | error messages in english language | |
Types Of Validations
| name | Description | | --------------------------------------------------------------------|------------------------------------| | | Text data validation | | | Required data validation | | | Number data validation | | | Number and decimal data validation | | | Text and number data validation | | | Text omit accents data validation | | | Required data for combo | | | CURP data validation | | | Date data validation | | | Email data validation | | | RFC data validation | | | RFC KEY CODE data validation | | | POSTAL CODE data validation | | | PASSWORD data validation | | | VERIFY PASSWORD data validation | | | Special character validation | | | Special character validation |
Parameter description
| name | Description |
| ----------------------------------------------------------|-------------------------------------------------------------------------------------|
| | Input identifier |
| | Value to validate |
| | Type validation "ARRAY":[ "R", "T" ]
OR "STRING" : "T"
|
| | Title of the entry to validate |
| | personalized message |
| | expression regular, applies in case the validation is of type "CUSTOM_EXP"
|
Usage
Import / Require
// ES6+ example
import {
singleValidation,
multiValidation,
multiValidationErrors,
EXPRESSIONS
} from "validations-forms";
// ES5 example
const {
singleValidation,
multiValidation,
multiValidationErrors,
EXPRESSIONS
} = require( "validations-forms" );
No ES+
const validationsForm = require( "validations-forms" );
validationsForm.singleValidation( DATA );
validationsForm.multiValidation( DATA );
validationsForm.multiValidationErrors( DATA );
validationsForm.EXPRESSIONS
Usage
Expression Regurar
import { EXPRESSIONS } from "validations-forms";
const { CURP, DATE, EMAIL, N, POSTAL_CODE, RFC, RFC_KEY_CODE, SPECIAL_CHARACTER, T, TN } = EXPRESSIONS;
console.log( EXPRESSIONS );
{
CURP : /^([A-Z][AEIOUX][A-Z]{2}\d{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01])[HM](?:AS|B[CS]|C[CLMSH]|D[FG]|G[TR]|HG|JC|M[CNS]|N[ETL]|OC|PL|Q[TR]|S[PLR]|T[CSL]|VZ|YN|ZS)[B-DF-HJ-NP-TV-Z]{3}[A-Z\d])(\d)$/
DATE : /(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d/
EMAIL : /^[-\w.%+]{1,64}@(?:[A-Z0-9-]{1,63}\.){1,125}[A-Z]{2,63}$/i
N : /^([0-9])*$/
ND : /^([0-9 .])*$/,
POSTAL_CODE : /(^([0-9]{5,5})|^)$/
RFC : /^([a-zñA-ZÑ&]{3,4}) ?(?:- ?)?(\d{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01]))$/
RFC_KEY_CODE : /^([a-zñA-ZÑ&]{3,4}) ?(?:- ?)?(\d{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01])) ?(?:- ?)?([a-zA-Z\d]{2})([A-ZÑa-zñ\d])$/
SPECIAL_CHARACTER : /^([a-z ñáäéëíïóöúüÑÁÄÉËÍÏÓÖÚÜ A-Z0-9 ‘,\-:;\\#/¿()´¨&"_*.%¿?$¡!@+¨]*$)|(\n[a-z ñáäéëíïóöúüÑÁÄÉËÍÏÓÖÚÜ A-Z0-9 ‘,\-:;\\#/()´¨&"_*.%¿?$¡!@+¨]{1,}$)/
T : /^([a-zA-Z ñáäéëíïóöúüÑÁÄÉËÍÏÓÖÚÜ .,]{0,100})$/
TN : /^[a-z ñáäéëíïóöúüÑÁÄÉËÍÏÓÖÚÜ A-Z0-9 .,]*$/
TN_OMIT_ACCENTS : /^[a-zA-Z0-9]*$/,
}
Example function singleValidation
import { singleValidation } from "validations-forms";
a single validation on an input
console.log( singleValidation( {
id : "id_input",
title : "input",
type : "T", // OR ["T"] <=== a single validation on an input
value : "text",
} ), "EN" )
{ status: true }
more than one validation on the same input
console.log( singleValidation( {
id : "id_input",
title : "TITULO UNO",
type : [ "R", "T" ], // <=== more than one validation on the same input,
value : "text",
} ), "EN" )
{ status: true }
Errors
console.log( singleValidation( {
id : "id_input",
title : "input",
type : [ "T" ],
value : 345,
} ), "EN" )
{
id : "id_input"
status : false
message : "The data input is not valid, please enter only letters"
}
console.log( singleValidation( {
id : "id_input",
title : "input",
type : "R",
value : "", // null OR undefined
} ),"EN" )
{
id : "id_input"
status : false
message : "The data input is required"
}
Example function multiValidation
import { multiValidation } from "validations-forms";
Datas
const DATA = [
{
id : "id_input_one",
title : "input_one",
type : [ "R", "T" ],
value : "texto",
},
{
id : "id_input_two",
title : "input_two",
type : "N",
value : 2323
}
{ ... }
];
const DATA_ERRORS = [
{
id : "id_input_one",
title : "input_one",
type : [ "R", "T" ],
value : 3434,
},
{
id : "id_input_two",
title : "input_two",
type : "T",
value : 12345,
},
{
id : "id_input_three",
title : "input_three",
type : [ "R" ],
value : null,
},
{
id : "id_input_four",
title : "input_four",
type : [ "TN" ],
value : "123abcABC..*",
},
{ ... }
];
console.log( multiValidation( DATA, "EN" ) )
{ status: true }
console.log( multiValidation( DATA_ERRORS, "EN" ) )
{
id : "id_input_one"
status : false
message : "The data input_one is not valid, please enter only letters"
}
Example function multiValidationErrors
import { multiValidationErrors } from "validations-forms";
Datas
const DATA = [
{
id : "id_input_one",
title : "input_one",
type : [ "R", "T" ],
value : "texto",
},
{
id : "id_input_two",
title : "input_two",
type : "N",
value : 2323
}
{ ... }
];
const DATA_ERRORS = [
{
id : "id_input_one",
title : "input_one",
type : [ "R", "T" ],
value : 3434,
},
{
id : "id_input_two",
title : "input_two",
type : "T",
value : 12345,
},
{
id : "id_input_three",
title : "input_three",
type : [ "R" ],
value : null,
},
{
id : "id_input_four",
title : "input_four",
type : [ "TN" ],
value : "123abcABC..*",
},
{ ... }
];
console.log( multiValidationErrors( DATA, "EN" ) )
{ status: true }
console.log( multiValidationErrors( DATA_ERRORS,"EN" ) )
{
status : false,
errors : [
{
id : "id_input_one",
status : false
message : "custom message",
},
{
id : "id_input_two",
status : false
message : "The data input_two is not valid, please enter only letters",
},
{
id : "id_input_three",
status : false
message : "The data input_three is required.",
},
{
id : "id_input_four",
status : false
message : "The data input_four is not valid, please enter only letters and numbers",
}
]
}
Example function type language
EN
console.log( singleValidation( {
id : "id_input",
title : "input",
type : [ "T" ],
value : 345,
} ), "EN" ) // <=== parameter language
{
id : "id_input"
status : false
message : "The data input is not valid, please enter only letters"
}
ES
console.log( singleValidation( {
id : "id_input",
title : "input",
type : [ "T" ],
value : 345,
} ), "ES" ) // <=== parameter language
OR
console.log( singleValidation( {
id : "id_input",
title : "input",
type : [ "T" ],
value : 345,
} ) )
Validation type "CUSTOM_EXP"
console.log( multiValidationErrors( [
{
id : 'one',
title : 'input one',
type : "CUSTOM_EXP",
value : "abc",
expression : /[0-9]/
},
{
id : 'two',
title : 'input one',
type : ["R","CUSTOM_EXP"],
value : "abc",
expression : ""
}
], "EN" )
{
"status": false,
"errors": [
{
"id" : "one",
"status" : false
"message" : "The data input one is not valid",
},
{
"id" : "two"
"status" : false,
"message" : "undefined regular expression :("
}
]
}
Example message custom
> To define a custom message you have to add the word "message" + the type of validation
Example:
{
id : 'one',
title : 'input one',
type : [ "R","T" ],
value : "",
}
{
id : 'two',
title : 'input two',
type : [ "R", "T" ],
value : "",
}
var T = "T";
"messageR" = mesage<string> + type<string>
"messageT" = `message${T}`
Result:
{
id : 'one',
title : 'input one',
type : [ "R", "T" ],
value : "",
messageR : "Message custom validation R"
messageT : "Message custom validation T"
}
> you can define the custom message to the validations you want
{
id : 'two',
title : 'input one',
type : [ "R", "T" ],
value : "",
messageT : "Message custom validation T"
}
Practical example
const DATA_ERRORS = [
{
id : 'one',
title : 'input one',
type : [ "R", "T" ],
value : "",
messageT : "Message custom T"
},
{
id : 'two',
title : 'input two',
type : [ "R", "T" ],
value : "",
messageR :"Message custom R",
messageT : "Message custom T"
},
{
id : 'three',
title : 'input three',
type : [ "R", "TN" ],
value : "54--",
messageTN : "Message custom TN"
},
{
id : 'four',
title : 'input four',
type : [ "R", "N" ],
value : "abc",
messageR : "Message custom R",
messageN : "Message custom N"
},
{
id : 'five',
title : 'input five',
type : [ "R", "N" ],
value : "welcome",
},
{ ... }
];
console.log( multiValidationErrors( DATA_ERRORS ), "EN" ) // <=== parameter language
{
"status" : false,
"errors" : [
{
"id" : "one",
"message" : "The data input one is required.",
"status" : false
},
{
"id" : "two",
"message" : "Message custom R",
"status" : false
},
{
"id" : "three",
"message" : "Message custom TN",
"status" : false
},
{
"id" : "four",
"message" : "Message custom N",
"status" : false
},
{
"id" : "five",
"message" : "The data input five is not valid, please enter only numbers",
"status" : false
}
]
}
Example validation type password
EN
console.log( singleValidation( {
id: 'passwor',
title: 'password',
messagePASSWORD: "Message custom PASSWORD",
type: ["R","PSWD"],
value: ".",
},), "EN" ) // <=== parameter language
validatios password
- At least one lowercase letter
- At least one uppercase letter
- At least one digit
- At least one special character
- Minimum 8 in length
- Maximum 8 in length
{
"id": "passwor",
"message": "Message custom PASSWORD",
"status": false,
"errors": {
"PASSWORD_LOWERCASE": {
"id": "passwor",
"message": "At least one lowercase letter",
"status": false,
"type": "PASSWORD_LOWERCASE"
},
"PASSWORD_UPPERCASE": {
"id": "passwor",
"message": "At least one uppercase letter",
"status": false,
"type": "PASSWORD_UPPERCASE"
},
"PASSWORD_DIGIT": {
"id": "passwor",
"message": "At least one digit",
"status": false,
"type": "PASSWORD_DIGIT"
},
"PASSWORD_SPECIAL_CHARACTER": {
"id": "passwor",
"message": "At least one special character $@$!%*?&",
"status": false,
"type": "PASSWORD_SPECIAL_CHARACTER"
},
"PASSWORD_MIN": {
"id": "passwor",
"message": "Minimum 8 in length",
"status": false,
"type": "PASSWORD_MIN"
},
"PASSWORD_MAX": {
"id": "passwor",
"message": "Maximum 15 in length",
"status": false,
"type": "PASSWORD_MAX"
}
}
}
Example validation verify password
EN
console.log( singleValidation({
id: 'passwor-one',
type: ['R','PSWD_VERIFY'],
value: '12345',
valueCompare:'6789'
}), "EN" ) // <=== parameter language
{
"id": "passwor-one",
"message": "Those passwords didn’t match.",
"status": false
}
Contributors
License MIT
MIT License
Copyright (c) 2021 Ferch01992
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.