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

@vitrical/utils

Package Overview
Dependencies
Maintainers
1
Versions
79
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@vitrical/utils - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

24

express.ts

@@ -14,3 +14,3 @@ import { Request, Response, NextFunction } from 'express'

msg: 'Unknown Error',
err,
data: err,
})

@@ -33,13 +33,15 @@ return

export const requireApiKey = (req: Request, res: Response, next: NextFunction): void => {
try {
if (!process.env.API_KEY) console.error('Missing API_KEY env variable')
if (req.headers['key'] !== process.env.API_KEY) {
res.status(403).send({ msg: 'Invalid API Key' })
return
export const requireApiKey =
(apiKey: string) =>
(req: Request, res: Response, next: NextFunction): void => {
try {
if (!apiKey) console.error('Missing apiKey variable')
if (req.headers['key'] !== apiKey) {
res.status(403).send({ msg: 'Invalid API Key' })
return
}
return next()
} catch (err) {
next(err)
}
return next()
} catch (err) {
next(err)
}
}
{
"name": "@vitrical/utils",
"version": "1.0.0",
"version": "1.0.1",
"description": "Collection of useful functions and typings",

@@ -8,3 +8,3 @@ "main": "index.js",

"build": "tsc --project tsconfig.json",
"test": "ts-node api.ts"
"test": "ts-node validation.ts"
},

@@ -11,0 +11,0 @@ "repository": {

"use strict";
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
exports.__esModule = true;
exports.getSeason = exports.capitalize = exports.isValidNAPhoneNumber = exports.formatPhoneNumber = exports.isValidEmail = exports.removeSpaces = exports.removeSpecialCharacters = exports.isApiError = exports.objectHasProperty = void 0;
exports.validateObject = exports.getSeason = exports.capitalize = exports.isValidNAPhoneNumber = exports.formatPhoneNumber = exports.isValidEmail = exports.removeSpaces = exports.removeSpecialCharacters = exports.isApiError = exports.objectHasProperty = void 0;
var objectHasProperty = function (obj, property) {

@@ -68,2 +77,99 @@ return !!obj && typeof obj === 'object' && property in obj;

exports.getSeason = getSeason;
function validateObject(schema, object) {
var bodyErrors = Object.keys(object)
.map(function (key) {
if (!(key in schema)) {
return "Unexpected property \"".concat(key, ": ").concat(object[key], "\"");
}
return null;
})
.filter(Boolean);
var schemaErrors = Object.entries(schema)
.map(function (_a) {
var key = _a[0], validationSchema = _a[1];
var value = object[key];
if (typeof validationSchema === 'object' && !Array.isArray(validationSchema)) {
if (typeof value === 'undefined') {
return "Expected property \"".concat(key, ": typeof object\" does not exist on object");
}
if (typeof value !== 'object' || Array.isArray(value)) {
return "Expected object but got ".concat(typeof value, " ").concat(value, " from property \"").concat(key, "\"");
}
validateObject(validationSchema, value);
return null;
}
var validationType = typeof validationSchema === 'string' ? validationSchema : validationSchema[0];
if (['number?', 'string?', 'array?', 'bigint?', 'boolean?', 'object?'].includes(validationType) &&
value === undefined) {
return null;
}
if (!(key in object)) {
return "Expected property \"".concat(key, ": typeof ").concat(schema[key], "\" does not exist on object");
}
switch (validationType) {
case 'array':
if (!Array.isArray(value)) {
return "Expected ".concat(validationType, " but got ").concat(typeof value, " ").concat(value, " from property \"").concat(key, "\"");
}
break;
case 'array?':
if (!Array.isArray(value)) {
return "Expected ".concat(validationType, " but got ").concat(typeof value, " ").concat(value, " from property \"").concat(key, "\"");
}
break;
case 'bigint?':
if (typeof value !== 'bigint') {
return "Expected ".concat(validationType, " but got ").concat(typeof value, " ").concat(value, " from property \"").concat(key, "\"");
}
break;
case 'boolean?':
if (typeof value !== 'boolean') {
return "Expected ".concat(validationType, " but got ").concat(typeof value, " ").concat(value, " from property \"").concat(key, "\"");
}
break;
case 'number?':
if (typeof value !== 'number') {
return "Expected ".concat(validationType, " but got ").concat(typeof value, " ").concat(value, " from property \"").concat(key, "\"");
}
break;
case 'object?':
if (typeof value !== 'object') {
return "Expected ".concat(validationType, " but got ").concat(typeof value, " ").concat(value, " from property \"").concat(key, "\"");
}
break;
case 'string?':
if (typeof value !== 'string') {
return "Expected ".concat(validationType, " but got ").concat(typeof value, " ").concat(value, " from property \"").concat(key, "\"");
}
break;
default:
if (typeof value !== validationType) {
return "Expected ".concat(validationType, " but got ").concat(typeof value, " ").concat(value, " from property \"").concat(key, "\"");
}
break;
}
if (typeof validationSchema !== 'string') {
var validationMin = validationSchema[1];
var validationMax = validationSchema[2];
if (typeof value === 'string' &&
(value.length < validationMin || value.length > validationMax)) {
return "Body property \"".concat(key, "\" must be within ").concat(validationMin, "-").concat(validationMax, " characters but recieved ").concat(value.length);
}
if (Array.isArray(value) &&
(value.length < validationMin || value.length > validationMax)) {
return "Body property \"".concat(key, "\" must be within ").concat(validationMin, "-").concat(validationMax, " length but got ").concat(value.length);
}
if (typeof value === 'number' && (value < validationMin || value > validationMax)) {
return "Body property \"".concat(key, "\" must be within the range ").concat(validationMin, "-").concat(validationMax, " but got ").concat(value);
}
}
return null;
})
.filter(Boolean);
var errors = __spreadArray(__spreadArray([], bodyErrors, true), schemaErrors, true);
if (errors.length !== 0) {
throw new Error("Object validation failed:\n ".concat(errors.reduce(function (a, b) { return "".concat(a, "\n ").concat(b); })));
}
}
exports.validateObject = validateObject;
//# sourceMappingURL=validation.js.map

@@ -81,1 +81,132 @@ export const objectHasProperty = (

}
type ValidationLength = 'number' | 'string' | 'array' | 'number?' | 'string?' | 'array?'
type ValidationRegular =
| 'bigint'
| 'boolean'
| 'object'
| 'undefined'
| 'bigint?'
| 'boolean?'
| 'object?'
type MaxLength = number
type MinLength = number
type SchemaInput = [ValidationLength, MinLength, MaxLength] | ValidationRegular | ValidationLength
export type ValidateObjectOptions = {
[key: string]: SchemaInput | ValidateObjectOptions
}
// Validates the request body
export function validateObject(schema: ValidateObjectOptions, object: { [key: string]: any }) {
const bodyErrors = Object.keys(object)
.map((key) => {
if (!(key in schema)) {
return `Unexpected property "${key}: ${object[key]}"`
}
return null
})
.filter(Boolean)
const schemaErrors = Object.entries(schema)
.map(([key, validationSchema]) => {
const value = object[key]
if (typeof validationSchema === 'object' && !Array.isArray(validationSchema)) {
if (typeof value === 'undefined') {
return `Expected property "${key}: typeof object" does not exist on object`
}
if (typeof value !== 'object' || Array.isArray(value)) {
return `Expected object but got ${typeof value} ${value} from property "${key}"`
}
validateObject(validationSchema, value)
return null
}
const validationType =
typeof validationSchema === 'string' ? validationSchema : validationSchema[0]
if (
['number?', 'string?', 'array?', 'bigint?', 'boolean?', 'object?'].includes(
validationType
) &&
value === undefined
) {
return null
}
if (!(key in object)) {
return `Expected property "${key}: typeof ${schema[key]}" does not exist on object`
}
switch (validationType) {
case 'array':
if (!Array.isArray(value)) {
return `Expected ${validationType} but got ${typeof value} ${value} from property "${key}"`
}
break
case 'array?':
if (!Array.isArray(value)) {
return `Expected ${validationType} but got ${typeof value} ${value} from property "${key}"`
}
break
case 'bigint?':
if (typeof value !== 'bigint') {
return `Expected ${validationType} but got ${typeof value} ${value} from property "${key}"`
}
break
case 'boolean?':
if (typeof value !== 'boolean') {
return `Expected ${validationType} but got ${typeof value} ${value} from property "${key}"`
}
break
case 'number?':
if (typeof value !== 'number') {
return `Expected ${validationType} but got ${typeof value} ${value} from property "${key}"`
}
break
case 'object?':
if (typeof value !== 'object') {
return `Expected ${validationType} but got ${typeof value} ${value} from property "${key}"`
}
break
case 'string?':
if (typeof value !== 'string') {
return `Expected ${validationType} but got ${typeof value} ${value} from property "${key}"`
}
break
default:
if (typeof value !== validationType) {
return `Expected ${validationType} but got ${typeof value} ${value} from property "${key}"`
}
break
}
if (typeof validationSchema !== 'string') {
const validationMin = validationSchema[1]
const validationMax = validationSchema[2]
if (
typeof value === 'string' &&
(value.length < validationMin || value.length > validationMax)
) {
return `Body property "${key}" must be within ${validationMin}-${validationMax} characters but recieved ${value.length}`
}
if (
Array.isArray(value) &&
(value.length < validationMin || value.length > validationMax)
) {
return `Body property "${key}" must be within ${validationMin}-${validationMax} length but got ${value.length}`
}
if (typeof value === 'number' && (value < validationMin || value > validationMax)) {
return `Body property "${key}" must be within the range ${validationMin}-${validationMax} but got ${value}`
}
}
return null
})
.filter(Boolean)
const errors = [...bodyErrors, ...schemaErrors]
if (errors.length !== 0) {
throw new Error(`Object validation failed:\n ${errors.reduce((a, b) => `${a}\n ${b}`)}`)
}
}

Sorry, the diff of this file is not supported yet

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