From 47d5a385082fdf7e12cbfbb0d31a96579237e08e Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Sun, 16 Feb 2020 23:27:59 +0100 Subject: [PATCH] Add isPlainObject validator --- index.js | 12 ++++++++++-- package.json | 3 ++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 7654919..87fe4c0 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,7 @@ const createError = require("create-error"); const assureArray = require("assure-array"); const defaultValue = require("default-value"); +const isPlainObj = require("is-plain-obj"); const util = require("util"); let ValidationError = createError("ValidationError", { path: [] }); @@ -136,7 +137,13 @@ function aggregrateErrors(errors) { function validateArguments(args, rules) { let errors = validateArgumentList(args, rules); - aggregrateErrors(errors); + aggregrateErrors(errors); +} + +function isPlainObject(value) { + if (!isPlainObj(value)) { + throw new ValidationError("Must be a plain object (eg. object literal)"); + } } module.exports = { @@ -148,7 +155,7 @@ module.exports = { }, validateOptions: function (args, optionsRules) { return validateArguments(args, [ - ["options", optionsRules] + ["options", isPlainObject].concat(assureArray(optionsRules)) ]); }, ValidationError: ValidationError, @@ -183,6 +190,7 @@ module.exports = { throw new ValidationError("Must be a Buffer object"); } }, + isPlainObject: isPlainObject, either: function (...alternatives) { if (alternatives.length === 0) { throw new Error("Must specify at least one alternative"); diff --git a/package.json b/package.json index 72f1a88..e937ca1 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "dependencies": { "assure-array": "^1.0.0", "create-error": "^0.3.1", - "default-value": "^1.0.0" + "default-value": "^1.0.0", + "is-plain-obj": "^2.0.0" } }