Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

express-validation-map

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-validation-map - npm Package Compare versions

Comparing version 0.1.2 to 0.1.3

index.js

13

lib/methods.js

@@ -5,4 +5,10 @@ function isRequired(value) {

function isName(value){
console.log('ddd', value);
const regName = new RegExp('^[a-zа-я-]+$', 'i');
return regName.test(value);
}
function isEmail(value) {
const regEmail: RegExp = new RegExp('^[0-9a-z-_\\+.]+\\@([0-9a-z-]{2,}\\.)+[a-z]{2,}$', 'i');
const regEmail = new RegExp('^[0-9a-z-_\\+.]+\\@([0-9a-z-]{2,}\\.)+[a-z]{2,}$', 'i');
return regEmail.test(value);

@@ -12,3 +18,3 @@ }

function isInteger(value) {
return Number.isInteger(value);
return Number.isInteger(+value);
}

@@ -20,3 +26,4 @@

isEmail,
isInteger
isInteger,
isName
}

@@ -1,32 +0,30 @@

type _onCheckDataType = {
res: Response,
body: { [k:string]: string | number | boolean },
validationMap: IValidationMap
const IS_REQUIRED_FIELD_TEXT = 'This field is required';
const VALIDATION_ERROR = 'validation.error';
// HELPERS
function isEmpty(obj) { // simple check
return !obj || !Object.keys(obj).length;
}
// SCENATIO 1 IF BODY EMPTY
const _onCheckEmptyRequest = ({ res, body, validationMap }: _onCheckDataType) => {
let errors = {};
const fieldsValidation = Object.keys(validationMap || {});
if(isEmpty(body)) {
fieldsValidation.forEach(field => errors[field] = isRequiredText);
function responseErrorValidation({ config, validations }) {
return {
status: config && config.code || 422,
message: config && config.message || VALIDATION_ERROR,
errors: validations
}
if(!isEmpty(errors)){
return responseErrorValidation(res, { validations: errors });
}
}
const _onValidationBody = ({ data, validationMap, config }) => {
// check for empty validation map
if(!validationMap || isEmpty(validationMap)) return;
const requestData = data || {};
const _onValidationBody = ({ res, body, validationMap }: _onCheckDataType) => {
if(!validationMap || !body) return;
let errors = {}
const fieldsValidation = Object.keys(validationMap || {});
const fieldsValidation = Object.keys(validationMap);
fieldsValidation.forEach(field => {
const value = body[field] || '';
const value = requestData[field];

@@ -43,6 +41,3 @@ Object.keys(validationMap[field].methods).forEach(method => {

if(!isEmpty(errors)){
res.json(responseErrorValidation(res, { validations: errors }));
}
return errors;
}

@@ -52,56 +47,26 @@

// SCENATIO 3 IF BODY CONTAINS NOT USE FIELD
const _onCheckUnecessaryFieldsType = ({ res, body, validationMap }: _onCheckDataType) => {
let errors = {};
const fieldsValidation = Object.keys(validationMap || {});
if(!isEmpty(body)) {
Object.keys(body).forEach(item => !fieldsValidation.includes(item) ? errors[item] = isNotUseField.replace('{field}', item) : null)
}
if(!isEmpty(errors)){
return res.json(responseErrorValidation(res, { validations: errors }));
}
}
const onValidation = (validationMap) => async (req, res, next)=> {
const { body, query, params, method } = req;
const { params: paramsMap, query: queryMap, body: bodyMap, config } = validationMap;
// check params
const paramsErrors = _onValidationBody({ config, data: params, validationMap: paramsMap });
if(!isEmpty(paramsErrors)){
return res.json(responseErrorValidation({ config, validations: paramsErrors }));
}
const onValidation = (validationMap: IValidationMapFull) => async (req: Request, res: Response, next): Promise<any> => {
// check queries
const queryErrors = method === 'GET' && _onValidationBody({ config, data: query, validationMap: queryMap });
if(!isEmpty(queryErrors)){
return res.json(responseErrorValidation({ config, validations: queryErrors }));
}
const params = req.params;
// SCENATIO 1 IF BODY EMPTY
_onCheckEmptyRequest({ body: params, res, validationMap: validationMap.params });
// SCENATIO 2 VALIDATION
_onValidationBody({ res, body: params, validationMap: validationMap.params });
// SCENATIO 3 IF BODY CONTAINS NOT USE FIELD
_onCheckUnecessaryFieldsType({ res, body: params, validationMap: validationMap.params });
if(req.method === 'GET'){
const query = req.query;
// SCENATIO 1 IF BODY EMPTY
_onCheckEmptyRequest({ body: query, res, validationMap: validationMap.query });
// SCENATIO 2 VALIDATION
_onValidationBody({ res, body: req.query, validationMap: validationMap.query });
// SCENATIO 3 IF BODY CONTAINS NOT USE FIELD
_onCheckUnecessaryFieldsType({ res, body: req.query, validationMap: validationMap.query });
} else {
const body = isEmpty(req.body) ? false: JSON.parse(req.body);
// SCENATIO 1 IF BODY EMPTY
_onCheckEmptyRequest({ body, res, validationMap: validationMap.body });
// SCENATIO 2 VALIDATION
_onValidationBody({ res, body, validationMap: validationMap.body });
// SCENATIO 3 IF BODY CONTAINS NOT USE FIELD
_onCheckUnecessaryFieldsType({ res, body, validationMap: validationMap.body });
// check body
const bodyErrors = _onValidationBody({ config, data: isEmpty(body) ? false: JSON.parse(body), validationMap: bodyMap });
if(!isEmpty(bodyErrors)){
return res.json(responseErrorValidation({ config, validations: bodyErrors }));
}
next();

@@ -111,4 +76,3 @@ }

module.exports = {
onValidation
}
module.exports = onValidation;
{
"name": "express-validation-map",
"version": "0.1.2",
"version": "0.1.3",
"description": "",
"homepage": "https://github.com/Valentinishe/express-validation-map",
"main": "index.js",

@@ -6,0 +7,0 @@ "scripts": {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc