@vitrical/utils
Advanced tools
Comparing version 1.0.1 to 1.0.2
{ | ||
"name": "@vitrical/utils", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Collection of useful functions and typings", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -86,2 +86,3 @@ "use strict"; | ||
.filter(Boolean); | ||
var extraErrors = []; | ||
var schemaErrors = Object.entries(schema) | ||
@@ -98,3 +99,6 @@ .map(function (_a) { | ||
} | ||
validateObject(validationSchema, value); | ||
var foundErrors = validateObject(validationSchema, value); | ||
if (foundErrors.length !== 0) { | ||
extraErrors.push.apply(extraErrors, foundErrors); | ||
} | ||
return null; | ||
@@ -170,8 +174,6 @@ } | ||
.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); }))); | ||
} | ||
var errors = __spreadArray(__spreadArray(__spreadArray([], bodyErrors, true), schemaErrors, true), extraErrors, true); | ||
return errors; | ||
} | ||
exports.validateObject = validateObject; | ||
//# sourceMappingURL=validation.js.map |
@@ -97,4 +97,8 @@ export const objectHasProperty = ( | ||
} | ||
// Validates the request body | ||
export function validateObject(schema: ValidateObjectOptions, object: { [key: string]: any }) { | ||
/* Returns a list of errors if the object does not match the schema */ | ||
export function validateObject( | ||
schema: ValidateObjectOptions, | ||
object: { [key: string]: any } | ||
): string[] { | ||
const bodyErrors = Object.keys(object) | ||
@@ -109,3 +113,5 @@ .map((key) => { | ||
const schemaErrors = Object.entries(schema) | ||
const extraErrors: string[] = [] | ||
const schemaErrors: string[] = Object.entries(schema) | ||
.map(([key, validationSchema]) => { | ||
@@ -121,3 +127,6 @@ const value = object[key] | ||
} | ||
validateObject(validationSchema, value) | ||
const foundErrors = validateObject(validationSchema, value) | ||
if (foundErrors.length !== 0) { | ||
extraErrors.push(...foundErrors) | ||
} | ||
return null | ||
@@ -210,6 +219,4 @@ } | ||
const errors = [...bodyErrors, ...schemaErrors] | ||
if (errors.length !== 0) { | ||
throw new Error(`Object validation failed:\n ${errors.reduce((a, b) => `${a}\n ${b}`)}`) | ||
} | ||
const errors: string[] = [...bodyErrors, ...schemaErrors, ...extraErrors] | ||
return errors | ||
} |
Sorry, the diff of this file is not supported yet
42555
769