├── .npmignore ├── conf.json ├── .gitignore ├── .travis.yml ├── spec ├── random.js └── unit │ └── singleton-spec.js ├── lib ├── preconditions.js ├── constants.js ├── validator │ └── validate.js ├── errr │ ├── decorator.js │ └── validator.js └── err │ ├── instance-validator.js │ └── singleton-validator.js ├── package.json └── .eslintrc /.npmignore: -------------------------------------------------------------------------------- 1 | spec 2 | node_modules 3 | *.idea 4 | build 5 | coverage 6 | .grunt 7 | *.log -------------------------------------------------------------------------------- /conf.json: -------------------------------------------------------------------------------- 1 | { 2 | "source": { 3 | "include": ["./lib/errr/errr-validation.js"] 4 | } 5 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.idea 3 | build 4 | coverage 5 | .grunt 6 | *.log 7 | *.iml 8 | package-lock.json -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | os: 3 | - linux 4 | dist: xenial 5 | node_js: 6 | - lts/* 7 | - 16.0 8 | - 14.0 9 | - 12.0 10 | notifications: 11 | webhooks: 12 | urls: 13 | - https://webhooks.gitter.im/e/c27fddacc09aaa988b05 14 | on_success: change # options: [always|never|change] default: always 15 | on_failure: always # options: [always|never|change] default: always 16 | on_start: never # options: [always|never|change] default: always 17 | -------------------------------------------------------------------------------- /spec/random.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const Chance = require("chance"); 4 | 5 | let chance = new Chance(); 6 | 7 | module.exports = { 8 | uniqueId: function () { 9 | return chance.hash({ length: 24 }); 10 | }, 11 | zip: function () { 12 | return chance.zip(); 13 | }, 14 | firstName: function () { 15 | return chance.first(); 16 | }, 17 | lastName: function () { 18 | return chance.last(); 19 | }, 20 | word: function (len) { 21 | return chance.word({ length: len || 5 }); 22 | }, 23 | sentence: function (len) { 24 | return chance.sentence({ words: len || 5 }); 25 | }, 26 | date: function () { 27 | return chance.date(); 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /lib/preconditions.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var InstanceValidator = require("./err/instance-validator"), 4 | SingletonValidator = require("./err/singleton-validator"), 5 | ErrrValidator = require("./errr/validator"); 6 | 7 | /** 8 | * Preconditions entry point interface. 9 | * 10 | * @class 11 | */ 12 | class Preconditions { 13 | /** 14 | * Validate single value with the buildable errr interface from the static errr Validation functionality. 15 | * 16 | * @returns Error Validation Singleton. 17 | */ 18 | 19 | static errr() { 20 | return ErrrValidator; 21 | } 22 | 23 | /** 24 | * Validate single value with the chainable interface from the Error Validation Singleton. 25 | * 26 | * @returns Error Validation Singleton. 27 | */ 28 | static singleton() { 29 | return SingletonValidator; 30 | } 31 | 32 | /** 33 | * @warning This functionality has very poor performance. Please use the 'singleton' or 'errr' functionality instead. 34 | * 35 | * Validate values of a given JSON object with the preconditions object. 36 | * @param objectUnderTest - Object Under Test 37 | * @returns Error Validation instance. 38 | */ 39 | static instance(objectUnderTest) { 40 | return new InstanceValidator(objectUnderTest); 41 | } 42 | 43 | /** 44 | * Gives ability to extend and add other preconditions to the Error Validation constructor. 45 | * 46 | * @Warning This functionality only works with the 'instance' function which has very poor performance. 47 | * @returns Error Validation constructor. 48 | */ 49 | static constructor() { 50 | return InstanceValidator; 51 | } 52 | } 53 | 54 | module.exports = Preconditions; 55 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "preconditions", 3 | "author": "Cory Parrish", 4 | "main": "./lib/preconditions.js", 5 | "version": "3.0.2", 6 | "description": "Support for Precondition error checking in Node.js.", 7 | "homepage": "https://github.com/corybill/preconditions", 8 | "bugs": { 9 | "url": "https://github.com/corybill/preconditions/issues" 10 | }, 11 | "scripts": { 12 | "test": "npm run ut && npm run lint", 13 | "ut": "./node_modules/mocha/bin/_mocha ./spec/unit", 14 | "perf": "env perf=true env test=false maddox ./spec/unit", 15 | "uap": "env perf=true test=true maddox ./spec/unit", 16 | "coverage": "./bin/maddox-cov.js", 17 | "lint": "./node_modules/eslint/bin/eslint.js ./spec/ ./lib", 18 | "docs": " ./node_modules/jsdoc-to-markdown/bin/cli.js \"./lib/**/*.js\" > api.md" 19 | }, 20 | "keywords": [ 21 | "StriveNine", 22 | "Strive9", 23 | "preconditions", 24 | "Guava", 25 | "Throw Error", 26 | "Errr", 27 | "Error", 28 | "Error Factory", 29 | "Error Builder", 30 | "Stack Trace", 31 | "Appendable stack trace", 32 | "append stack trace", 33 | "concat strack trace", 34 | "concatenate strack trace", 35 | "Appendable error", 36 | "append error", 37 | "concat error", 38 | "concatenate error", 39 | "Debug Params", 40 | "Promise", 41 | "Promise Chain", 42 | "errr" 43 | ], 44 | "license": "MIT", 45 | "repository": { 46 | "type": "git", 47 | "url": "https://github.com/corybill/preconditions.git" 48 | }, 49 | "dependencies": { 50 | "core-util-is": "1.x", 51 | "errr": "2.x" 52 | }, 53 | "devDependencies": { 54 | "chai": "3.x", 55 | "chance": "1.x", 56 | "eslint": "7.x", 57 | "jsdoc-to-markdown": "7.x", 58 | "maddox": "2.x", 59 | "mocha": "9.x" 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /lib/constants.js: -------------------------------------------------------------------------------- 1 | exports.ShouldBeDefined = "Variable should be defined."; 2 | exports.ShouldBeUndefined = "Variable should be undefined."; 3 | 4 | exports.ShouldBeArray = "Variable should be of type Array."; 5 | exports.ShouldNotBeArray = "Variable should NOT be of type Array."; 6 | 7 | exports.ShouldBeObject = "Variable should be of type Object."; 8 | exports.ShouldNotBeObject = "Variable should NOT be of type Object."; 9 | 10 | exports.ShouldBeEmpty = "Array or object should be empty."; 11 | exports.ShouldNotBeEmpty = "Array or object should NOT be empty."; 12 | 13 | exports.ShouldBeFunction = "Variable should be a Function."; 14 | exports.ShouldNotBeFunction = "Variable should NOT be a Function."; 15 | 16 | exports.ShouldBeString = "Variable should be a String."; 17 | exports.ShouldNotBeString = "Variable should NOT be a String."; 18 | 19 | exports.ShouldBeNumber = "Variable should be a Number."; 20 | exports.ShouldNotBeNumber = "Variable should NOT be a Number."; 21 | 22 | exports.ShouldBeFinite = "Variable should be Finite (i.e. not infinity)."; 23 | exports.ShouldBeInfinite = "Variable should be Infinite."; 24 | 25 | exports.ShouldBeBoolean = "Variable should be a Boolean."; 26 | exports.ShouldNotBeBoolean = "Variable should NOT be a Boolean."; 27 | 28 | exports.ShouldBeDate = "Variable should be a Date."; 29 | exports.ShouldNotBeDate = "Variable should NOT be a Date."; 30 | 31 | exports.ShouldBeRegExp = "Variable should be a RegExp."; 32 | exports.ShouldNotBeRegExp = "Variable should NOT be a RegExp."; 33 | 34 | exports.ShouldBeFalsey = "Variable should be falsey."; 35 | exports.ShouldNotBeFalsey = "Variable should NOT be falsey."; 36 | 37 | exports.IllegalArgument = "Illegal Argument."; 38 | exports.IllegalState = "Illegal State."; 39 | 40 | exports.ShouldHaveValidIndex = "Index should be between between 0 (inclusive) and size (exclusive)."; 41 | exports.ShouldHaveValidPosition = "Index should be between index between 0 (inclusive) and size (inclusive)."; 42 | exports.ShouldHaveValidPositions = "Start and End should be between valid sub range between 0 (inclusive) and size (inclusive)."; 43 | exports.StartBeforeEnd = "Start value should be less than the end value."; 44 | -------------------------------------------------------------------------------- /lib/validator/validate.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var CoreUtilIs = require("core-util-is"); 4 | 5 | var validate = { 6 | shouldBeDefined: function (val) { 7 | return CoreUtilIs.isUndefined(val); 8 | }, 9 | shouldBeUndefined: function (val) { 10 | return !CoreUtilIs.isUndefined(val); 11 | }, 12 | 13 | shouldBeArray: function (val) { 14 | return !CoreUtilIs.isArray(val); 15 | }, 16 | shouldNotBeArray: function (val) { 17 | return CoreUtilIs.isArray(val); 18 | }, 19 | 20 | shouldBeObject: function (val) { 21 | return !(val !== null && (typeof val === "object" || typeof val === "function")); 22 | }, 23 | shouldNotBeObject: function (val) { 24 | return (val !== null && (typeof val === "object" || typeof val === "function")); 25 | }, 26 | 27 | shouldBeEmpty: function (val) { 28 | return !CoreUtilIs.isEmpty(val); 29 | }, 30 | shouldNotBeEmpty: function (val) { 31 | return CoreUtilIs.isEmpty(val); 32 | }, 33 | 34 | shouldBeFunction: function (val) { 35 | return !CoreUtilIs.isFunction(val); 36 | }, 37 | shouldNotBeFunction: function (val) { 38 | return CoreUtilIs.isFunction(val); 39 | }, 40 | 41 | shouldBeString: function (val) { 42 | return !CoreUtilIs.isString(val); 43 | }, 44 | shouldNotBeString: function (val) { 45 | return CoreUtilIs.isString(val); 46 | }, 47 | 48 | shouldBeNumber: function (val) { 49 | return !CoreUtilIs.isNumber(val); 50 | }, 51 | shouldNotBeNumber: function (val) { 52 | return CoreUtilIs.isNumber(val); 53 | }, 54 | 55 | shouldBeFinite: function (val) { 56 | return !CoreUtilIs.isFinite(val); 57 | }, 58 | shouldBeInfinite: function (val) { 59 | return CoreUtilIs.isFinite(val); 60 | }, 61 | 62 | shouldBeBoolean: function (val) { 63 | return !CoreUtilIs.isBoolean(val); 64 | }, 65 | shouldNotBeBoolean: function (val) { 66 | return CoreUtilIs.isBoolean(val); 67 | }, 68 | 69 | shouldBeDate: function (val) { 70 | return !CoreUtilIs.isDate(val); 71 | }, 72 | shouldNotBeDate: function (val) { 73 | return CoreUtilIs.isDate(val); 74 | }, 75 | 76 | shouldBeRegExp: function (val) { 77 | return !CoreUtilIs.isRegExp(val); 78 | }, 79 | shouldNotBeRegExp: function (val) { 80 | return CoreUtilIs.isRegExp(val); 81 | }, 82 | 83 | shouldBeFalsey: function (val) { 84 | return !!val; 85 | }, 86 | shouldNotBeFalsey: function (val) { 87 | return !val; 88 | }, 89 | 90 | checkArgument: function (val) { 91 | return !val; 92 | }, 93 | checkState: function (val) { 94 | return !val; 95 | }, 96 | 97 | checkElementIndex: function (index, size) { 98 | return (index < 0 || index >= size); 99 | }, 100 | checkPositionIndex: function (index, size) { 101 | return (index < 0 || index > size); 102 | }, 103 | checkPositionIndexes: function (start, end, size) { 104 | return ((end < start) || (start < 0 || end > size)); 105 | } 106 | }; 107 | 108 | module.exports = validate; 109 | -------------------------------------------------------------------------------- /lib/errr/decorator.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const Errr = require("errr"); 4 | 5 | /** 6 | * Error Builder allows you to use optional functions to build an error object. The error can have appended stack traces and debug params to assist with debugging. 7 | */ 8 | class ErrrDecorator { 9 | 10 | /** 11 | * Provides an interface to build an error. Then allows you to get or throw the error. 12 | * @class 13 | * 14 | * @param {String} [message] - Error message that will supplied to Error Object. 15 | * @param {Array} [template] - Array of parameters. If given, util.format(message, template) will be applied to the message string. 16 | */ 17 | constructor(message, templateParams, failsTest) { 18 | this._message_ = message; 19 | this._templateParams_ = templateParams; 20 | this._failsTest_ = failsTest; 21 | this._setValues_ = []; 22 | } 23 | 24 | /** 25 | * Decorated function from 'errr' module. Add parameters to the stack trace that will make it easier to debug the problem. 26 | * 27 | * @param {Object} params - Object Map of key value parameters that will make it easier to debug the error. 28 | * @param {Boolean} [shouldDebug] - If shouldDebug === false, then debug params will not print. Any other value (including undefined), and the debug params will be printed. Useful if you want to only print debugParams given an Environment Variable. 29 | * @returns {ErrrDecorator} - Returns the instance of errorBuilder to allow chainability. 30 | */ 31 | debug(debugParams, shouldDebug) { 32 | this._debugParams_ = debugParams; 33 | this._shouldDebug_ = shouldDebug; 34 | return this; 35 | } 36 | 37 | /** 38 | * Decorated function from 'errr' module. Sets an immutable value on the error object using the key as the variable name. 39 | * 40 | * @param {String} key - The key that will be used to set the value on the error object. 41 | * @param {Object} value - The value that will be set on the object. 42 | * @param {Boolean} [force] - If force equals true, then this value will override a value with the same key from an errr 43 | * passed in using the 'appendTo' function. 44 | * @returns {ErrrDecorator} - Returns the instance of errorBuilder to allow chainability. 45 | */ 46 | set(key, value) { 47 | this._setValues_.push({ key, value }); 48 | return this; 49 | } 50 | 51 | /** 52 | * Decorated function from 'errr' module. Same concept and functionality as the 'set' function. The difference is 53 | * that you can set all values in a given object onto the Errr instance. 54 | * 55 | * @param {String} key - The key that will be used to set the value on the error object. 56 | * @param {Object} value - The value that will be set on the object. 57 | * @param {Boolean} [force] - If force equals true, then this value will override a value with the same key from an errr 58 | * passed in using the 'appendTo' function. 59 | * @returns {ErrrDecorator} - Returns the instance of errorBuilder to allow chainability. 60 | */ 61 | setAll(object) { 62 | const keys = Object.keys(object); 63 | 64 | for (let key of keys) { 65 | this._setValues_.push({ key, value: object[key] }); 66 | } 67 | 68 | return this; 69 | } 70 | 71 | /** 72 | * Decorated function from 'errr' module. Append the error being built, to the end of this error's stack trace. 73 | * 74 | * @param {Error} err - The stack trace of the error being built, will be appended to this error's stack trace. 75 | * @returns {ErrrDecorator} - Returns the instance of errorBuilder to allow chainability. 76 | */ 77 | appendTo(err) { 78 | this._appendTo_ = err; 79 | return this; 80 | } 81 | 82 | /** 83 | * Validate preconditions check and throw an errr if it fails. 84 | */ 85 | test() { 86 | if (this._failsTest_()) { 87 | let errr = Errr.newError(this._message_, this._templateParams_) 88 | .debug(this._debugParams_, this._shouldDebug_) 89 | .appendTo(this._appendTo_); 90 | 91 | this._setValues_.forEach((item) => { 92 | errr.set(item.key, item.value); 93 | }); 94 | 95 | errr.throw(); 96 | } 97 | } 98 | 99 | /** 100 | * Synonym for the test function. 101 | */ 102 | t() { // eslint-disable-line 103 | this.test(); 104 | } 105 | } 106 | 107 | module.exports = ErrrDecorator; 108 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "rules": { 3 | // common 4 | "indent": [2, 2, {"SwitchCase": 1}], // specify tab or space width for your code 5 | "quotes": [2, "double"], // specify whether backticks, double or single quotes should be used 6 | "linebreak-style": [2, "unix"], // disallow mixed 'LF' and 'CRLF' as linebreaks 7 | "semi": [2, "always"], // require or disallow use of semicolons instead of ASI 8 | // errors 9 | "no-extra-parens": [2, "functions"], // disallow unnecessary parentheses 10 | "no-unexpected-multiline": [2], // Avoid code that looks like two expressions but is actually one 11 | "valid-jsdoc": [0], // Ensure JSDoc comments are valid 12 | // best practices 13 | "accessor-pairs": [0], // Enforces getter/setter pairs in objects 14 | "block-scoped-var": [2], // treat var statements as if they were block scoped 15 | "complexity": [0], // specify the maximum cyclomatic complexity allowed in a program 16 | "consistent-return": [2], // require return statements to either always or never specify values 17 | "curly": [2, "all"], // specify curly brace conventions for all control statements 18 | "default-case": [2], // require default case in switch statements 19 | "dot-notation": [2], // encourages use of dot notation whenever possible 20 | "dot-location": [2, "property"], // enforces consistent newlines before or after dots 21 | "eqeqeq": [2, "smart"], // require the use of === and !== 22 | "guard-for-in": [2], // make sure for-in loops have an if statement 23 | "no-alert": [2], // disallow the use of alert, confirm, and prompt 24 | "no-caller": [2], // disallow use of arguments.caller or arguments.callee 25 | "no-div-regex": [0], // disallow division operators explicitly at beginning of regular expression 26 | "no-else-return": [0], // disallow else after a return in an if 27 | "no-eq-null": [2], // disallow comparisons to null without a type-checking operator 28 | "no-eval": [2], // disallow use of eval() 29 | "no-extend-native": [2], // disallow adding to native types 30 | "no-extra-bind": [2], // disallow unnecessary function binding 31 | "no-fallthrough": [2], // disallow fallthrough of case statements (recommended) 32 | "no-floating-decimal": [2], // disallow the use of leading or trailing decimal points in numeric literals 33 | "no-implicit-coercion": [0], // disallow the type conversions with shorter notations 34 | "no-implied-eval": [2], // disallow use of eval()-like methods 35 | "no-invalid-this": [0], // disallow this keywords outside of classes or class-like objects 36 | "no-iterator": [2], // disallow usage of __iterator__ property 37 | "no-labels": [2], // disallow use of labeled statements 38 | "no-lone-blocks": [2], // disallow unnecessary nested blocks 39 | "no-loop-func": [2], // disallow creation of functions within loops 40 | "no-multi-spaces": [2], // disallow use of multiple spaces 41 | "no-multi-str": [2], // disallow use of multiline strings 42 | "no-native-reassign": [2], // disallow reassignments of native objects 43 | "no-new-func": [2], // disallow use of new operator for Function object 44 | "no-new-wrappers": [0], // disallows creating new instances of String,Number, and Boolean 45 | "no-new": [2], // disallow use of the new operator when not part of an assignment or comparison 46 | "no-octal-escape": [2], // disallow use of octal escape sequences in string literals, such as var foo = "Copyright \251"; 47 | "no-octal": [2], // disallow use of octal literals (recommended) 48 | "no-param-reassign": [0, {"props": false}], // disallow reassignment of function parameters 49 | "no-process-env": [0], // disallow use of process.env 50 | "no-proto": [2], // disallow usage of __proto__ property 51 | "no-redeclare": [2, {"builtinGlobals": true}], // disallow declaring the same variable more than once (recommended) 52 | "no-return-assign": [0], // disallow use of assignment in return statement 53 | "no-script-url": [2], // disallow use of javascript: urls. 54 | "no-self-compare": [2], // disallow comparisons where both sides are exactly the same 55 | "no-sequences": [2], // disallow use of the comma operator 56 | "no-throw-literal": [1], // restrict what can be thrown as an exception 57 | "no-unused-expressions": [2], // disallow usage of expressions in statement position 58 | "no-useless-call": [2], // disallow unnecessary .call() and .apply() 59 | "no-useless-concat": [2], // disallow unnecessary concatenation of literals or template literals 60 | "no-void": [2], // disallow use of the void operator 61 | "no-warning-comments": [0, {"terms": ["todo", "fixme"], "location": "start"}], // disallow usage of configurable warning terms in comments": [2], // e.g. TODO or FIXME 62 | "no-with": [2], // disallow use of the with statement 63 | "radix": [2], // require use of the second argument for parseInt() 64 | "vars-on-top": [0], // require declaration of all vars at the top of their containing scope 65 | "wrap-iife": [2], // require immediate function invocation to be wrapped in parentheses 66 | "yoda": [0, "never"], // require or disallow Yoda conditions 67 | // Variables 68 | "init-declarations": [0], // enforce or disallow variable initializations at definition 69 | "no-catch-shadow": [0], // disallow the catch clause parameter name being the same as a variable in the outer scope 70 | "no-delete-var": [2], // disallow deletion of variables (recommended) 71 | "no-label-var": [2], // disallow labels that share a name with a variable 72 | "no-shadow-restricted-names": [2], // disallow shadowing of names such as arguments 73 | "no-shadow": [2], // disallow declaration of variables already declared in the outer scope 74 | "no-undef-init": [2], // disallow use of undefined when initializing variables 75 | "no-undef": [2], // disallow use of undeclared variables unless mentioned in a /*global */ block (recommended) 76 | "no-undefined": [0], // disallow use of undefined variable 77 | "no-unused-vars": [2], // disallow declaration of variables that are not used in the code (recommended) 78 | "no-use-before-define": [2, "nofunc"], // disallow use of variables before they are defined 79 | // nodejs 80 | "callback-return": [2, ["callback", "cb", "next"]], // enforce return after a callback 81 | "handle-callback-err": [2, "^(err\\d?|error\\d?|^.+Err$|^.+Error$)$"], // enforce error handling in callbacks 82 | "no-mixed-requires": [2, false], // disallow mixing regular variable and require declarations 83 | "no-new-require": [2], // disallow use of new operator with the require function 84 | "no-path-concat": [2], // disallow string concatenation with __dirname and __filename 85 | "no-process-exit": [0], // disallow process.exit() 86 | "no-restricted-modules": [0], // restrict usage of specified node modules 87 | "no-sync": [2], // disallow use of synchronous methods 88 | // Stylistic 89 | "array-bracket-spacing": [2, "never"], // enforce spacing inside array brackets 90 | "block-spacing": [2, "never"], // disallow or enforce spaces inside of single line blocks 91 | "brace-style": [2, "1tbs", { "allowSingleLine": true }], // enforce one true brace style 92 | "camelcase": [2, {"properties": "always"}], // require camel case names 93 | "comma-spacing": [2, {"before": false, "after": true}], // enforce spacing before and after comma 94 | "comma-style": [2, "last"], // enforce one true comma style 95 | "computed-property-spacing": [2, "never"], // require or disallow padding inside computed properties 96 | "consistent-this": [2, "self"], // enforce consistent naming when capturing the current execution context 97 | "eol-last": [2], // enforce newline at the end of file, with no multiple empty lines 98 | "func-names": [0], // require function expressions to have a name 99 | "func-style": [2, "declaration"], // enforce use of function declarations or expressions 100 | "id-length": [2, {"min": 3, "properties": "never", "exceptions": ["Q", "q", "_", "cb", "id", "i", "j"]}], // this option enforces minimum and maximum identifier lengths (variable names, property names etc.) 101 | "id-match": [0], // require identifiers to match the provided regular expression 102 | "key-spacing": [2, {"beforeColon": false, "afterColon": true}], // enforce spacing between keys and values in object literal properties 103 | "keyword-spacing": [2, {}], // require a space after certain keywords 104 | "lines-around-comment": [0], // enforce empty lines around comments 105 | "max-nested-callbacks": [2, 6], // specify the maximum depth callbacks can be nested 106 | "new-cap": [2, {"capIsNewExceptions": ["Router"]}], // require a capital letter for constructors 107 | "new-parens": [2], // disallow the omission of parentheses when invoking a constructor with no arguments 108 | "newline-after-var": [2, "always"], // require or disallow an empty newline after variable declarations 109 | "no-array-constructor": [2], // disallow use of the Array constructor 110 | "no-continue": [2], // disallow use of the continue statement 111 | "no-inline-comments": [0], // disallow comments inline after code 112 | "no-lonely-if": [0], // disallow if as the only statement in an else block 113 | "no-mixed-spaces-and-tabs": [2], // disallow mixed spaces and tabs for indentation (recommended) 114 | "no-multiple-empty-lines": [2, {"max": 1}], // disallow multiple empty lines 115 | "no-nested-ternary": [2], // disallow nested ternary expressions 116 | "no-new-object": [2], // disallow the use of the Object constructor 117 | "no-spaced-func": [2], // disallow space between function identifier and application 118 | "no-ternary": [0], // disallow the use of ternary operators 119 | "no-trailing-spaces": [2, { "skipBlankLines": true }], // disallow trailing whitespace at the end of lines 120 | "no-underscore-dangle": [0], // disallow dangling underscores in identifiers 121 | "no-unneeded-ternary": [2], // disallow the use of Boolean literals in conditional expressions 122 | "object-curly-spacing": [2, "always"], // require or disallow padding inside curly braces 123 | "one-var": [0], // require or disallow one variable declaration per function 124 | "operator-assignment": [0], // require assignment operator shorthand where possible or prohibit it entirely 125 | "operator-linebreak": [2, "after"], // enforce operators to be placed before or after line breaks 126 | "padded-blocks": [0, "never"], // enforce padding within blocks 127 | "quote-props": [2, "consistent"], // require quotes around object literal property names 128 | "semi-spacing": [2, {"before": false, "after": true}], // enforce spacing before and after semicolons 129 | "sort-vars": [0], // sort variables within the same declaration block 130 | "space-before-blocks": [2, "always"], // require or disallow a space before blocks 131 | "space-before-function-paren": [2, {"anonymous": "always", "named": "never"}], // require or disallow a space before function opening parenthesis 132 | "space-in-parens": [2, "never"], // require or disallow spaces inside parentheses 133 | "space-infix-ops": [2, {"int32Hint": false}], // require spaces around operators 134 | "space-return-throw-case": [0], // require a space after return, throw, and case 135 | "space-unary-ops": [2, {"words": true, "nonwords": false}], // require or disallow spaces before/after unary operators 136 | "spaced-comment": [2, "always"], // require or disallow a space immediately following the // or /* in a comment 137 | "wrap-regex": [2] // require regex literals to be wrapped in parentheses 138 | }, 139 | "env": { 140 | "node": true, 141 | "es6": true, 142 | "mocha": true 143 | }, 144 | "extends": "eslint:recommended" 145 | } -------------------------------------------------------------------------------- /lib/err/instance-validator.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var errValidation = require("./singleton-validator"); 4 | 5 | /** 6 | * 7 | * Validate values in a nested object using a dot notation structure (e.g. .shouldBeString("Person.Address.Street.zip")) 8 | * System will validate the the Person, Person.Address, and Person.Address.Street objects exist, and will validate that zip is a String. 9 | * 10 | * Use this interface if you want to utilize the following functionality: 11 | * 1. Nested object validation using a dot notation. 12 | * 13 | * @param {Object} objectUnderTest - Object to run validations against. 14 | * @class 15 | */ 16 | class InstanceValidator { 17 | 18 | constructor(objectUnderTest) { 19 | // out = Object Under Test 20 | this.out = objectUnderTest; 21 | } 22 | 23 | _validate_(configPath, verification, message) { 24 | var variables = configPath.split("."); 25 | 26 | var current = this.out || {}; 27 | var count = 0; 28 | 29 | variables.forEach((variable) => { 30 | // If statement needed because we need to be able to verify shouldBeUndefined. 31 | if (count !== variables.length - 1) { 32 | errValidation.shouldBeDefined(current[variable], message); 33 | } 34 | 35 | current = current[variable]; 36 | count++; 37 | }); 38 | 39 | verification(current); 40 | } 41 | 42 | /** 43 | * Throws an error if any value does not exist in the objectToTest, from configPath. Throws an error if the last key from configPath is defined. 44 | * 45 | * @param {String} configPath - The value to validate. 46 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 47 | * @returns {this} - Returns itself to allow chainable validations. 48 | */ 49 | shouldBeDefined(configPath, message) { 50 | this._validate_(configPath, function (val) { 51 | errValidation.shouldBeDefined(val, message); 52 | }, message); 53 | 54 | return this; 55 | } 56 | 57 | /** 58 | * Throws an error if any value does not exist in the objectToTest, from configPath. Throws an error if the last key from configPath is not defined. 59 | * 60 | * @param {String} configPath - The value to validate. 61 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 62 | * @returns {this} - Returns itself to allow chainable validations. 63 | */ 64 | shouldBeUndefined(configPath, message) { 65 | this._validate_(configPath, function (val) { 66 | errValidation.shouldBeUndefined(val, message); 67 | }, message); 68 | 69 | return this; 70 | } 71 | 72 | /** 73 | * Throws an error if any value does not exist in the objectToTest, from configPath. Throws an error if the last key from configPath is not an array or is an empty array. 74 | * 75 | * @param {String} configPath - The value to validate. 76 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 77 | * @returns {this} - Returns itself to allow chainable validations. 78 | */ 79 | shouldBeNonEmptyArray(configPath, message) { 80 | this._validate_(configPath, function (val) { 81 | errValidation.shouldBeArray(val, message); 82 | errValidation.shouldNotBeEmpty(val, message); 83 | }, message); 84 | 85 | return this; 86 | } 87 | 88 | /** 89 | * Throws an error if any value does not exist in the objectToTest, from configPath. Throws an error if the last key from configPath is an array. 90 | * 91 | * @param {String} configPath - The value to validate. 92 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 93 | * @returns {this} - Returns itself to allow chainable validations. 94 | */ 95 | shouldBeArray(configPath, message) { 96 | this._validate_(configPath, function (val) { 97 | errValidation.shouldBeArray(val, message); 98 | }, message); 99 | 100 | return this; 101 | } 102 | 103 | /** 104 | * Throws an error if any value does not exist in the objectToTest, from configPath. Throws an error if the last key from configPath is not an array. 105 | * 106 | * @param {String} configPath - The value to validate. 107 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 108 | * @returns {this} - Returns itself to allow chainable validations. 109 | */ 110 | shouldNotBeArray(configPath, message) { 111 | this._validate_(configPath, function (val) { 112 | errValidation.shouldNotBeArray(val, message); 113 | }, message); 114 | 115 | return this; 116 | } 117 | 118 | /** 119 | * Throws an error if any value does not exist in the objectToTest, from configPath. Throws an error if the last key from configPath is of type Object. 120 | * 121 | * @param {String} configPath - The value to validate. 122 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 123 | * @returns {this} - Returns itself to allow chainable validations. 124 | */ 125 | shouldBeObject(configPath, message) { 126 | this._validate_(configPath, function (val) { 127 | errValidation.shouldBeObject(val, message); 128 | }, message); 129 | 130 | return this; 131 | } 132 | 133 | /** 134 | * Throws an error if any value does not exist in the objectToTest, from configPath. Throws an error if the last key from configPath is not of type Object. 135 | * 136 | * @param {String} configPath - The value to validate. 137 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 138 | * @returns {this} - Returns itself to allow chainable validations. 139 | */ 140 | shouldNotBeObject(configPath, message) { 141 | this._validate_(configPath, function (val) { 142 | errValidation.shouldNotBeObject(val, message); 143 | }, message); 144 | 145 | return this; 146 | } 147 | 148 | /** 149 | * Throws an error if any value does not exist in the objectToTest, from configPath. Throws an error if the last key from configPath is not empty. 150 | * 151 | * @param {String} configPath - The value to validate. 152 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 153 | * @returns {this} - Returns itself to allow chainable validations. 154 | */ 155 | shouldBeEmpty(configPath, message) { 156 | this._validate_(configPath, function (val) { 157 | errValidation.shouldBeEmpty(val, message); 158 | }, message); 159 | 160 | return this; 161 | } 162 | 163 | /** 164 | * Throws an error if any value does not exist in the objectToTest, from configPath. Throws an error if the last key from configPath is empty. 165 | * 166 | * @param {String} configPath - The value to validate. 167 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 168 | * @returns {this} - Returns itself to allow chainable validations. 169 | */ 170 | shouldNotBeEmpty(configPath, message) { 171 | this._validate_(configPath, function (val) { 172 | errValidation.shouldNotBeEmpty(val, message); 173 | }, message); 174 | 175 | return this; 176 | } 177 | 178 | /** 179 | * Throws an error if any value does not exist in the objectToTest, from configPath. Throws an error if the last key from configPath is not of type Function. 180 | * 181 | * @param {String} configPath - The value to validate. 182 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 183 | * @returns {this} - Returns itself to allow chainable validations. 184 | */ 185 | shouldBeFunction(configPath, message) { 186 | this._validate_(configPath, function (val) { 187 | errValidation.shouldBeFunction(val, message); 188 | }, message); 189 | 190 | return this; 191 | } 192 | 193 | /** 194 | * Throws an error if any value does not exist in the objectToTest, from configPath. Throws an error if the last key from configPath is of type Function. 195 | * 196 | * @param {String} configPath - The value to validate. 197 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 198 | * @returns {this} - Returns itself to allow chainable validations. 199 | */ 200 | shouldNotBeFunction(configPath, message) { 201 | this._validate_(configPath, function (val) { 202 | errValidation.shouldNotBeFunction(val, message); 203 | }, message); 204 | 205 | return this; 206 | } 207 | 208 | /** 209 | * Throws an error if any value does not exist in the objectToTest, from configPath. Throws an error if the last key from configPath is not of type String. 210 | * 211 | * @param {String} configPath - The value to validate. 212 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 213 | * @returns {this} - Returns itself to allow chainable validations. 214 | */ 215 | shouldBeString(configPath, message) { 216 | this._validate_(configPath, function (val) { 217 | errValidation.shouldBeString(val, message); 218 | }, message); 219 | 220 | return this; 221 | } 222 | 223 | /** 224 | * Throws an error if any value does not exist in the objectToTest, from configPath. Throws an error if the last key from configPath is of type String. 225 | * 226 | * @param {String} configPath - The value to validate. 227 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 228 | * @returns {this} - Returns itself to allow chainable validations. 229 | */ 230 | shouldNotBeString(configPath, message) { 231 | this._validate_(configPath, function (val) { 232 | errValidation.shouldNotBeString(val, message); 233 | }, message); 234 | 235 | return this; 236 | } 237 | 238 | /** 239 | * Throws an error if any value does not exist in the objectToTest, from configPath. Throws an error if the last key from configPath is not of type Number. 240 | * 241 | * @param {String} configPath - The value to validate. 242 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 243 | * @returns {this} - Returns itself to allow chainable validations. 244 | */ 245 | shouldBeNumber(configPath, message) { 246 | this._validate_(configPath, function (val) { 247 | errValidation.shouldBeNumber(val, message); 248 | }, message); 249 | 250 | return this; 251 | } 252 | 253 | /** 254 | * Throws an error if any value does not exist in the objectToTest, from configPath. Throws an error if the last key from configPath is of type Number. 255 | * 256 | * @param {String} configPath - The value to validate. 257 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 258 | * @returns {this} - Returns itself to allow chainable validations. 259 | */ 260 | shouldNotBeNumber(configPath, message) { 261 | this._validate_(configPath, function (val) { 262 | errValidation.shouldNotBeNumber(val, message); 263 | }, message); 264 | 265 | return this; 266 | } 267 | 268 | /** 269 | * Throws an error if any value does not exist in the objectToTest, from configPath. Throws an error if the last key from configPath is not finite. 270 | * 271 | * @param {String} configPath - The value to validate. 272 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 273 | * @returns {this} - Returns itself to allow chainable validations. 274 | */ 275 | shouldBeFinite(configPath, message) { 276 | this._validate_(configPath, function (val) { 277 | errValidation.shouldBeFinite(val, message); 278 | }, message); 279 | 280 | return this; 281 | } 282 | 283 | /** 284 | * Throws an error if any value does not exist in the objectToTest, from configPath. Throws an error if the last key from configPath is not infinte. 285 | * 286 | * @param {String} configPath - The value to validate. 287 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 288 | * @returns {this} - Returns itself to allow chainable validations. 289 | */ 290 | shouldBeInfinite(configPath, message) { 291 | this._validate_(configPath, function (val) { 292 | errValidation.shouldBeInfinite(val, message); 293 | }, message); 294 | 295 | return this; 296 | } 297 | 298 | /** 299 | * Throws an error if any value does not exist in the objectToTest, from configPath. Throws an error if the last key from configPath is not of type Boolean. 300 | * 301 | * @param {String} configPath - The value to validate. 302 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 303 | * @returns {this} - Returns itself to allow chainable validations. 304 | */ 305 | shouldBeBoolean(configPath, message) { 306 | this._validate_(configPath, function (val) { 307 | errValidation.shouldBeBoolean(val, message); 308 | }, message); 309 | 310 | return this; 311 | } 312 | 313 | /** 314 | * Throws an error if any value does not exist in the objectToTest, from configPath. Throws an error if the last key from configPath is of type Boolean. 315 | * 316 | * @param {String} configPath - The value to validate. 317 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 318 | * @returns {this} - Returns itself to allow chainable validations. 319 | */ 320 | shouldNotBeBoolean(configPath, message) { 321 | this._validate_(configPath, function (val) { 322 | errValidation.shouldNotBeBoolean(val, message); 323 | }, message); 324 | 325 | return this; 326 | } 327 | 328 | /** 329 | * Throws an error if any value does not exist in the objectToTest, from configPath. Throws an error if the last key from configPath is not of type Date. 330 | * 331 | * @param {String} configPath - The value to validate. 332 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 333 | * @returns {this} - Returns itself to allow chainable validations. 334 | */ 335 | shouldBeDate(configPath, message) { 336 | this._validate_(configPath, function (val) { 337 | errValidation.shouldBeDate(val, message); 338 | }, message); 339 | 340 | return this; 341 | } 342 | 343 | /** 344 | * Throws an error if any value does not exist in the objectToTest, from configPath. Throws an error if the last key from configPath is of type Date. 345 | * 346 | * @param {String} configPath - The value to validate. 347 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 348 | * @returns {this} - Returns itself to allow chainable validations. 349 | */ 350 | shouldNotBeDate(configPath, message) { 351 | this._validate_(configPath, function (val) { 352 | errValidation.shouldNotBeDate(val, message); 353 | }, message); 354 | 355 | return this; 356 | } 357 | 358 | /** 359 | * Throws an error if any value does not exist in the objectToTest, from configPath. Throws an error if the last key from configPath is not a Regular Expression. 360 | * 361 | * @param {String} configPath - The value to validate. 362 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 363 | * @returns {this} - Returns itself to allow chainable validations. 364 | */ 365 | shouldBeRegExp(configPath, message) { 366 | this._validate_(configPath, function (val) { 367 | errValidation.shouldBeRegExp(val, message); 368 | }, message); 369 | 370 | return this; 371 | } 372 | 373 | /** 374 | * Throws an error if any value does not exist in the objectToTest, from configPath. Throws an error if the last key from configPath is a Regular Expression. 375 | * 376 | * @param {String} configPath - The value to validate. 377 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 378 | * @returns {this} - Returns itself to allow chainable validations. 379 | */ 380 | shouldNotBeRegExp(configPath, message) { 381 | this._validate_(configPath, function (val) { 382 | errValidation.shouldNotBeRegExp(val, message); 383 | }, message); 384 | 385 | return this; 386 | } 387 | 388 | /** 389 | * Throws an error if any value does not exist in the objectToTest, from configPath. Throws an error if the last key from configPath is not falsey. 390 | * 391 | * @param {String} configPath - The value to validate. 392 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 393 | * @returns {this} - Returns itself to allow chainable validations. 394 | */ 395 | shouldBeFalsey(configPath, message) { 396 | this._validate_(configPath, function (val) { 397 | errValidation.shouldBeFalsey(val, message); 398 | }, message); 399 | 400 | return this; 401 | } 402 | 403 | /** 404 | * Throws an error if any value does not exist in the objectToTest, from configPath. Throws an error if the last key from configPath is falsey. 405 | * 406 | * @param {String} configPath - The value to validate. 407 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 408 | * @returns {this} - Returns itself to allow chainable validations. 409 | */ 410 | shouldNotBeFalsey(configPath, message) { 411 | this._validate_(configPath, function (val) { 412 | errValidation.shouldNotBeFalsey(val, message); 413 | }, message); 414 | 415 | return this; 416 | } 417 | 418 | /** 419 | * Synonym for shouldBeFalsey. 420 | * 421 | * @param {String} configPath - The value to validate. 422 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 423 | * @returns {this} - Returns itself to allow chainable validations. 424 | */ 425 | shouldBeFalsy(configPath, message) { 426 | this._validate_(configPath, function (val) { 427 | errValidation.shouldBeFalsey(val, message); 428 | }, message); 429 | 430 | return this; 431 | } 432 | 433 | /** 434 | * Synonym for shouldNotBeFalsey. 435 | * 436 | * @param {String} configPath - The value to validate. 437 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 438 | * @returns {this} - Returns itself to allow chainable validations. 439 | */ 440 | shouldNotBeFalsy(configPath, message) { 441 | this._validate_(configPath, function (val) { 442 | errValidation.shouldNotBeFalsey(val, message); 443 | }, message); 444 | 445 | return this; 446 | } 447 | 448 | /** 449 | * Synonym for shouldNotBeFalsey. 450 | * 451 | * @param {String} configPath - The value to validate. 452 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 453 | * @returns {this} - Returns itself to allow chainable validations. 454 | */ 455 | shouldBeTruthy(configPath, message) { 456 | this._validate_(configPath, function (val) { 457 | errValidation.shouldNotBeFalsey(val, message); 458 | }, message); 459 | 460 | return this; 461 | } 462 | 463 | /** 464 | * Synonym for shouldBeFalsey. 465 | * 466 | * @param {String} configPath - The value to validate. 467 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 468 | * @returns {this} - Returns itself to allow chainable validations. 469 | */ 470 | shouldNotBeTruthy(configPath, message) { 471 | this._validate_(configPath, function (val) { 472 | errValidation.shouldBeFalsey(val, message); 473 | }, message); 474 | 475 | return this; 476 | } 477 | 478 | /** 479 | * Ensures the truth of an expression involving one or more parameters to the calling method. 480 | * 481 | * @param {String} expression - The value to validate. 482 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 483 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 484 | * @returns {this} - Returns itself to allow chainable validations. 485 | */ 486 | checkArgument(expression, message) { 487 | errValidation.checkArgument(expression, message); 488 | return this; 489 | } 490 | 491 | /** 492 | * Ensures the truth of an expression involving the state of the calling InstanceValidator, but not involving any parameters to the calling method. 493 | * 494 | * @param {String} expression - The value to validate. 495 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 496 | * @returns {this} - Returns itself to allow chainable validations. 497 | */ 498 | checkState(expression, message) { 499 | errValidation.checkState(expression, message); 500 | return this; 501 | } 502 | 503 | /** 504 | * Ensures that index specifies a valid element in an array, list or string of size size. 505 | * 506 | * @param {Number} index 507 | * @param {Number} size 508 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 509 | * @returns {this} - Returns itself to allow chainable validations. 510 | */ 511 | checkElementIndex(index, size, message) { 512 | errValidation.checkElementIndex(index, size, message); 513 | return this; 514 | } 515 | 516 | /** 517 | * Ensures that index specifies a valid position in an array, list or string of size size. 518 | * 519 | * @param {Number} index 520 | * @param {Number} size 521 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 522 | * @returns {this} - Returns itself to allow chainable validations. 523 | */ 524 | checkPositionIndex(index, size, message) { 525 | errValidation.checkPositionIndex(index, size, message); 526 | return this; 527 | } 528 | 529 | /** 530 | * Ensures that start and end specify a valid positions in an array, list or string of size size, and are in order. 531 | * 532 | * @param {Number} start 533 | * @param {Number} end 534 | * @param {Number} size 535 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 536 | * @returns {this} - Returns itself to allow chainable validations. 537 | */ 538 | checkPositionIndexes(start, end, size, message) { 539 | errValidation.checkPositionIndexes(start, end, size, message); 540 | return this; 541 | } 542 | } 543 | 544 | module.exports = InstanceValidator; 545 | -------------------------------------------------------------------------------- /lib/errr/validator.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var constants = require("./../constants"), 4 | ErrrDecorator = require("./decorator"), 5 | CoreUtilIs = require("core-util-is"); 6 | 7 | /** 8 | * Validate single value with a buildable interface on top of the errr node module. 9 | * Use this interface if you want to utilize the following functionality: 10 | * 1. Error message templating. 11 | * 2. Only templates error message if ErrrValidator fails which saves event queue cycles. 12 | * 3. Gives ability to append Stack traces to an existing error. 13 | * 4. Gives ability to append debug params to stack trace. 14 | */ 15 | class ErrrValidator { 16 | /** 17 | * Throws an error if 'val' is not defined. 18 | * 19 | * @param {*} val - The value to validate. 20 | * @param {String} [message] - The error message or the error template string to use if the ErrrValidator fails. 21 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 22 | * @returns {ErrrDecorator} - An object that decorates the errr node module. 23 | */ 24 | static shouldBeDefined(val, message, template) { 25 | function doesFail() { 26 | return CoreUtilIs.isUndefined(val); 27 | } 28 | 29 | message = message || constants.ShouldBeDefined; 30 | return new ErrrDecorator(message, template, doesFail); 31 | } 32 | /** 33 | * Throws an error if 'val' is defined. 34 | * 35 | * @param {String} val - The value to validate. 36 | * @param {String} [message] - The error message or the error template string to use if the ErrrValidator fails. 37 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 38 | * @returns {ErrrDecorator} - An object that decorates the errr node module. 39 | */ 40 | static shouldBeUndefined(val, message, template) { 41 | function doesFail() { 42 | return !CoreUtilIs.isUndefined(val); 43 | } 44 | 45 | message = message || constants.ShouldBeUndefined; 46 | return new ErrrDecorator(message, template, doesFail); 47 | } 48 | 49 | /** 50 | * Throws an error if 'val' is not of type Array. 51 | * 52 | * @param {String} val - The value to validate. 53 | * @param {String} [message] - The error message or the error template string to use if the ErrrValidator fails. 54 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 55 | * @returns {ErrrDecorator} - An object that decorates the errr node module. 56 | */ 57 | static shouldBeArray(val, message, template) { 58 | function doesFail() { 59 | return !CoreUtilIs.isArray(val); 60 | } 61 | 62 | message = message || constants.ShouldBeArray; 63 | return new ErrrDecorator(message, template, doesFail); 64 | } 65 | /** 66 | * Throws an error if 'val' is of type Array. 67 | * 68 | * @param {String} val - The value to validate. 69 | * @param {String} [message] - The error message or the error template string to use if the ErrrValidator fails. 70 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 71 | * @returns {ErrrDecorator} - An object that decorates the errr node module. 72 | */ 73 | static shouldNotBeArray(val, message, template) { 74 | function doesFail() { 75 | return CoreUtilIs.isArray(val); 76 | } 77 | 78 | message = message || constants.ShouldNotBeArray; 79 | return new ErrrDecorator(message, template, doesFail); 80 | } 81 | 82 | /** 83 | * Throws an error if 'val' is not of type Object. 84 | * 85 | * @param {String} val - The value to validate. 86 | * @param {String} [message] - The error message or the error template string to use if the ErrrValidator fails. 87 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 88 | * @returns {ErrrDecorator} - An object that decorates the errr node module. 89 | */ 90 | static shouldBeObject(val, message, template) { 91 | function doesFail() { 92 | return !(val !== null && (typeof val === "object" || typeof val === "function")); 93 | } 94 | 95 | message = message || constants.ShouldBeObject; 96 | return new ErrrDecorator(message, template, doesFail); 97 | } 98 | /** 99 | * Throws an error if 'val' is of type Object. 100 | * 101 | * @param {String} val - The value to validate. 102 | * @param {String} [message] - The error message or the error template string to use if the ErrrValidator fails. 103 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 104 | * @returns {ErrrDecorator} - An object that decorates the errr node module. 105 | */ 106 | static shouldNotBeObject(val, message, template) { 107 | function doesFail() { 108 | return val !== null && (typeof val === "object" || typeof val === "function"); 109 | } 110 | 111 | message = message || constants.ShouldNotBeObject; 112 | return new ErrrDecorator(message, template, doesFail); 113 | } 114 | 115 | /** 116 | * Throws an error if 'val' is not empty. 117 | * 118 | * @param {String} val - The value to validate. 119 | * @param {String} [message] - The error message or the error template string to use if the ErrrValidator fails. 120 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 121 | * @returns {ErrrDecorator} - An object that decorates the errr node module. 122 | */ 123 | static shouldBeEmpty(val, message, template) { 124 | function doesFail() { 125 | return !CoreUtilIs.isEmpty(val); 126 | } 127 | 128 | message = message || constants.ShouldBeEmpty; 129 | return new ErrrDecorator(message, template, doesFail); 130 | } 131 | /** 132 | * Throws an error if 'val' is empty. 133 | * 134 | * @param {String} val - The value to validate. 135 | * @param {String} [message] - The error message or the error template string to use if the ErrrValidator fails. 136 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 137 | * @returns {ErrrDecorator} - An object that decorates the errr node module. 138 | */ 139 | static shouldNotBeEmpty(val, message, template) { 140 | function doesFail() { 141 | return CoreUtilIs.isEmpty(val); 142 | } 143 | 144 | message = message || constants.ShouldNotBeEmpty; 145 | return new ErrrDecorator(message, template, doesFail); 146 | } 147 | 148 | /** 149 | * Throws an error if 'val' is not of type Function. 150 | * 151 | * @param {String} val - The value to validate. 152 | * @param {String} [message] - The error message or the error template string to use if the ErrrValidator fails. 153 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 154 | * @returns {ErrrDecorator} - An object that decorates the errr node module. 155 | */ 156 | static shouldBeFunction(val, message, template) { 157 | function doesFail() { 158 | return !CoreUtilIs.isFunction(val); 159 | } 160 | 161 | message = message || constants.ShouldBeFunction; 162 | return new ErrrDecorator(message, template, doesFail); 163 | } 164 | /** 165 | * Throws an error if 'val' is of type Function. 166 | * 167 | * @param {String} val - The value to validate. 168 | * @param {String} [message] - The error message or the error template string to use if the ErrrValidator fails. 169 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 170 | * @returns {ErrrDecorator} - An object that decorates the errr node module. 171 | */ 172 | static shouldNotBeFunction(val, message, template) { 173 | function doesFail() { 174 | return CoreUtilIs.isFunction(val); 175 | } 176 | 177 | message = message || constants.ShouldNotBeFunction; 178 | return new ErrrDecorator(message, template, doesFail); 179 | } 180 | 181 | /** 182 | * Throws an error if 'val' is not of type String. 183 | * 184 | * @param {String} val - The value to validate. 185 | * @param {String} [message] - The error message or the error template string to use if the ErrrValidator fails. 186 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 187 | * @returns {ErrrDecorator} - An object that decorates the errr node module. 188 | */ 189 | static shouldBeString(val, message, template) { 190 | function doesFail() { 191 | return !CoreUtilIs.isString(val); 192 | } 193 | 194 | message = message || constants.ShouldBeString; 195 | return new ErrrDecorator(message, template, doesFail); 196 | } 197 | /** 198 | * Throws an error if 'val' is of type String. 199 | * 200 | * @param {String} val - The value to validate. 201 | * @param {String} [message] - The error message or the error template string to use if the ErrrValidator fails. 202 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 203 | * @returns {ErrrDecorator} - An object that decorates the errr node module. 204 | */ 205 | static shouldNotBeString(val, message, template) { 206 | function doesFail() { 207 | return CoreUtilIs.isString(val); 208 | } 209 | 210 | message = message || constants.ShouldNotBeString; 211 | return new ErrrDecorator(message, template, doesFail); 212 | } 213 | 214 | /** 215 | * Throws an error if 'val' is not of type Number. 216 | * 217 | * @param {String} val - The value to validate. 218 | * @param {String} [message] - The error message or the error template string to use if the ErrrValidator fails. 219 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 220 | * @returns {ErrrDecorator} - An object that decorates the errr node module. 221 | */ 222 | static shouldBeNumber(val, message, template) { 223 | function doesFail() { 224 | return !CoreUtilIs.isNumber(val); 225 | } 226 | 227 | message = message || constants.ShouldBeNumber; 228 | return new ErrrDecorator(message, template, doesFail); 229 | } 230 | /** 231 | * Throws an error if 'val' is of type Number. 232 | * 233 | * @param {String} val - The value to validate. 234 | * @param {String} [message] - The error message or the error template string to use if the ErrrValidator fails. 235 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 236 | * @returns {ErrrDecorator} - An object that decorates the errr node module. 237 | */ 238 | static shouldNotBeNumber(val, message, template) { 239 | function doesFail() { 240 | return CoreUtilIs.isNumber(val); 241 | } 242 | 243 | message = message || constants.ShouldNotBeNumber; 244 | return new ErrrDecorator(message, template, doesFail); 245 | } 246 | 247 | /** 248 | * Throws an error if 'val' is not finite. 249 | * 250 | * @param {String} val - The value to validate. 251 | * @param {String} [message] - The error message or the error template string to use if the ErrrValidator fails. 252 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 253 | * @returns {ErrrDecorator} - An object that decorates the errr node module. 254 | */ 255 | static shouldBeFinite(val, message, template) { 256 | function doesFail() { 257 | return !CoreUtilIs.isFinite(val); 258 | } 259 | 260 | message = message || constants.ShouldBeFinite; 261 | return new ErrrDecorator(message, template, doesFail); 262 | } 263 | /** 264 | * Throws an error if 'val' is finite. 265 | * 266 | * @param {String} val - The value to validate. 267 | * @param {String} [message] - The error message or the error template string to use if the ErrrValidator fails. 268 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 269 | * @returns {ErrrDecorator} - An object that decorates the errr node module. 270 | */ 271 | static shouldBeInfinite(val, message, template) { 272 | function doesFail() { 273 | return CoreUtilIs.isFinite(val); 274 | } 275 | 276 | message = message || constants.ShouldBeInfinite; 277 | return new ErrrDecorator(message, template, doesFail); 278 | } 279 | 280 | /** 281 | * Throws an error if 'val' is not of type Boolean. 282 | * 283 | * @param {String} val - The value to validate. 284 | * @param {String} [message] - The error message or the error template string to use if the ErrrValidator fails. 285 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 286 | * @returns {ErrrDecorator} - An object that decorates the errr node module. 287 | */ 288 | static shouldBeBoolean(val, message, template) { 289 | function doesFail() { 290 | return !CoreUtilIs.isBoolean(val); 291 | } 292 | 293 | message = message || constants.ShouldBeBoolean; 294 | return new ErrrDecorator(message, template, doesFail); 295 | } 296 | /** 297 | * Throws an error if 'val' is of type Boolean. 298 | * 299 | * @param {String} val - The value to validate. 300 | * @param {String} [message] - The error message or the error template string to use if the ErrrValidator fails. 301 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 302 | * @returns {ErrrDecorator} - An object that decorates the errr node module. 303 | */ 304 | static shouldNotBeBoolean(val, message, template) { 305 | function doesFail() { 306 | return CoreUtilIs.isBoolean(val); 307 | } 308 | 309 | message = message || constants.ShouldNotBeBoolean; 310 | return new ErrrDecorator(message, template, doesFail); 311 | } 312 | 313 | /** 314 | * Throws an error if 'val' is not of type Date. 315 | * 316 | * @param {String} val - The value to validate. 317 | * @param {String} [message] - The error message or the error template string to use if the ErrrValidator fails. 318 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 319 | * @returns {ErrrDecorator} - An object that decorates the errr node module. 320 | */ 321 | static shouldBeDate(val, message, template) { 322 | function doesFail() { 323 | return !CoreUtilIs.isDate(val); 324 | } 325 | 326 | message = message || constants.ShouldBeDate; 327 | return new ErrrDecorator(message, template, doesFail); 328 | } 329 | /** 330 | * Throws an error if 'val' is of type Date. 331 | * 332 | * @param {String} val - The value to validate. 333 | * @param {String} [message] - The error message or the error template string to use if the ErrrValidator fails. 334 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 335 | * @returns {ErrrDecorator} - An object that decorates the errr node module. 336 | */ 337 | static shouldNotBeDate(val, message, template) { 338 | function doesFail() { 339 | return CoreUtilIs.isDate(val); 340 | } 341 | 342 | message = message || constants.ShouldNotBeDate; 343 | return new ErrrDecorator(message, template, doesFail); 344 | } 345 | 346 | /** 347 | * Throws an error if 'val' is not a Regular Expression. 348 | * 349 | * @param {String} val - The value to validate. 350 | * @param {String} [message] - The error message or the error template string to use if the ErrrValidator fails. 351 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 352 | * @returns {ErrrDecorator} - An object that decorates the errr node module. 353 | */ 354 | static shouldBeRegExp(val, message, template) { 355 | function doesFail() { 356 | return !CoreUtilIs.isRegExp(val); 357 | } 358 | 359 | message = message || constants.ShouldBeRegExp; 360 | return new ErrrDecorator(message, template, doesFail); 361 | } 362 | /** 363 | * Throws an error if 'val' is a Regular Expression. 364 | * 365 | * @param {String} val - The value to validate. 366 | * @param {String} [message] - The error message or the error template string to use if the ErrrValidator fails. 367 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 368 | * @returns {ErrrDecorator} - An object that decorates the errr node module. 369 | */ 370 | static shouldNotBeRegExp(val, message, template) { 371 | function doesFail() { 372 | return CoreUtilIs.isRegExp(val); 373 | } 374 | 375 | message = message || constants.ShouldNotBeRegExp; 376 | return new ErrrDecorator(message, template, doesFail); 377 | } 378 | 379 | /** 380 | * Throws an error if 'val' is not falsey. 381 | * 382 | * @param {Any} val - The value to validate. 383 | * @param {String} [message] - The error message or the error template string to use if the ErrrValidator fails. 384 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 385 | * @returns {ErrrDecorator} - An object that decorates the errr node module. 386 | */ 387 | static shouldBeFalsey(val, message, template) { 388 | function doesFail() { 389 | return !!val; 390 | } 391 | 392 | message = message || constants.ShouldBeFalsey; 393 | return new ErrrDecorator(message, template, doesFail); 394 | } 395 | /** 396 | * Throws an error if 'val' is falsey. 397 | * 398 | * @param {Any} val - The value to validate. 399 | * @param {String} [message] - The error message or the error template string to use if the ErrrValidator fails. 400 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 401 | * @returns {ErrrDecorator} - An object that decorates the errr node module. 402 | */ 403 | static shouldNotBeFalsey(val, message, template) { 404 | function doesFail() { 405 | return !val; 406 | } 407 | 408 | message = message || constants.ShouldNotBeFalsey; 409 | return new ErrrDecorator(message, template, doesFail); 410 | } 411 | /** 412 | * Synonym for shouldBeFalsey. 413 | * 414 | * @param {Any} val - The value to validate. 415 | * @param {String} [message] - The error message or the error template string to use if the ErrrValidator fails. 416 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 417 | * @returns {ErrrDecorator} - An object that decorates the errr node module. 418 | */ 419 | static shouldBeFalsy(val, message, template) { 420 | return this.shouldBeFalsey(val, message, template); 421 | } 422 | /** 423 | * Synonym for shouldNotBeFalsey. 424 | * 425 | * @param {Any} val - The value to validate. 426 | * @param {String} [message] - The error message or the error template string to use if the ErrrValidator fails. 427 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 428 | * @returns {ErrrDecorator} - An object that decorates the errr node module. 429 | */ 430 | static shouldNotBeFalsy(val, message, template) { 431 | return this.shouldNotBeFalsey(val, message, template); 432 | } 433 | 434 | /** 435 | * Synonym for shouldNotBeFalsey. 436 | * 437 | * @param {Any} val - The value to validate. 438 | * @param {String} [message] - The error message or the error template string to use if the ErrrValidator fails. 439 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 440 | * @returns {ErrrDecorator} - An object that decorates the errr node module. 441 | */ 442 | static shouldBeTruthy(val, message, template) { 443 | return this.shouldNotBeFalsey(val, message, template); 444 | } 445 | /** 446 | * Synonym for shouldBeFalsey. 447 | * 448 | * @param {Any} val - The value to validate. 449 | * @param {String} [message] - The error message or the error template string to use if the ErrrValidator fails. 450 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 451 | * @returns {ErrrDecorator} - An object that decorates the errr node module. 452 | */ 453 | static shouldNotBeTruthy(val, message, template) { 454 | return this.shouldBeFalsey(val, message, template); 455 | } 456 | 457 | /** 458 | * Ensures the truth of an expression involving one or more parameters to the calling method. 459 | * 460 | * @param {Any} expression - The value to validate. 461 | * @param {String} [message] - The error message or the error template string to use if the ErrrValidator fails. 462 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 463 | * @returns {ErrrDecorator} - An object that decorates the errr node module. 464 | */ 465 | static checkArgument(expression, message, template) { 466 | function doesFail() { 467 | return !expression; 468 | } 469 | 470 | message = message || constants.IllegalArgument; 471 | return new ErrrDecorator(message, template, doesFail); 472 | } 473 | /** 474 | * Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the calling method. 475 | * 476 | * @param {Any} expression - The value to validate. 477 | * @param {String} [message] - The error message or the error template string to use if the ErrrValidator fails. 478 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 479 | * @returns {ErrrDecorator} - An object that decorates the errr node module. 480 | */ 481 | static checkState(expression, message, template) { 482 | function doesFail() { 483 | return !expression; 484 | } 485 | 486 | message = message || constants.IllegalState; 487 | return new ErrrDecorator(message, template, doesFail); 488 | } 489 | 490 | /** 491 | * Ensures that index specifies a valid element in an array, list or string of size size. 492 | * 493 | * @param {Number} index 494 | * @param {Number} size 495 | * @param {String} [message] - The error message or the error template string to use if the ErrrValidator fails. 496 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 497 | * @returns {ErrrDecorator} - An object that decorates the errr node module. 498 | */ 499 | static checkElementIndex(index, size, message, template) { 500 | function doesFail() { 501 | return (index < 0 || index >= size); 502 | } 503 | 504 | message = message || constants.ShouldHaveValidIndex; 505 | return new ErrrDecorator(message, template, doesFail); 506 | } 507 | /** 508 | * Ensures that index specifies a valid position in an array, list or string of size size. 509 | * 510 | * @param {Number} index 511 | * @param {Number} size 512 | * @param {String} [message] - The error message or the error template string to use if the ErrrValidator fails. 513 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 514 | * @returns {ErrrDecorator} - An object that decorates the errr node module. 515 | */ 516 | static checkPositionIndex(index, size, message, template) { 517 | function doesFail() { 518 | return (index < 0 || index > size); 519 | } 520 | 521 | message = message || constants.ShouldHaveValidPosition; 522 | return new ErrrDecorator(message, template, doesFail); 523 | } 524 | /** 525 | * Ensures that start and end specify a valid positions in an array, list or string of size size, and are in order. 526 | * 527 | * @param {Number} start 528 | * @param {Number} end 529 | * @param {Number} size 530 | * @param {String} [message] - The error message or the error template string to use if the ErrrValidator fails. 531 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 532 | * @returns {ErrrDecorator} - An object that decorates the errr node module. 533 | */ 534 | static checkPositionIndexes(start, end, size, message, template) { 535 | function doesFail() { 536 | return ((end < start) || (start < 0 || end > size)); 537 | } 538 | 539 | message = message || constants.ShouldHaveValidPositions; 540 | return new ErrrDecorator(message, template, doesFail); 541 | } 542 | } 543 | 544 | module.exports = ErrrValidator; 545 | -------------------------------------------------------------------------------- /lib/err/singleton-validator.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var constants = require("./../constants"), 4 | validate = require("../validator/validate"); 5 | 6 | var util = require("util"); 7 | 8 | /** 9 | * Validate single value with a chainable interface. 10 | * Use this interface if you want to utilize the following functionality: 11 | * 1. Error message templating. 12 | * 2. Only templates error message if validation fails which saves event queue cycles. 13 | * 3. Chain together precondition validations. 14 | */ 15 | class SingletonValidator { 16 | /** 17 | * Throws an error if 'val' is not defined. 18 | * 19 | * @param {String} val - The value to validate. 20 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 21 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 22 | * @returns {this} - Returns itself to allow chainable validations. 23 | */ 24 | static shouldBeDefined(val, message, template) { 25 | if (validate.shouldBeDefined(val)) { 26 | var msg = message || constants.ShouldBeDefined; 27 | 28 | msg = (template === undefined) ? msg : util.format.apply(this, [msg].concat(template)); 29 | throw new Error(msg); 30 | } 31 | return this; 32 | } 33 | 34 | /** 35 | * Throws an error if 'val' is defined. 36 | * 37 | * @param {String} val - The value to validate. 38 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 39 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 40 | * @returns {this} - Returns itself to allow chainable validations. 41 | */ 42 | static shouldBeUndefined(val, message, template) { 43 | if (validate.shouldBeUndefined(val)) { 44 | var msg = message || constants.ShouldBeUndefined; 45 | 46 | msg = (template === undefined) ? msg : util.format.apply(this, [msg].concat(template)); 47 | throw new Error(msg); 48 | } 49 | return this; 50 | } 51 | 52 | /** 53 | * Throws an error if 'val' is not of type Array. 54 | * 55 | * @param {String} val - The value to validate. 56 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 57 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 58 | * @returns {this} - Returns itself to allow chainable validations. 59 | */ 60 | static shouldBeArray(val, message, template) { 61 | if (validate.shouldBeArray(val)) { 62 | var msg = message || constants.ShouldBeArray; 63 | 64 | msg = (template === undefined) ? msg : util.format.apply(this, [msg].concat(template)); 65 | throw new Error(msg); 66 | } 67 | return this; 68 | } 69 | 70 | /** 71 | * Throws an error if 'val' is of type Array. 72 | * 73 | * @param {String} val - The value to validate. 74 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 75 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 76 | * @returns {this} - Returns itself to allow chainable validations. 77 | */ 78 | static shouldNotBeArray(val, message, template) { 79 | if (validate.shouldNotBeArray(val)) { 80 | var msg = message || constants.ShouldNotBeArray; 81 | 82 | msg = (template === undefined) ? msg : util.format.apply(this, [msg].concat(template)); 83 | throw new Error(msg); 84 | } 85 | return this; 86 | } 87 | 88 | /** 89 | * Throws an error if 'val' is not of type Object. 90 | * 91 | * @param {String} val - The value to validate. 92 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 93 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 94 | * @returns {this} - Returns itself to allow chainable validations. 95 | */ 96 | static shouldBeObject(val, message, template) { 97 | if (validate.shouldBeObject(val)) { 98 | var msg = message || constants.ShouldBeObject; 99 | 100 | msg = (template === undefined) ? msg : util.format.apply(this, [msg].concat(template)); 101 | throw new Error(msg); 102 | } 103 | return this; 104 | } 105 | 106 | /** 107 | * Throws an error if 'val' is of type Object. 108 | * 109 | * @param {String} val - The value to validate. 110 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 111 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 112 | * @returns {this} - Returns itself to allow chainable validations. 113 | */ 114 | static shouldNotBeObject(val, message, template) { 115 | if (validate.shouldNotBeObject(val)) { 116 | var msg = message || constants.ShouldNotBeObject; 117 | 118 | msg = (template === undefined) ? msg : util.format.apply(this, [msg].concat(template)); 119 | throw new Error(msg); 120 | } 121 | return this; 122 | } 123 | 124 | /** 125 | * Throws an error if 'val' is not empty. 126 | * 127 | * @param {String} val - The value to validate. 128 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 129 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 130 | * @returns {this} - Returns itself to allow chainable validations. 131 | */ 132 | static shouldBeEmpty(val, message, template) { 133 | if (validate.shouldBeEmpty(val)) { 134 | var msg = message || constants.ShouldBeEmpty; 135 | 136 | msg = (template === undefined) ? msg : util.format.apply(this, [msg].concat(template)); 137 | throw new Error(msg); 138 | } 139 | return this; 140 | } 141 | 142 | /** 143 | * Throws an error if 'val' is empty. 144 | * 145 | * @param {String} val - The value to validate. 146 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 147 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 148 | * @returns {this} - Returns itself to allow chainable validations. 149 | */ 150 | static shouldNotBeEmpty(val, message, template) { 151 | if (validate.shouldNotBeEmpty(val)) { 152 | var msg = message || constants.ShouldNotBeEmpty; 153 | 154 | msg = (template === undefined) ? msg : util.format.apply(this, [msg].concat(template)); 155 | throw new Error(msg); 156 | } 157 | return this; 158 | } 159 | 160 | /** 161 | * Throws an error if 'val' is not of type Function. 162 | * 163 | * @param {String} val - The value to validate. 164 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 165 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 166 | * @returns {this} - Returns itself to allow chainable validations. 167 | */ 168 | static shouldBeFunction(val, message, template) { 169 | if (validate.shouldBeFunction(val)) { 170 | var msg = message || constants.ShouldBeFunction; 171 | 172 | msg = (template === undefined) ? msg : util.format.apply(this, [msg].concat(template)); 173 | throw new Error(msg); 174 | } 175 | return this; 176 | } 177 | 178 | /** 179 | * Throws an error if 'val' is of type Function. 180 | * 181 | * @param {String} val - The value to validate. 182 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 183 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 184 | * @returns {this} - Returns itself to allow chainable validations. 185 | */ 186 | static shouldNotBeFunction(val, message, template) { 187 | if (validate.shouldNotBeFunction(val)) { 188 | var msg = message || constants.ShouldNotBeFunction; 189 | 190 | msg = (template === undefined) ? msg : util.format.apply(this, [msg].concat(template)); 191 | throw new Error(msg); 192 | } 193 | return this; 194 | } 195 | 196 | /** 197 | * Throws an error if 'val' is not of type String. 198 | * 199 | * @param {String} val - The value to validate. 200 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 201 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 202 | * @returns {this} - Returns itself to allow chainable validations. 203 | */ 204 | static shouldBeString(val, message, template) { 205 | if (validate.shouldBeString(val)) { 206 | var msg = message || constants.ShouldBeString; 207 | 208 | msg = (template === undefined) ? msg : util.format.apply(this, [msg].concat(template)); 209 | throw new Error(msg); 210 | } 211 | return this; 212 | } 213 | 214 | /** 215 | * Throws an error if 'val' is of type String. 216 | * 217 | * @param {String} val - The value to validate. 218 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 219 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 220 | * @returns {this} - Returns itself to allow chainable validations. 221 | */ 222 | static shouldNotBeString(val, message, template) { 223 | if (validate.shouldNotBeString(val)) { 224 | var msg = message || constants.ShouldNotBeString; 225 | 226 | msg = (template === undefined) ? msg : util.format.apply(this, [msg].concat(template)); 227 | throw new Error(msg); 228 | } 229 | return this; 230 | } 231 | 232 | /** 233 | * Throws an error if 'val' is not of type Number. 234 | * 235 | * @param {String} val - The value to validate. 236 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 237 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 238 | * @returns {this} - Returns itself to allow chainable validations. 239 | */ 240 | static shouldBeNumber(val, message, template) { 241 | if (validate.shouldBeNumber(val)) { 242 | var msg = message || constants.ShouldBeNumber; 243 | 244 | msg = (template === undefined) ? msg : util.format.apply(this, [msg].concat(template)); 245 | throw new Error(msg); 246 | } 247 | return this; 248 | } 249 | 250 | /** 251 | * Throws an error if 'val' is of type Number. 252 | * 253 | * @param {String} val - The value to validate. 254 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 255 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 256 | * @returns {this} - Returns itself to allow chainable validations. 257 | */ 258 | static shouldNotBeNumber(val, message, template) { 259 | if (validate.shouldNotBeNumber(val)) { 260 | var msg = message || constants.ShouldNotBeNumber; 261 | 262 | msg = (template === undefined) ? msg : util.format.apply(this, [msg].concat(template)); 263 | throw new Error(msg); 264 | } 265 | return this; 266 | } 267 | 268 | /** 269 | * Throws an error if 'val' is not finite. 270 | * 271 | * @param {String} val - The value to validate. 272 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 273 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 274 | * @returns {this} - Returns itself to allow chainable validations. 275 | */ 276 | static shouldBeFinite(val, message, template) { 277 | if (validate.shouldBeFinite(val)) { 278 | var msg = message || constants.ShouldBeFinite; 279 | 280 | msg = (template === undefined) ? msg : util.format.apply(this, [msg].concat(template)); 281 | throw new Error(msg); 282 | } 283 | return this; 284 | } 285 | 286 | /** 287 | * Throws an error if 'val' is not infinite. 288 | * 289 | * @param {String} val - The value to validate. 290 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 291 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 292 | * @returns {this} - Returns itself to allow chainable validations. 293 | */ 294 | static shouldBeInfinite(val, message, template) { 295 | if (validate.shouldBeInfinite(val)) { 296 | var msg = message || constants.ShouldBeInfinite; 297 | 298 | msg = (template === undefined) ? msg : util.format.apply(this, [msg].concat(template)); 299 | throw new Error(msg); 300 | } 301 | return this; 302 | } 303 | 304 | /** 305 | * Throws an error if 'val' is not of type Boolean. 306 | * 307 | * @param {String} val - The value to validate. 308 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 309 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 310 | * @returns {this} - Returns itself to allow chainable validations. 311 | */ 312 | static shouldBeBoolean(val, message, template) { 313 | if (validate.shouldBeBoolean(val)) { 314 | var msg = message || constants.ShouldBeBoolean; 315 | 316 | msg = (template === undefined) ? msg : util.format.apply(this, [msg].concat(template)); 317 | throw new Error(msg); 318 | } 319 | return this; 320 | } 321 | 322 | /** 323 | * Throws an error if 'val' is of type Boolean. 324 | * 325 | * @param {String} val - The value to validate. 326 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 327 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 328 | * @returns {this} - Returns itself to allow chainable validations. 329 | */ 330 | static shouldNotBeBoolean(val, message, template) { 331 | if (validate.shouldNotBeBoolean(val)) { 332 | var msg = message || constants.ShouldNotBeBoolean; 333 | 334 | msg = (template === undefined) ? msg : util.format.apply(this, [msg].concat(template)); 335 | throw new Error(msg); 336 | } 337 | return this; 338 | } 339 | 340 | /** 341 | * Throws an error if 'val' is not of type Date. 342 | * 343 | * @param {String} val - The value to validate. 344 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 345 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 346 | * @returns {this} - Returns itself to allow chainable validations. 347 | */ 348 | static shouldBeDate(val, message, template) { 349 | if (validate.shouldBeDate(val)) { 350 | var msg = message || constants.ShouldBeDate; 351 | 352 | msg = (template === undefined) ? msg : util.format.apply(this, [msg].concat(template)); 353 | throw new Error(msg); 354 | } 355 | return this; 356 | } 357 | 358 | /** 359 | * Throws an error if 'val' is of type Date. 360 | * 361 | * @param {String} val - The value to validate. 362 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 363 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 364 | * @returns {this} - Returns itself to allow chainable validations. 365 | */ 366 | static shouldNotBeDate(val, message, template) { 367 | if (validate.shouldNotBeDate(val)) { 368 | var msg = message || constants.ShouldNotBeDate; 369 | 370 | msg = (template === undefined) ? msg : util.format.apply(this, [msg].concat(template)); 371 | throw new Error(msg); 372 | } 373 | return this; 374 | } 375 | 376 | /** 377 | * Throws an error if 'val' is not a Regular Expression. 378 | * 379 | * @param {String} val - The value to validate. 380 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 381 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 382 | * @returns {this} - Returns itself to allow chainable validations. 383 | */ 384 | static shouldBeRegExp(val, message, template) { 385 | if (validate.shouldBeRegExp(val)) { 386 | var msg = message || constants.ShouldBeRegExp; 387 | 388 | msg = (template === undefined) ? msg : util.format.apply(this, [msg].concat(template)); 389 | throw new Error(msg); 390 | } 391 | return this; 392 | } 393 | 394 | /** 395 | * Throws an error if 'val' is a Regular Expression. 396 | * 397 | * @param {String} val - The value to validate. 398 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 399 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 400 | * @returns {this} - Returns itself to allow chainable validations. 401 | */ 402 | static shouldNotBeRegExp(val, message, template) { 403 | if (validate.shouldNotBeRegExp(val)) { 404 | var msg = message || constants.ShouldNotBeRegExp; 405 | 406 | msg = (template === undefined) ? msg : util.format.apply(this, [msg].concat(template)); 407 | throw new Error(msg); 408 | } 409 | return this; 410 | } 411 | 412 | /** 413 | * Throws an error if 'val' is not falsey. 414 | * 415 | * @param {String} val - The value to validate. 416 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 417 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 418 | * @returns {this} - Returns itself to allow chainable validations. 419 | */ 420 | static shouldBeFalsey(val, message, template) { 421 | if (validate.shouldBeFalsey(val)) { 422 | var msg = message || constants.ShouldBeFalsey; 423 | 424 | msg = (template === undefined) ? msg : util.format.apply(this, [msg].concat(template)); 425 | throw new Error(msg); 426 | } 427 | return this; 428 | } 429 | 430 | /** 431 | * Throws an error if 'val' is falsey. 432 | * 433 | * @param {String} val - The value to validate. 434 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 435 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 436 | * @returns {this} - Returns itself to allow chainable validations. 437 | */ 438 | static shouldNotBeFalsey(val, message, template) { 439 | if (validate.shouldNotBeFalsey(val)) { 440 | var msg = message || constants.ShouldNotBeFalsey; 441 | 442 | msg = (template === undefined) ? msg : util.format.apply(this, [msg].concat(template)); 443 | throw new Error(msg); 444 | } 445 | return this; 446 | } 447 | 448 | /** 449 | * Synonym for shouldBeFalsey. 450 | * 451 | * @param {String} val - The value to validate. 452 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 453 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 454 | * @returns {this} - Returns itself to allow chainable validations. 455 | */ 456 | static shouldBeFalsy(val, message, template) { 457 | return this.shouldBeFalsey(val, message, template); 458 | } 459 | 460 | /** 461 | * Synonym for shouldNotBeFalsey. 462 | * 463 | * @param {String} val - The value to validate. 464 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 465 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 466 | * @returns {this} - Returns itself to allow chainable validations. 467 | */ 468 | static shouldNotBeFalsy(val, message, template) { 469 | return this.shouldNotBeFalsey(val, message, template); 470 | } 471 | 472 | /** 473 | * Synonym for shouldNotBeFalsey. 474 | * 475 | * @param {String} val - The value to validate. 476 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 477 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 478 | * @returns {this} - Returns itself to allow chainable validations. 479 | */ 480 | static shouldBeTruthy(val, message, template) { 481 | return this.shouldNotBeFalsey(val, message, template); 482 | } 483 | 484 | /** 485 | * Synonym for shouldBeFalsey. 486 | * 487 | * @param {String} val - The value to validate. 488 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 489 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 490 | * @returns {this} - Returns itself to allow chainable validations. 491 | */ 492 | static shouldNotBeTruthy(val, message, template) { 493 | return this.shouldBeFalsey(val, message, template); 494 | } 495 | 496 | /** 497 | * Ensures the truth of an expression involving one or more parameters to the calling method. 498 | * 499 | * @param {String} expression - The value to validate. 500 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 501 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 502 | * @returns {this} - Returns itself to allow chainable validations. 503 | */ 504 | static checkArgument(expression, message, template) { 505 | if (validate.checkArgument(expression)) { 506 | var msg = message || constants.IllegalArgument; 507 | 508 | msg = (template === undefined) ? msg : util.format.apply(this, [msg].concat(template)); 509 | throw new Error(msg); 510 | } 511 | return this; 512 | } 513 | 514 | /** 515 | * Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the calling method. 516 | * 517 | * @param {String} expression - The value to validate. 518 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 519 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 520 | * @returns {this} - Returns itself to allow chainable validations. 521 | */ 522 | static checkState(expression, message, template) { 523 | if (validate.checkState(expression)) { 524 | var msg = message || constants.IllegalState; 525 | 526 | msg = (template === undefined) ? msg : util.format.apply(this, [msg].concat(template)); 527 | throw new Error(msg); 528 | } 529 | return this; 530 | } 531 | 532 | /** 533 | * Ensures that index specifies a valid element in an array, list or string of size size. 534 | * 535 | * @param {Number} index 536 | * @param {Number} size 537 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 538 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 539 | * @returns {this} - Returns itself to allow chainable validations. 540 | */ 541 | static checkElementIndex(index, size, message, template) { 542 | if (validate.checkElementIndex(index, size)) { 543 | var msg = message || constants.ShouldHaveValidIndex; 544 | 545 | msg = (template === undefined) ? msg : util.format.apply(this, [msg].concat(template)); 546 | throw new Error(msg); 547 | } 548 | return this; 549 | } 550 | 551 | /** 552 | * Ensures that index specifies a valid position in an array, list or string of size size. 553 | * 554 | * @param {Number} index 555 | * @param {Number} size 556 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 557 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 558 | * @returns {this} - Returns itself to allow chainable validations. 559 | */ 560 | static checkPositionIndex(index, size, message, template) { 561 | if (validate.checkPositionIndex(index, size)) { 562 | var msg = message || constants.ShouldHaveValidPosition; 563 | 564 | msg = (template === undefined) ? msg : util.format.apply(this, [msg].concat(template)); 565 | throw new Error(msg); 566 | } 567 | return this; 568 | } 569 | 570 | /** 571 | * Ensures that start and end specify a valid positions in an array, list or string of size size, and are in order. 572 | * 573 | * @param {Number} start 574 | * @param {Number} end 575 | * @param {Number} size 576 | * @param {String} [message] - The error message or the error template string to use if the validation fails. 577 | * @param {Array} [template] - Template params. If provided, the error message will be generated using util.format(message, template). 578 | * @returns {this} - Returns itself to allow chainable validations. 579 | */ 580 | static checkPositionIndexes(start, end, size, message, template) { 581 | if (validate.checkPositionIndexes(start, end, size)) { 582 | var msg = message || constants.ShouldHaveValidPositions; 583 | 584 | msg = (template === undefined) ? msg : util.format.apply(this, [msg].concat(template)); 585 | throw new Error(msg); 586 | } 587 | return this; 588 | } 589 | } 590 | 591 | module.exports = SingletonValidator; 592 | -------------------------------------------------------------------------------- /spec/unit/singleton-spec.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const Preconditions = require("./../../lib/preconditions"), 4 | constants = require("./../../lib/constants"), 5 | random = require("../random"); 6 | 7 | const Maddox = require("maddox"), 8 | chai = require("chai"); 9 | 10 | const expect = chai.expect, 11 | Scenario = Maddox.functional.FromSynchronousScenario; 12 | 13 | class ImAClass {} 14 | 15 | describe("preconditions - when using singleton instance", function () { 16 | let errorContext; 17 | 18 | beforeEach(function () { 19 | errorContext = {}; 20 | 21 | errorContext.setupTest = function () { 22 | errorContext.out = { 23 | foo: { 24 | deep: { 25 | stringValue: "FOO", 26 | numberValue: 10, 27 | functionValue: function () {}, 28 | nonEmptyArray: ["some", "values"], 29 | emptyArray: [], 30 | finiteValue: -100, 31 | infiniteValue: Infinity, 32 | trueValue: true, 33 | falseValue: false, 34 | NaNValue: NaN, 35 | nullValue: null, 36 | regExpValue: /moe/, 37 | dateValue: new Date() 38 | } 39 | } 40 | }; 41 | errorContext.customErrorMessage = "There was an error."; 42 | errorContext.sut = Preconditions.singleton(); 43 | errorContext.entryPointObject = errorContext.sut; 44 | }; 45 | }); 46 | 47 | describe("shouldBeDefined", function () { 48 | it("it should fail when passing in an undefined value", function () { 49 | errorContext.setupErrorMessages = function () { 50 | errorContext.expectedErrorMessage = new Error(constants.ShouldBeDefined).message; 51 | }; 52 | errorContext.setupInputParams = function () { 53 | errorContext.inputParams = [errorContext.out.foo.badValue]; 54 | }; 55 | 56 | errorContext.setupTest(); 57 | errorContext.setupErrorMessages(); 58 | errorContext.setupInputParams(); 59 | 60 | new Scenario() 61 | .withEntryPoint(errorContext.entryPointObject, "shouldBeDefined") 62 | .withInputParams(errorContext.inputParams) 63 | .test(function (err) { 64 | expect(err.message).eql(errorContext.expectedErrorMessage); 65 | }); 66 | }); 67 | 68 | it("it should fail when passing in a deep undefined value", function () { 69 | errorContext.setupErrorMessages = function () { 70 | errorContext.expectedErrorMessage = new Error(constants.ShouldBeDefined).message; 71 | }; 72 | errorContext.setupInputParams = function () { 73 | errorContext.inputParams = [errorContext.out.foo.deep.badValue]; 74 | }; 75 | 76 | errorContext.setupTest(); 77 | errorContext.setupErrorMessages(); 78 | errorContext.setupInputParams(); 79 | 80 | new Scenario() 81 | .withEntryPoint(errorContext.entryPointObject, "shouldBeDefined") 82 | .withInputParams(errorContext.inputParams) 83 | .test(function (err) { 84 | expect(err.message).eql(errorContext.expectedErrorMessage); 85 | }); 86 | }); 87 | 88 | it("it should return my custom error message", function () { 89 | errorContext.setupErrorMessages = function () { 90 | errorContext.expectedErrorMessage = new Error(errorContext.customErrorMessage).message; 91 | }; 92 | errorContext.setupInputParams = function () { 93 | errorContext.inputParams = [errorContext.out.foo.deep.badValue, errorContext.customErrorMessage]; 94 | }; 95 | 96 | errorContext.setupTest(); 97 | errorContext.setupErrorMessages(); 98 | errorContext.setupInputParams(); 99 | 100 | new Scenario() 101 | .withEntryPoint(errorContext.entryPointObject, "shouldBeDefined") 102 | .withInputParams(errorContext.inputParams) 103 | .test(function (err) { 104 | expect(err.message).eql(errorContext.expectedErrorMessage); 105 | }); 106 | }); 107 | 108 | it("it should return the templated error message.", function () { 109 | errorContext.setupErrorMessages = function () { 110 | errorContext.type = random.word(); 111 | errorContext.code = random.uniqueId(); 112 | errorContext.message = random.uniqueId(); 113 | 114 | errorContext.expectedErrorMessage = `(${errorContext.type}:${errorContext.code}) - Error ${errorContext.message}.`; 115 | errorContext.templatedMessage = "(%s:%s) - Error %s."; 116 | }; 117 | errorContext.setupInputParams = function () { 118 | errorContext.inputParams = [errorContext.out.foo.deep.badValue, errorContext.templatedMessage, [errorContext.type, errorContext.code, errorContext.message]]; 119 | }; 120 | 121 | errorContext.setupTest(); 122 | errorContext.setupErrorMessages(); 123 | errorContext.setupInputParams(); 124 | 125 | new Scenario() 126 | .withEntryPoint(errorContext.entryPointObject, "shouldBeDefined") 127 | .withInputParams(errorContext.inputParams) 128 | .test(function (err) { 129 | expect(err.message).eql(errorContext.expectedErrorMessage); 130 | }); 131 | }); 132 | 133 | it("it should pass when passing in a defined value", function () { 134 | errorContext.setupInputParams = function () { 135 | errorContext.inputParams = [errorContext.out.foo]; 136 | }; 137 | 138 | errorContext.setupTest(); 139 | errorContext.setupInputParams(); 140 | 141 | new Scenario() 142 | .withEntryPoint(errorContext.entryPointObject, "shouldBeDefined") 143 | .withInputParams(errorContext.inputParams) 144 | .test(function (err) { 145 | expect(err).to.be.undefined; // eslint-disable-line 146 | }); 147 | }); 148 | }); 149 | describe("shouldBeUndefined", function () { 150 | it("it should fail when value is defined", function () { 151 | errorContext.setupErrorMessages = function () { 152 | errorContext.expectedErrorMessage = new Error(constants.ShouldBeUndefined).message; 153 | }; 154 | errorContext.setupInputParams = function () { 155 | errorContext.inputParams = [errorContext.out.foo.deep.stringValue]; 156 | }; 157 | 158 | errorContext.setupTest(); 159 | errorContext.setupErrorMessages(); 160 | errorContext.setupInputParams(); 161 | 162 | new Scenario() 163 | .withEntryPoint(errorContext.entryPointObject, "shouldBeUndefined") 164 | .withInputParams(errorContext.inputParams) 165 | .test(function (err) { 166 | expect(err.message).eql(errorContext.expectedErrorMessage); 167 | }); 168 | }); 169 | 170 | it("it should pass when value is undefined", function () { 171 | errorContext.setupInputParams = function () { 172 | errorContext.inputParams = [errorContext.out.foo.deep.undefinedValue, this.customErrorMessage]; 173 | }; 174 | 175 | errorContext.setupTest(); 176 | errorContext.setupInputParams(); 177 | 178 | new Scenario() 179 | .withEntryPoint(errorContext.entryPointObject, "shouldBeUndefined") 180 | .withInputParams(errorContext.inputParams) 181 | .test(function (err) { 182 | expect(err).to.be.undefined; // eslint-disable-line 183 | }); 184 | }); 185 | }); 186 | 187 | describe("shouldBeArray", function () { 188 | it("it should fail when value is NOT array", function () { 189 | errorContext.setupErrorMessages = function () { 190 | errorContext.expectedErrorMessage = new Error(constants.ShouldBeArray).message; 191 | }; 192 | errorContext.setupInputParams = function () { 193 | errorContext.inputParams = [errorContext.out.foo.deep.stringValue]; 194 | }; 195 | 196 | errorContext.setupTest(); 197 | errorContext.setupErrorMessages(); 198 | errorContext.setupInputParams(); 199 | 200 | new Scenario() 201 | .withEntryPoint(errorContext.entryPointObject, "shouldBeArray") 202 | .withInputParams(errorContext.inputParams) 203 | .test(function (err) { 204 | expect(err.message).eql(errorContext.expectedErrorMessage); 205 | }); 206 | }); 207 | 208 | it("it should pass when value is array", function () { 209 | errorContext.setupInputParams = function () { 210 | errorContext.inputParams = [errorContext.out.foo.deep.emptyArray]; 211 | }; 212 | 213 | errorContext.setupTest(); 214 | errorContext.setupInputParams(); 215 | 216 | new Scenario() 217 | .withEntryPoint(errorContext.entryPointObject, "shouldBeArray") 218 | .withInputParams(errorContext.inputParams) 219 | .test(function (err) { 220 | expect(err).to.be.undefined; // eslint-disable-line 221 | }); 222 | }); 223 | }); 224 | describe("shouldNotBeArray", function () { 225 | it("it should fail when value is array", function () { 226 | errorContext.setupErrorMessages = function () { 227 | errorContext.expectedErrorMessage = new Error(constants.ShouldNotBeArray).message; 228 | }; 229 | errorContext.setupInputParams = function () { 230 | errorContext.inputParams = [errorContext.out.foo.deep.emptyArray]; 231 | }; 232 | 233 | errorContext.setupTest(); 234 | errorContext.setupErrorMessages(); 235 | errorContext.setupInputParams(); 236 | 237 | new Scenario() 238 | .withEntryPoint(errorContext.entryPointObject, "shouldNotBeArray") 239 | .withInputParams(errorContext.inputParams) 240 | .test(function (err) { 241 | expect(err.message).eql(errorContext.expectedErrorMessage); 242 | }); 243 | }); 244 | 245 | it("it should pass when value is NOT array", function () { 246 | errorContext.setupInputParams = function () { 247 | errorContext.inputParams = [errorContext.out.foo.deep.stringValue]; 248 | }; 249 | 250 | errorContext.setupTest(); 251 | errorContext.setupInputParams(); 252 | 253 | new Scenario() 254 | .withEntryPoint(errorContext.entryPointObject, "shouldNotBeArray") 255 | .withInputParams(errorContext.inputParams) 256 | .test(function (err) { 257 | expect(err).to.be.undefined; // eslint-disable-line 258 | }); 259 | }); 260 | }); 261 | 262 | describe("shouldBeEmpty", function () { 263 | it("it should fail when value is NOT empty", function () { 264 | errorContext.setupErrorMessages = function () { 265 | errorContext.expectedErrorMessage = new Error(constants.ShouldBeEmpty).message; 266 | }; 267 | errorContext.setupInputParams = function () { 268 | errorContext.inputParams = [errorContext.out.foo.deep.nonEmptyArray]; 269 | }; 270 | 271 | errorContext.setupTest(); 272 | errorContext.setupErrorMessages(); 273 | errorContext.setupInputParams(); 274 | 275 | new Scenario() 276 | .withEntryPoint(errorContext.entryPointObject, "shouldBeEmpty") 277 | .withInputParams(errorContext.inputParams) 278 | .test(function (err) { 279 | expect(err.message).eql(errorContext.expectedErrorMessage); 280 | }); 281 | }); 282 | 283 | it("it should pass when value is empty", function () { 284 | errorContext.setupInputParams = function () { 285 | errorContext.inputParams = [errorContext.out.foo.deep.emptyArray]; 286 | }; 287 | 288 | errorContext.setupTest(); 289 | errorContext.setupInputParams(); 290 | 291 | new Scenario() 292 | .withEntryPoint(errorContext.entryPointObject, "shouldBeEmpty") 293 | .withInputParams(errorContext.inputParams) 294 | .test(function (err) { 295 | expect(err).to.be.undefined; // eslint-disable-line 296 | }); 297 | }); 298 | }); 299 | describe("shouldNotBeEmpty", function () { 300 | it("it should fail when value is empty", function () { 301 | errorContext.setupErrorMessages = function () { 302 | errorContext.expectedErrorMessage = new Error(constants.ShouldNotBeEmpty).message; 303 | }; 304 | errorContext.setupInputParams = function () { 305 | errorContext.inputParams = [errorContext.out.foo.deep.emptyArray]; 306 | }; 307 | 308 | errorContext.setupTest(); 309 | errorContext.setupErrorMessages(); 310 | errorContext.setupInputParams(); 311 | 312 | new Scenario() 313 | .withEntryPoint(errorContext.entryPointObject, "shouldNotBeEmpty") 314 | .withInputParams(errorContext.inputParams) 315 | .test(function (err) { 316 | expect(err.message).eql(errorContext.expectedErrorMessage); 317 | }); 318 | }); 319 | 320 | it("it should pass when value is NOT empty", function () { 321 | errorContext.setupInputParams = function () { 322 | errorContext.inputParams = [errorContext.out.foo.deep.nonEmptyArray]; 323 | }; 324 | 325 | errorContext.setupTest(); 326 | errorContext.setupInputParams(); 327 | 328 | new Scenario() 329 | .withEntryPoint(errorContext.entryPointObject, "shouldNotBeEmpty") 330 | .withInputParams(errorContext.inputParams) 331 | .test(function (err) { 332 | expect(err).to.be.undefined; // eslint-disable-line 333 | }); 334 | }); 335 | }); 336 | 337 | describe("shouldBeObject", function () { 338 | it("it should fail when value is NOT an Object", function () { 339 | errorContext.setupErrorMessages = function () { 340 | errorContext.expectedErrorMessage = new Error(constants.ShouldBeObject).message; 341 | }; 342 | errorContext.setupInputParams = function () { 343 | errorContext.inputParams = [errorContext.out.foo.deep.stringValue]; 344 | }; 345 | 346 | errorContext.setupTest(); 347 | errorContext.setupErrorMessages(); 348 | errorContext.setupInputParams(); 349 | 350 | new Scenario() 351 | .withEntryPoint(errorContext.entryPointObject, "shouldBeObject") 352 | .withInputParams(errorContext.inputParams) 353 | .test(function (err) { 354 | expect(err.message).eql(errorContext.expectedErrorMessage); 355 | }); 356 | }); 357 | 358 | it("it should pass when value is an Object", function () { 359 | errorContext.setupInputParams = function () { 360 | errorContext.inputParams = [errorContext.out.foo.deep]; 361 | }; 362 | 363 | errorContext.setupTest(); 364 | errorContext.setupInputParams(); 365 | 366 | new Scenario() 367 | .withEntryPoint(errorContext.entryPointObject, "shouldBeObject") 368 | .withInputParams(errorContext.inputParams) 369 | .test(function (err) { 370 | expect(err).to.be.undefined; // eslint-disable-line 371 | }); 372 | }); 373 | 374 | it("it should pass when value is a Class", function () { 375 | errorContext.setupInputParams = function () { 376 | errorContext.inputParams = [ImAClass]; 377 | }; 378 | 379 | errorContext.setupTest(); 380 | errorContext.setupInputParams(); 381 | 382 | new Scenario() 383 | .withEntryPoint(errorContext.entryPointObject, "shouldBeObject") 384 | .withInputParams(errorContext.inputParams) 385 | .test(function (err) { 386 | expect(err).to.be.undefined; // eslint-disable-line 387 | }); 388 | }); 389 | }); 390 | describe("shouldNotBeObject", function () { 391 | it("it should fail when value is an Object", function () { 392 | errorContext.setupErrorMessages = function () { 393 | errorContext.expectedErrorMessage = new Error(constants.ShouldNotBeObject).message; 394 | }; 395 | errorContext.setupInputParams = function () { 396 | errorContext.inputParams = [errorContext.out.foo.deep]; 397 | }; 398 | 399 | errorContext.setupTest(); 400 | errorContext.setupErrorMessages(); 401 | errorContext.setupInputParams(); 402 | 403 | new Scenario() 404 | .withEntryPoint(errorContext.entryPointObject, "shouldNotBeObject") 405 | .withInputParams(errorContext.inputParams) 406 | .test(function (err) { 407 | expect(err.message).eql(errorContext.expectedErrorMessage); 408 | }); 409 | }); 410 | 411 | it("it should fail when value is a Class", function () { 412 | errorContext.setupErrorMessages = function () { 413 | errorContext.expectedErrorMessage = new Error(constants.ShouldNotBeObject).message; 414 | }; 415 | errorContext.setupInputParams = function () { 416 | errorContext.inputParams = [ImAClass]; 417 | }; 418 | 419 | errorContext.setupTest(); 420 | errorContext.setupErrorMessages(); 421 | errorContext.setupInputParams(); 422 | 423 | new Scenario() 424 | .withEntryPoint(errorContext.entryPointObject, "shouldNotBeObject") 425 | .withInputParams(errorContext.inputParams) 426 | .test(function (err) { 427 | expect(err.message).eql(errorContext.expectedErrorMessage); 428 | }); 429 | }); 430 | 431 | it("it should pass when value is NOT an Object", function () { 432 | errorContext.setupInputParams = function () { 433 | errorContext.inputParams = [errorContext.out.foo.deep.stringValue]; 434 | }; 435 | 436 | errorContext.setupTest(); 437 | errorContext.setupInputParams(); 438 | 439 | new Scenario() 440 | .withEntryPoint(errorContext.entryPointObject, "shouldNotBeObject") 441 | .withInputParams(errorContext.inputParams) 442 | .test(function (err) { 443 | expect(err).to.be.undefined; // eslint-disable-line 444 | }); 445 | }); 446 | }); 447 | 448 | describe("shouldBeFunction", function () { 449 | it("it should fail when value is NOT a Function", function () { 450 | errorContext.setupErrorMessages = function () { 451 | errorContext.expectedErrorMessage = new Error(constants.ShouldBeFunction).message; 452 | }; 453 | errorContext.setupInputParams = function () { 454 | errorContext.inputParams = [errorContext.out.foo.deep.numberValue]; 455 | }; 456 | 457 | errorContext.setupTest(); 458 | errorContext.setupErrorMessages(); 459 | errorContext.setupInputParams(); 460 | 461 | new Scenario() 462 | .withEntryPoint(errorContext.entryPointObject, "shouldBeFunction") 463 | .withInputParams(errorContext.inputParams) 464 | .test(function (err) { 465 | expect(err.message).eql(errorContext.expectedErrorMessage); 466 | }); 467 | }); 468 | 469 | it("it should pass when value is a Function", function () { 470 | errorContext.setupInputParams = function () { 471 | errorContext.inputParams = [errorContext.out.foo.deep.functionValue]; 472 | }; 473 | 474 | errorContext.setupTest(); 475 | errorContext.setupInputParams(); 476 | 477 | new Scenario() 478 | .withEntryPoint(errorContext.entryPointObject, "shouldBeFunction") 479 | .withInputParams(errorContext.inputParams) 480 | .test(function (err) { 481 | expect(err).to.be.undefined; // eslint-disable-line 482 | }); 483 | }); 484 | }); 485 | describe("shouldNotBeFunction", function () { 486 | it("it should fail when value is a Function", function () { 487 | errorContext.setupErrorMessages = function () { 488 | errorContext.expectedErrorMessage = new Error(constants.ShouldNotBeFunction).message; 489 | }; 490 | errorContext.setupInputParams = function () { 491 | errorContext.inputParams = [errorContext.out.foo.deep.functionValue]; 492 | }; 493 | 494 | errorContext.setupTest(); 495 | errorContext.setupErrorMessages(); 496 | errorContext.setupInputParams(); 497 | 498 | new Scenario() 499 | .withEntryPoint(errorContext.entryPointObject, "shouldNotBeFunction") 500 | .withInputParams(errorContext.inputParams) 501 | .test(function (err) { 502 | expect(err.message).eql(errorContext.expectedErrorMessage); 503 | }); 504 | }); 505 | 506 | it("it should pass when value is NOT a Function", function () { 507 | errorContext.setupInputParams = function () { 508 | errorContext.inputParams = [errorContext.out.foo.deep.numberValue]; 509 | }; 510 | 511 | errorContext.setupTest(); 512 | errorContext.setupInputParams(); 513 | 514 | new Scenario() 515 | .withEntryPoint(errorContext.entryPointObject, "shouldNotBeFunction") 516 | .withInputParams(errorContext.inputParams) 517 | .test(function (err) { 518 | expect(err).to.be.undefined; // eslint-disable-line 519 | }); 520 | }); 521 | }); 522 | 523 | describe("shouldBeString", function () { 524 | it("it should fail when value is NOT a String", function () { 525 | errorContext.setupErrorMessages = function () { 526 | errorContext.expectedErrorMessage = new Error(constants.ShouldBeString).message; 527 | }; 528 | errorContext.setupInputParams = function () { 529 | errorContext.inputParams = [errorContext.out.foo.deep.numberValue]; 530 | }; 531 | 532 | errorContext.setupTest(); 533 | errorContext.setupErrorMessages(); 534 | errorContext.setupInputParams(); 535 | 536 | new Scenario() 537 | .withEntryPoint(errorContext.entryPointObject, "shouldBeString") 538 | .withInputParams(errorContext.inputParams) 539 | .test(function (err) { 540 | expect(err.message).eql(errorContext.expectedErrorMessage); 541 | }); 542 | }); 543 | 544 | it("it should pass when value is a String", function () { 545 | errorContext.setupInputParams = function () { 546 | errorContext.inputParams = [errorContext.out.foo.deep.stringValue]; 547 | }; 548 | 549 | errorContext.setupTest(); 550 | errorContext.setupInputParams(); 551 | 552 | new Scenario() 553 | .withEntryPoint(errorContext.entryPointObject, "shouldBeString") 554 | .withInputParams(errorContext.inputParams) 555 | .test(function (err) { 556 | expect(err).to.be.undefined; // eslint-disable-line 557 | }); 558 | }); 559 | }); 560 | describe("shouldNotBeString", function () { 561 | it("it should fail when value is a String", function () { 562 | errorContext.setupErrorMessages = function () { 563 | errorContext.expectedErrorMessage = new Error(constants.ShouldNotBeString).message; 564 | }; 565 | errorContext.setupInputParams = function () { 566 | errorContext.inputParams = [errorContext.out.foo.deep.stringValue]; 567 | }; 568 | 569 | errorContext.setupTest(); 570 | errorContext.setupErrorMessages(); 571 | errorContext.setupInputParams(); 572 | 573 | new Scenario() 574 | .withEntryPoint(errorContext.entryPointObject, "shouldNotBeString") 575 | .withInputParams(errorContext.inputParams) 576 | .test(function (err) { 577 | expect(err.message).eql(errorContext.expectedErrorMessage); 578 | }); 579 | }); 580 | 581 | it("it should pass when value is NOT a String", function () { 582 | errorContext.setupInputParams = function () { 583 | errorContext.inputParams = [errorContext.out.foo.deep.numberValue]; 584 | }; 585 | 586 | errorContext.setupTest(); 587 | errorContext.setupInputParams(); 588 | 589 | new Scenario() 590 | .withEntryPoint(errorContext.entryPointObject, "shouldNotBeString") 591 | .withInputParams(errorContext.inputParams) 592 | .test(function (err) { 593 | expect(err).to.be.undefined; // eslint-disable-line 594 | }); 595 | }); 596 | }); 597 | 598 | describe("shouldBeNumber", function () { 599 | it("it should fail when value is NOT a Number", function () { 600 | errorContext.setupErrorMessages = function () { 601 | errorContext.expectedErrorMessage = new Error(constants.ShouldBeNumber).message; 602 | }; 603 | errorContext.setupInputParams = function () { 604 | errorContext.inputParams = [errorContext.out.foo.deep.stringValue]; 605 | }; 606 | 607 | errorContext.setupTest(); 608 | errorContext.setupErrorMessages(); 609 | errorContext.setupInputParams(); 610 | 611 | new Scenario() 612 | .withEntryPoint(errorContext.entryPointObject, "shouldBeNumber") 613 | .withInputParams(errorContext.inputParams) 614 | .test(function (err) { 615 | expect(err.message).eql(errorContext.expectedErrorMessage); 616 | }); 617 | }); 618 | 619 | it("it should pass when value is a Number", function () { 620 | errorContext.setupInputParams = function () { 621 | errorContext.inputParams = [errorContext.out.foo.deep.numberValue]; 622 | }; 623 | 624 | errorContext.setupTest(); 625 | errorContext.setupInputParams(); 626 | 627 | new Scenario() 628 | .withEntryPoint(errorContext.entryPointObject, "shouldBeNumber") 629 | .withInputParams(errorContext.inputParams) 630 | .test(function (err) { 631 | expect(err).to.be.undefined; // eslint-disable-line 632 | }); 633 | }); 634 | }); 635 | describe("shouldNotBeNumber", function () { 636 | it("it should fail when value is a Number", function () { 637 | errorContext.setupErrorMessages = function () { 638 | errorContext.expectedErrorMessage = new Error(constants.ShouldNotBeNumber).message; 639 | }; 640 | errorContext.setupInputParams = function () { 641 | errorContext.inputParams = [errorContext.out.foo.deep.numberValue]; 642 | }; 643 | 644 | errorContext.setupTest(); 645 | errorContext.setupErrorMessages(); 646 | errorContext.setupInputParams(); 647 | 648 | new Scenario() 649 | .withEntryPoint(errorContext.entryPointObject, "shouldNotBeNumber") 650 | .withInputParams(errorContext.inputParams) 651 | .test(function (err) { 652 | expect(err.message).eql(errorContext.expectedErrorMessage); 653 | }); 654 | }); 655 | 656 | it("it should pass when value is NOT a Number", function () { 657 | errorContext.setupInputParams = function () { 658 | errorContext.inputParams = [errorContext.out.foo.deep.stringValue]; 659 | }; 660 | 661 | errorContext.setupTest(); 662 | errorContext.setupInputParams(); 663 | 664 | new Scenario() 665 | .withEntryPoint(errorContext.entryPointObject, "shouldNotBeNumber") 666 | .withInputParams(errorContext.inputParams) 667 | .test(function (err) { 668 | expect(err).to.be.undefined; // eslint-disable-line 669 | }); 670 | }); 671 | }); 672 | 673 | describe("shouldBeFinite", function () { 674 | it("it should fail when value is NOT Finite", function () { 675 | errorContext.setupErrorMessages = function () { 676 | errorContext.expectedErrorMessage = new Error(constants.ShouldBeFinite).message; 677 | }; 678 | errorContext.setupInputParams = function () { 679 | errorContext.inputParams = [errorContext.out.foo.deep.infiniteValue]; 680 | }; 681 | 682 | errorContext.setupTest(); 683 | errorContext.setupErrorMessages(); 684 | errorContext.setupInputParams(); 685 | 686 | new Scenario() 687 | .withEntryPoint(errorContext.entryPointObject, "shouldBeFinite") 688 | .withInputParams(errorContext.inputParams) 689 | .test(function (err) { 690 | expect(err.message).eql(errorContext.expectedErrorMessage); 691 | }); 692 | }); 693 | 694 | it("it should pass when value is Finite", function () { 695 | errorContext.setupInputParams = function () { 696 | errorContext.inputParams = [errorContext.out.foo.deep.finiteValue]; 697 | }; 698 | 699 | errorContext.setupTest(); 700 | errorContext.setupInputParams(); 701 | 702 | new Scenario() 703 | .withEntryPoint(errorContext.entryPointObject, "shouldBeFinite") 704 | .withInputParams(errorContext.inputParams) 705 | .test(function (err) { 706 | expect(err).to.be.undefined; // eslint-disable-line 707 | }); 708 | }); 709 | }); 710 | describe("shouldBeInfinite", function () { 711 | it("it should fail when value is NOT Infinite", function () { 712 | errorContext.setupErrorMessages = function () { 713 | errorContext.expectedErrorMessage = new Error(constants.ShouldBeInfinite).message; 714 | }; 715 | errorContext.setupInputParams = function () { 716 | errorContext.inputParams = [errorContext.out.foo.deep.finiteValue]; 717 | }; 718 | 719 | errorContext.setupTest(); 720 | errorContext.setupErrorMessages(); 721 | errorContext.setupInputParams(); 722 | 723 | new Scenario() 724 | .withEntryPoint(errorContext.entryPointObject, "shouldBeInfinite") 725 | .withInputParams(errorContext.inputParams) 726 | .test(function (err) { 727 | expect(err.message).eql(errorContext.expectedErrorMessage); 728 | }); 729 | }); 730 | 731 | it("it should pass when value is Infinite", function () { 732 | errorContext.setupInputParams = function () { 733 | errorContext.inputParams = [errorContext.out.foo.deep.infiniteValue]; 734 | }; 735 | 736 | errorContext.setupTest(); 737 | errorContext.setupInputParams(); 738 | 739 | new Scenario() 740 | .withEntryPoint(errorContext.entryPointObject, "shouldBeInfinite") 741 | .withInputParams(errorContext.inputParams) 742 | .test(function (err) { 743 | expect(err).to.be.undefined; // eslint-disable-line 744 | }); 745 | }); 746 | }); 747 | 748 | describe("shouldBeBoolean", function () { 749 | it("it should fail when value is NOT a Boolean", function () { 750 | errorContext.setupErrorMessages = function () { 751 | errorContext.expectedErrorMessage = new Error(constants.ShouldBeBoolean).message; 752 | }; 753 | errorContext.setupInputParams = function () { 754 | errorContext.inputParams = [errorContext.out.foo.deep.stringValue]; 755 | }; 756 | 757 | errorContext.setupTest(); 758 | errorContext.setupErrorMessages(); 759 | errorContext.setupInputParams(); 760 | 761 | new Scenario() 762 | .withEntryPoint(errorContext.entryPointObject, "shouldBeBoolean") 763 | .withInputParams(errorContext.inputParams) 764 | .test(function (err) { 765 | expect(err.message).eql(errorContext.expectedErrorMessage); 766 | }); 767 | }); 768 | 769 | it("it should pass when value is a Boolean", function () { 770 | errorContext.setupInputParams = function () { 771 | errorContext.inputParams = [errorContext.out.foo.deep.falseValue]; 772 | }; 773 | 774 | errorContext.setupTest(); 775 | errorContext.setupInputParams(); 776 | 777 | new Scenario() 778 | .withEntryPoint(errorContext.entryPointObject, "shouldBeBoolean") 779 | .withInputParams(errorContext.inputParams) 780 | .test(function (err) { 781 | expect(err).to.be.undefined; // eslint-disable-line 782 | }); 783 | }); 784 | }); 785 | describe("shouldNotBeBoolean", function () { 786 | it("it should fail when value is a Boolean", function () { 787 | errorContext.setupErrorMessages = function () { 788 | errorContext.expectedErrorMessage = new Error(constants.ShouldNotBeBoolean).message; 789 | }; 790 | errorContext.setupInputParams = function () { 791 | errorContext.inputParams = [errorContext.out.foo.deep.falseValue]; 792 | }; 793 | 794 | errorContext.setupTest(); 795 | errorContext.setupErrorMessages(); 796 | errorContext.setupInputParams(); 797 | 798 | new Scenario() 799 | .withEntryPoint(errorContext.entryPointObject, "shouldNotBeBoolean") 800 | .withInputParams(errorContext.inputParams) 801 | .test(function (err) { 802 | expect(err.message).eql(errorContext.expectedErrorMessage); 803 | }); 804 | }); 805 | 806 | it("it should pass when value is NOT a Boolean", function () { 807 | errorContext.setupInputParams = function () { 808 | errorContext.inputParams = [errorContext.out.foo.deep.stringValue]; 809 | }; 810 | 811 | errorContext.setupTest(); 812 | errorContext.setupInputParams(); 813 | 814 | new Scenario() 815 | .withEntryPoint(errorContext.entryPointObject, "shouldNotBeBoolean") 816 | .withInputParams(errorContext.inputParams) 817 | .test(function (err) { 818 | expect(err).to.be.undefined; // eslint-disable-line 819 | }); 820 | }); 821 | }); 822 | 823 | describe("shouldBeDate", function () { 824 | it("it should fail when value is NOT a Date", function () { 825 | errorContext.setupErrorMessages = function () { 826 | errorContext.expectedErrorMessage = new Error(constants.ShouldBeDate).message; 827 | }; 828 | errorContext.setupInputParams = function () { 829 | errorContext.inputParams = [errorContext.out.foo.deep.stringValue]; 830 | }; 831 | 832 | errorContext.setupTest(); 833 | errorContext.setupErrorMessages(); 834 | errorContext.setupInputParams(); 835 | 836 | new Scenario() 837 | .withEntryPoint(errorContext.entryPointObject, "shouldBeDate") 838 | .withInputParams(errorContext.inputParams) 839 | .test(function (err) { 840 | expect(err.message).eql(errorContext.expectedErrorMessage); 841 | }); 842 | }); 843 | 844 | it("it should pass when value is a Date", function () { 845 | errorContext.setupInputParams = function () { 846 | errorContext.inputParams = [errorContext.out.foo.deep.dateValue]; 847 | }; 848 | 849 | errorContext.setupTest(); 850 | errorContext.setupInputParams(); 851 | 852 | new Scenario() 853 | .withEntryPoint(errorContext.entryPointObject, "shouldBeDate") 854 | .withInputParams(errorContext.inputParams) 855 | .test(function (err) { 856 | expect(err).to.be.undefined; // eslint-disable-line 857 | }); 858 | }); 859 | }); 860 | describe("shouldNotBeDate", function () { 861 | it("it should fail when value is a Date", function () { 862 | errorContext.setupErrorMessages = function () { 863 | errorContext.expectedErrorMessage = new Error(constants.ShouldNotBeDate).message; 864 | }; 865 | errorContext.setupInputParams = function () { 866 | errorContext.inputParams = [errorContext.out.foo.deep.dateValue]; 867 | }; 868 | 869 | errorContext.setupTest(); 870 | errorContext.setupErrorMessages(); 871 | errorContext.setupInputParams(); 872 | 873 | new Scenario() 874 | .withEntryPoint(errorContext.entryPointObject, "shouldNotBeDate") 875 | .withInputParams(errorContext.inputParams) 876 | .test(function (err) { 877 | expect(err.message).eql(errorContext.expectedErrorMessage); 878 | }); 879 | }); 880 | 881 | it("it should pass when value is NOT a Date", function () { 882 | errorContext.setupInputParams = function () { 883 | errorContext.inputParams = [errorContext.out.foo.deep.stringValue]; 884 | }; 885 | 886 | errorContext.setupTest(); 887 | errorContext.setupInputParams(); 888 | 889 | new Scenario() 890 | .withEntryPoint(errorContext.entryPointObject, "shouldNotBeDate") 891 | .withInputParams(errorContext.inputParams) 892 | .test(function (err) { 893 | expect(err).to.be.undefined; // eslint-disable-line 894 | }); 895 | }); 896 | }); 897 | 898 | describe("shouldBeRegExp", function () { 899 | it("it should fail when value is NOT a Regular Expression", function () { 900 | errorContext.setupErrorMessages = function () { 901 | errorContext.expectedErrorMessage = new Error(constants.ShouldBeRegExp).message; 902 | }; 903 | errorContext.setupInputParams = function () { 904 | errorContext.inputParams = [errorContext.out.foo.deep.stringValue]; 905 | }; 906 | 907 | errorContext.setupTest(); 908 | errorContext.setupErrorMessages(); 909 | errorContext.setupInputParams(); 910 | 911 | new Scenario() 912 | .withEntryPoint(errorContext.entryPointObject, "shouldBeRegExp") 913 | .withInputParams(errorContext.inputParams) 914 | .test(function (err) { 915 | expect(err.message).eql(errorContext.expectedErrorMessage); 916 | }); 917 | }); 918 | 919 | it("it should pass when value is a Regular Expression", function () { 920 | errorContext.setupInputParams = function () { 921 | errorContext.inputParams = [errorContext.out.foo.deep.regExpValue]; 922 | }; 923 | 924 | errorContext.setupTest(); 925 | errorContext.setupInputParams(); 926 | 927 | new Scenario() 928 | .withEntryPoint(errorContext.entryPointObject, "shouldBeRegExp") 929 | .withInputParams(errorContext.inputParams) 930 | .test(function (err) { 931 | expect(err).to.be.undefined; // eslint-disable-line 932 | }); 933 | }); 934 | }); 935 | describe("shouldNotBeRegExp", function () { 936 | it("it should fail when value is a Regular Expression", function () { 937 | errorContext.setupErrorMessages = function () { 938 | errorContext.expectedErrorMessage = new Error(constants.ShouldNotBeRegExp).message; 939 | }; 940 | errorContext.setupInputParams = function () { 941 | errorContext.inputParams = [errorContext.out.foo.deep.regExpValue]; 942 | }; 943 | 944 | errorContext.setupTest(); 945 | errorContext.setupErrorMessages(); 946 | errorContext.setupInputParams(); 947 | 948 | new Scenario() 949 | .withEntryPoint(errorContext.entryPointObject, "shouldNotBeRegExp") 950 | .withInputParams(errorContext.inputParams) 951 | .test(function (err) { 952 | expect(err.message).eql(errorContext.expectedErrorMessage); 953 | }); 954 | }); 955 | 956 | it("it should pass when value is NOT a Regular Expression", function () { 957 | errorContext.setupInputParams = function () { 958 | errorContext.inputParams = [errorContext.out.foo.deep.stringValue]; 959 | }; 960 | 961 | errorContext.setupTest(); 962 | errorContext.setupInputParams(); 963 | 964 | new Scenario() 965 | .withEntryPoint(errorContext.entryPointObject, "shouldNotBeRegExp") 966 | .withInputParams(errorContext.inputParams) 967 | .test(function (err) { 968 | expect(err).to.be.undefined; // eslint-disable-line 969 | }); 970 | }); 971 | }); 972 | 973 | describe("shouldBeFalsey", function () { 974 | it("it should fail when value is NOT falsey", function () { 975 | errorContext.setupErrorMessages = function () { 976 | errorContext.expectedErrorMessage = new Error(constants.ShouldBeFalsey).message; 977 | }; 978 | errorContext.setupInputParams = function () { 979 | errorContext.inputParams = [errorContext.out.foo.deep.stringValue]; 980 | }; 981 | 982 | errorContext.setupTest(); 983 | errorContext.setupErrorMessages(); 984 | errorContext.setupInputParams(); 985 | 986 | new Scenario() 987 | .withEntryPoint(errorContext.entryPointObject, "shouldBeFalsey") 988 | .withInputParams(errorContext.inputParams) 989 | .test(function (err) { 990 | expect(err.message).eql(errorContext.expectedErrorMessage); 991 | }); 992 | }); 993 | 994 | it("it should pass when value is undefined", function () { 995 | errorContext.setupInputParams = function () { 996 | errorContext.inputParams = [errorContext.out.foo.deep.undefinedValue]; 997 | }; 998 | 999 | errorContext.setupTest(); 1000 | errorContext.setupInputParams(); 1001 | 1002 | new Scenario() 1003 | .withEntryPoint(errorContext.entryPointObject, "shouldBeFalsey") 1004 | .withInputParams(errorContext.inputParams) 1005 | .test(function (err) { 1006 | expect(err).to.be.undefined; // eslint-disable-line 1007 | }); 1008 | }); 1009 | it("it should pass when value is Nan", function () { 1010 | errorContext.setupInputParams = function () { 1011 | errorContext.inputParams = [errorContext.out.foo.deep.NanValue]; 1012 | }; 1013 | 1014 | errorContext.setupTest(); 1015 | errorContext.setupInputParams(); 1016 | 1017 | new Scenario() 1018 | .withEntryPoint(errorContext.entryPointObject, "shouldBeFalsey") 1019 | .withInputParams(errorContext.inputParams) 1020 | .test(function (err) { 1021 | expect(err).to.be.undefined; // eslint-disable-line 1022 | }); 1023 | }); 1024 | it("it should pass when value is null", function () { 1025 | errorContext.setupInputParams = function () { 1026 | errorContext.inputParams = [errorContext.out.foo.deep.nullValue]; 1027 | }; 1028 | 1029 | errorContext.setupTest(); 1030 | errorContext.setupInputParams(); 1031 | 1032 | new Scenario() 1033 | .withEntryPoint(errorContext.entryPointObject, "shouldBeFalsey") 1034 | .withInputParams(errorContext.inputParams) 1035 | .test(function (err) { 1036 | expect(err).to.be.undefined; // eslint-disable-line 1037 | }); 1038 | }); 1039 | }); 1040 | describe("shouldNotBeFalsey", function () { 1041 | it("it should fail when value is undefined", function () { 1042 | errorContext.setupErrorMessages = function () { 1043 | errorContext.expectedErrorMessage = new Error(constants.ShouldNotBeFalsey).message; 1044 | }; 1045 | errorContext.setupInputParams = function () { 1046 | errorContext.inputParams = [errorContext.out.foo.deep.undefinedValue]; 1047 | }; 1048 | 1049 | errorContext.setupTest(); 1050 | errorContext.setupErrorMessages(); 1051 | errorContext.setupInputParams(); 1052 | 1053 | new Scenario() 1054 | .withEntryPoint(errorContext.entryPointObject, "shouldNotBeFalsey") 1055 | .withInputParams(errorContext.inputParams) 1056 | .test(function (err) { 1057 | expect(err.message).eql(errorContext.expectedErrorMessage); 1058 | }); 1059 | }); 1060 | it("it should fail when value is Nan", function () { 1061 | errorContext.setupErrorMessages = function () { 1062 | errorContext.expectedErrorMessage = new Error(constants.ShouldNotBeFalsey).message; 1063 | }; 1064 | errorContext.setupInputParams = function () { 1065 | errorContext.inputParams = [errorContext.out.foo.deep.NanValue]; 1066 | }; 1067 | 1068 | errorContext.setupTest(); 1069 | errorContext.setupErrorMessages(); 1070 | errorContext.setupInputParams(); 1071 | 1072 | new Scenario() 1073 | .withEntryPoint(errorContext.entryPointObject, "shouldNotBeFalsey") 1074 | .withInputParams(errorContext.inputParams) 1075 | .test(function (err) { 1076 | expect(err.message).eql(errorContext.expectedErrorMessage); 1077 | }); 1078 | }); 1079 | it("it should fail when value is null", function () { 1080 | errorContext.setupErrorMessages = function () { 1081 | errorContext.expectedErrorMessage = new Error(constants.ShouldNotBeFalsey).message; 1082 | }; 1083 | errorContext.setupInputParams = function () { 1084 | errorContext.inputParams = [errorContext.out.foo.deep.nullValue]; 1085 | }; 1086 | 1087 | errorContext.setupTest(); 1088 | errorContext.setupErrorMessages(); 1089 | errorContext.setupInputParams(); 1090 | 1091 | new Scenario() 1092 | .withEntryPoint(errorContext.entryPointObject, "shouldNotBeFalsey") 1093 | .withInputParams(errorContext.inputParams) 1094 | .test(function (err) { 1095 | expect(err.message).eql(errorContext.expectedErrorMessage); 1096 | }); 1097 | }); 1098 | 1099 | it("it should pass when value is NOT falsey", function () { 1100 | errorContext.setupInputParams = function () { 1101 | errorContext.inputParams = [errorContext.out.foo.deep.stringValue]; 1102 | }; 1103 | 1104 | errorContext.setupTest(); 1105 | errorContext.setupInputParams(); 1106 | 1107 | new Scenario() 1108 | .withEntryPoint(errorContext.entryPointObject, "shouldNotBeFalsey") 1109 | .withInputParams(errorContext.inputParams) 1110 | .test(function (err) { 1111 | expect(err).to.be.undefined; // eslint-disable-line 1112 | }); 1113 | }); 1114 | }); 1115 | 1116 | describe("shouldBeFalsy", function () { 1117 | it("it should fail when value is NOT falsy", function () { 1118 | errorContext.setupErrorMessages = function () { 1119 | errorContext.expectedErrorMessage = new Error(constants.ShouldBeFalsey).message; 1120 | }; 1121 | errorContext.setupInputParams = function () { 1122 | errorContext.inputParams = [errorContext.out.foo.deep.stringValue]; 1123 | }; 1124 | 1125 | errorContext.setupTest(); 1126 | errorContext.setupErrorMessages(); 1127 | errorContext.setupInputParams(); 1128 | 1129 | new Scenario() 1130 | .withEntryPoint(errorContext.entryPointObject, "shouldBeFalsy") 1131 | .withInputParams(errorContext.inputParams) 1132 | .test(function (err) { 1133 | expect(err.message).eql(errorContext.expectedErrorMessage); 1134 | }); 1135 | }); 1136 | 1137 | it("it should pass when value is undefined", function () { 1138 | errorContext.setupInputParams = function () { 1139 | errorContext.inputParams = [errorContext.out.foo.deep.undefinedValue]; 1140 | }; 1141 | 1142 | errorContext.setupTest(); 1143 | errorContext.setupInputParams(); 1144 | 1145 | new Scenario() 1146 | .withEntryPoint(errorContext.entryPointObject, "shouldBeFalsy") 1147 | .withInputParams(errorContext.inputParams) 1148 | .test(function (err) { 1149 | expect(err).to.be.undefined; // eslint-disable-line 1150 | }); 1151 | }); 1152 | it("it should pass when value is Nan", function () { 1153 | errorContext.setupInputParams = function () { 1154 | errorContext.inputParams = [errorContext.out.foo.deep.NanValue]; 1155 | }; 1156 | 1157 | errorContext.setupTest(); 1158 | errorContext.setupInputParams(); 1159 | 1160 | new Scenario() 1161 | .withEntryPoint(errorContext.entryPointObject, "shouldBeFalsy") 1162 | .withInputParams(errorContext.inputParams) 1163 | .test(function (err) { 1164 | expect(err).to.be.undefined; // eslint-disable-line 1165 | }); 1166 | }); 1167 | it("it should pass when value is null", function () { 1168 | errorContext.setupInputParams = function () { 1169 | errorContext.inputParams = [errorContext.out.foo.deep.nullValue]; 1170 | }; 1171 | 1172 | errorContext.setupTest(); 1173 | errorContext.setupInputParams(); 1174 | 1175 | new Scenario() 1176 | .withEntryPoint(errorContext.entryPointObject, "shouldBeFalsy") 1177 | .withInputParams(errorContext.inputParams) 1178 | .test(function (err) { 1179 | expect(err).to.be.undefined; // eslint-disable-line 1180 | }); 1181 | }); 1182 | }); 1183 | describe("shouldNotBeFalsy", function () { 1184 | it("it should fail when value is undefined", function () { 1185 | errorContext.setupErrorMessages = function () { 1186 | errorContext.expectedErrorMessage = new Error(constants.ShouldNotBeFalsey).message; 1187 | }; 1188 | errorContext.setupInputParams = function () { 1189 | errorContext.inputParams = [errorContext.out.foo.deep.undefinedValue]; 1190 | }; 1191 | 1192 | errorContext.setupTest(); 1193 | errorContext.setupErrorMessages(); 1194 | errorContext.setupInputParams(); 1195 | 1196 | new Scenario() 1197 | .withEntryPoint(errorContext.entryPointObject, "shouldNotBeFalsy") 1198 | .withInputParams(errorContext.inputParams) 1199 | .test(function (err) { 1200 | expect(err.message).eql(errorContext.expectedErrorMessage); 1201 | }); 1202 | }); 1203 | it("it should fail when value is Nan", function () { 1204 | errorContext.setupErrorMessages = function () { 1205 | errorContext.expectedErrorMessage = new Error(constants.ShouldNotBeFalsey).message; 1206 | }; 1207 | errorContext.setupInputParams = function () { 1208 | errorContext.inputParams = [errorContext.out.foo.deep.NanValue]; 1209 | }; 1210 | 1211 | errorContext.setupTest(); 1212 | errorContext.setupErrorMessages(); 1213 | errorContext.setupInputParams(); 1214 | 1215 | new Scenario() 1216 | .withEntryPoint(errorContext.entryPointObject, "shouldNotBeFalsy") 1217 | .withInputParams(errorContext.inputParams) 1218 | .test(function (err) { 1219 | expect(err.message).eql(errorContext.expectedErrorMessage); 1220 | }); 1221 | }); 1222 | it("it should fail when value is null", function () { 1223 | errorContext.setupErrorMessages = function () { 1224 | errorContext.expectedErrorMessage = new Error(constants.ShouldNotBeFalsey).message; 1225 | }; 1226 | errorContext.setupInputParams = function () { 1227 | errorContext.inputParams = [errorContext.out.foo.deep.nullValue]; 1228 | }; 1229 | 1230 | errorContext.setupTest(); 1231 | errorContext.setupErrorMessages(); 1232 | errorContext.setupInputParams(); 1233 | 1234 | new Scenario() 1235 | .withEntryPoint(errorContext.entryPointObject, "shouldNotBeFalsy") 1236 | .withInputParams(errorContext.inputParams) 1237 | .test(function (err) { 1238 | expect(err.message).eql(errorContext.expectedErrorMessage); 1239 | }); 1240 | }); 1241 | 1242 | it("it should pass when value is NOT falsy", function () { 1243 | errorContext.setupInputParams = function () { 1244 | errorContext.inputParams = [errorContext.out.foo.deep.stringValue]; 1245 | }; 1246 | 1247 | errorContext.setupTest(); 1248 | errorContext.setupInputParams(); 1249 | 1250 | new Scenario() 1251 | .withEntryPoint(errorContext.entryPointObject, "shouldNotBeFalsy") 1252 | .withInputParams(errorContext.inputParams) 1253 | .test(function (err) { 1254 | expect(err).to.be.undefined; // eslint-disable-line 1255 | }); 1256 | }); 1257 | }); 1258 | 1259 | describe("shouldBeTruthy", function () { 1260 | it("it should fail when value is undefined", function () { 1261 | errorContext.setupErrorMessages = function () { 1262 | errorContext.expectedErrorMessage = new Error(constants.ShouldNotBeFalsey).message; 1263 | }; 1264 | errorContext.setupInputParams = function () { 1265 | errorContext.inputParams = [errorContext.out.foo.deep.undefinedValue]; 1266 | }; 1267 | 1268 | errorContext.setupTest(); 1269 | errorContext.setupErrorMessages(); 1270 | errorContext.setupInputParams(); 1271 | 1272 | new Scenario() 1273 | .withEntryPoint(errorContext.entryPointObject, "shouldBeTruthy") 1274 | .withInputParams(errorContext.inputParams) 1275 | .test(function (err) { 1276 | expect(err.message).eql(errorContext.expectedErrorMessage); 1277 | }); 1278 | }); 1279 | it("it should fail when value is Nan", function () { 1280 | errorContext.setupErrorMessages = function () { 1281 | errorContext.expectedErrorMessage = new Error(constants.ShouldNotBeFalsey).message; 1282 | }; 1283 | errorContext.setupInputParams = function () { 1284 | errorContext.inputParams = [errorContext.out.foo.deep.NanValue]; 1285 | }; 1286 | 1287 | errorContext.setupTest(); 1288 | errorContext.setupErrorMessages(); 1289 | errorContext.setupInputParams(); 1290 | 1291 | new Scenario() 1292 | .withEntryPoint(errorContext.entryPointObject, "shouldBeTruthy") 1293 | .withInputParams(errorContext.inputParams) 1294 | .test(function (err) { 1295 | expect(err.message).eql(errorContext.expectedErrorMessage); 1296 | }); 1297 | }); 1298 | it("it should fail when value is null", function () { 1299 | errorContext.setupErrorMessages = function () { 1300 | errorContext.expectedErrorMessage = new Error(constants.ShouldNotBeFalsey).message; 1301 | }; 1302 | errorContext.setupInputParams = function () { 1303 | errorContext.inputParams = [errorContext.out.foo.deep.nullValue]; 1304 | }; 1305 | 1306 | errorContext.setupTest(); 1307 | errorContext.setupErrorMessages(); 1308 | errorContext.setupInputParams(); 1309 | 1310 | new Scenario() 1311 | .withEntryPoint(errorContext.entryPointObject, "shouldBeTruthy") 1312 | .withInputParams(errorContext.inputParams) 1313 | .test(function (err) { 1314 | expect(err.message).eql(errorContext.expectedErrorMessage); 1315 | }); 1316 | }); 1317 | 1318 | it("it should pass when value is truthy", function () { 1319 | errorContext.setupInputParams = function () { 1320 | errorContext.inputParams = [errorContext.out.foo.deep.stringValue]; 1321 | }; 1322 | 1323 | errorContext.setupTest(); 1324 | errorContext.setupInputParams(); 1325 | 1326 | new Scenario() 1327 | .withEntryPoint(errorContext.entryPointObject, "shouldBeTruthy") 1328 | .withInputParams(errorContext.inputParams) 1329 | .test(function (err) { 1330 | expect(err).to.be.undefined; // eslint-disable-line 1331 | }); 1332 | }); 1333 | }); 1334 | describe("shouldNotBeTruthy", function () { 1335 | it("it should fail when value is truthy", function () { 1336 | errorContext.setupErrorMessages = function () { 1337 | errorContext.expectedErrorMessage = new Error(constants.ShouldBeFalsey).message; 1338 | }; 1339 | errorContext.setupInputParams = function () { 1340 | errorContext.inputParams = [errorContext.out.foo.deep.stringValue]; 1341 | }; 1342 | 1343 | errorContext.setupTest(); 1344 | errorContext.setupErrorMessages(); 1345 | errorContext.setupInputParams(); 1346 | 1347 | new Scenario() 1348 | .withEntryPoint(errorContext.entryPointObject, "shouldNotBeTruthy") 1349 | .withInputParams(errorContext.inputParams) 1350 | .test(function (err) { 1351 | expect(err.message).eql(errorContext.expectedErrorMessage); 1352 | }); 1353 | }); 1354 | 1355 | it("it should pass when value is undefined", function () { 1356 | errorContext.setupInputParams = function () { 1357 | errorContext.inputParams = [errorContext.out.foo.deep.undefinedValue]; 1358 | }; 1359 | 1360 | errorContext.setupTest(); 1361 | errorContext.setupInputParams(); 1362 | 1363 | new Scenario() 1364 | .withEntryPoint(errorContext.entryPointObject, "shouldNotBeTruthy") 1365 | .withInputParams(errorContext.inputParams) 1366 | .test(function (err) { 1367 | expect(err).to.be.undefined; // eslint-disable-line 1368 | }); 1369 | }); 1370 | it("it should pass when value is Nan", function () { 1371 | errorContext.setupInputParams = function () { 1372 | errorContext.inputParams = [errorContext.out.foo.deep.NanValue]; 1373 | }; 1374 | 1375 | errorContext.setupTest(); 1376 | errorContext.setupInputParams(); 1377 | 1378 | new Scenario() 1379 | .withEntryPoint(errorContext.entryPointObject, "shouldNotBeTruthy") 1380 | .withInputParams(errorContext.inputParams) 1381 | .test(function (err) { 1382 | expect(err).to.be.undefined; // eslint-disable-line 1383 | }); 1384 | }); 1385 | it("it should pass when value is null", function () { 1386 | errorContext.setupInputParams = function () { 1387 | errorContext.inputParams = [errorContext.out.foo.deep.nullValue]; 1388 | }; 1389 | 1390 | errorContext.setupTest(); 1391 | errorContext.setupInputParams(); 1392 | 1393 | new Scenario() 1394 | .withEntryPoint(errorContext.entryPointObject, "shouldNotBeTruthy") 1395 | .withInputParams(errorContext.inputParams) 1396 | .test(function (err) { 1397 | expect(err).to.be.undefined; // eslint-disable-line 1398 | }); 1399 | }); 1400 | }); 1401 | 1402 | describe("checkArgument", function () { 1403 | it("it should fail when value is false (i.e. is an illegal argument)", function () { 1404 | errorContext.setupErrorMessages = function () { 1405 | errorContext.expectedErrorMessage = new Error(constants.IllegalArgument).message; 1406 | }; 1407 | errorContext.setupInputParams = function () { 1408 | errorContext.inputParams = [false]; 1409 | }; 1410 | 1411 | errorContext.setupTest(); 1412 | errorContext.setupErrorMessages(); 1413 | errorContext.setupInputParams(); 1414 | 1415 | new Scenario() 1416 | .withEntryPoint(errorContext.entryPointObject, "checkArgument") 1417 | .withInputParams(errorContext.inputParams) 1418 | .test(function (err) { 1419 | expect(err.message).eql(errorContext.expectedErrorMessage); 1420 | }); 1421 | }); 1422 | 1423 | it("it should pass when value is true", function () { 1424 | errorContext.setupInputParams = function () { 1425 | errorContext.inputParams = [true]; 1426 | }; 1427 | 1428 | errorContext.setupTest(); 1429 | errorContext.setupInputParams(); 1430 | 1431 | new Scenario() 1432 | .withEntryPoint(errorContext.entryPointObject, "checkArgument") 1433 | .withInputParams(errorContext.inputParams) 1434 | .test(function (err) { 1435 | expect(err).to.be.undefined; // eslint-disable-line 1436 | }); 1437 | }); 1438 | }); 1439 | 1440 | describe("checkState", function () { 1441 | it("it should fail when value is false (i.e. is an illegal argument)", function () { 1442 | errorContext.setupErrorMessages = function () { 1443 | errorContext.expectedErrorMessage = new Error(constants.IllegalState).message; 1444 | }; 1445 | errorContext.setupInputParams = function () { 1446 | errorContext.inputParams = [false]; 1447 | }; 1448 | 1449 | errorContext.setupTest(); 1450 | errorContext.setupErrorMessages(); 1451 | errorContext.setupInputParams(); 1452 | 1453 | new Scenario() 1454 | .withEntryPoint(errorContext.entryPointObject, "checkState") 1455 | .withInputParams(errorContext.inputParams) 1456 | .test(function (err) { 1457 | expect(err.message).eql(errorContext.expectedErrorMessage); 1458 | }); 1459 | }); 1460 | 1461 | it("it should pass when value is true", function () { 1462 | errorContext.setupInputParams = function () { 1463 | errorContext.inputParams = [true]; 1464 | }; 1465 | 1466 | errorContext.setupTest(); 1467 | errorContext.setupInputParams(); 1468 | 1469 | new Scenario() 1470 | .withEntryPoint(errorContext.entryPointObject, "checkState") 1471 | .withInputParams(errorContext.inputParams) 1472 | .test(function (err) { 1473 | expect(err).to.be.undefined; // eslint-disable-line 1474 | }); 1475 | }); 1476 | }); 1477 | 1478 | describe("checkElementIndex", function () { 1479 | it("it should fail when index is less than 0", function () { 1480 | errorContext.setupErrorMessages = function () { 1481 | errorContext.expectedErrorMessage = new Error(constants.ShouldHaveValidIndex).message; 1482 | }; 1483 | errorContext.setupInputParams = function () { 1484 | errorContext.inputParams = [-4, 10]; 1485 | }; 1486 | 1487 | errorContext.setupTest(); 1488 | errorContext.setupErrorMessages(); 1489 | errorContext.setupInputParams(); 1490 | 1491 | new Scenario() 1492 | .withEntryPoint(errorContext.entryPointObject, "checkElementIndex") 1493 | .withInputParams(errorContext.inputParams) 1494 | .test(function (err) { 1495 | expect(err.message).eql(errorContext.expectedErrorMessage); 1496 | }); 1497 | }); 1498 | it("it should fail when index is greater than size", function () { 1499 | errorContext.setupErrorMessages = function () { 1500 | errorContext.expectedErrorMessage = new Error(constants.ShouldHaveValidIndex).message; 1501 | }; 1502 | errorContext.setupInputParams = function () { 1503 | errorContext.inputParams = [12, 10]; 1504 | }; 1505 | 1506 | errorContext.setupTest(); 1507 | errorContext.setupErrorMessages(); 1508 | errorContext.setupInputParams(); 1509 | 1510 | new Scenario() 1511 | .withEntryPoint(errorContext.entryPointObject, "checkElementIndex") 1512 | .withInputParams(errorContext.inputParams) 1513 | .test(function (err) { 1514 | expect(err.message).eql(errorContext.expectedErrorMessage); 1515 | }); 1516 | }); 1517 | it("it should fail when index is equal to size", function () { 1518 | errorContext.setupErrorMessages = function () { 1519 | errorContext.expectedErrorMessage = new Error(constants.ShouldHaveValidIndex).message; 1520 | }; 1521 | errorContext.setupInputParams = function () { 1522 | errorContext.inputParams = [10, 10]; 1523 | }; 1524 | 1525 | errorContext.setupTest(); 1526 | errorContext.setupErrorMessages(); 1527 | errorContext.setupInputParams(); 1528 | 1529 | new Scenario() 1530 | .withEntryPoint(errorContext.entryPointObject, "checkElementIndex") 1531 | .withInputParams(errorContext.inputParams) 1532 | .test(function (err) { 1533 | expect(err.message).eql(errorContext.expectedErrorMessage); 1534 | }); 1535 | }); 1536 | 1537 | it("it should pass when value is greater than zero and less than size", function () { 1538 | errorContext.setupInputParams = function () { 1539 | errorContext.inputParams = [8, 10]; 1540 | }; 1541 | 1542 | errorContext.setupTest(); 1543 | errorContext.setupInputParams(); 1544 | 1545 | new Scenario() 1546 | .withEntryPoint(errorContext.entryPointObject, "checkElementIndex") 1547 | .withInputParams(errorContext.inputParams) 1548 | .test(function (err) { 1549 | expect(err).to.be.undefined; // eslint-disable-line 1550 | }); 1551 | }); 1552 | it("it should pass when value is equal to zero", function () { 1553 | errorContext.setupInputParams = function () { 1554 | errorContext.inputParams = [0, 10]; 1555 | }; 1556 | 1557 | errorContext.setupTest(); 1558 | errorContext.setupInputParams(); 1559 | 1560 | new Scenario() 1561 | .withEntryPoint(errorContext.entryPointObject, "checkElementIndex") 1562 | .withInputParams(errorContext.inputParams) 1563 | .test(function (err) { 1564 | expect(err).to.be.undefined; // eslint-disable-line 1565 | }); 1566 | }); 1567 | }); 1568 | 1569 | describe("checkPositionIndex", function () { 1570 | it("it should fail when index is less than 0", function () { 1571 | errorContext.setupErrorMessages = function () { 1572 | errorContext.expectedErrorMessage = new Error(constants.ShouldHaveValidPosition).message; 1573 | }; 1574 | errorContext.setupInputParams = function () { 1575 | errorContext.inputParams = [-4, 10]; 1576 | }; 1577 | 1578 | errorContext.setupTest(); 1579 | errorContext.setupErrorMessages(); 1580 | errorContext.setupInputParams(); 1581 | 1582 | new Scenario() 1583 | .withEntryPoint(errorContext.entryPointObject, "checkPositionIndex") 1584 | .withInputParams(errorContext.inputParams) 1585 | .test(function (err) { 1586 | expect(err.message).eql(errorContext.expectedErrorMessage); 1587 | }); 1588 | }); 1589 | it("it should fail when index is greater than size", function () { 1590 | errorContext.setupErrorMessages = function () { 1591 | errorContext.expectedErrorMessage = new Error(constants.ShouldHaveValidPosition).message; 1592 | }; 1593 | errorContext.setupInputParams = function () { 1594 | errorContext.inputParams = [12, 10]; 1595 | }; 1596 | 1597 | errorContext.setupTest(); 1598 | errorContext.setupErrorMessages(); 1599 | errorContext.setupInputParams(); 1600 | 1601 | new Scenario() 1602 | .withEntryPoint(errorContext.entryPointObject, "checkPositionIndex") 1603 | .withInputParams(errorContext.inputParams) 1604 | .test(function (err) { 1605 | expect(err.message).eql(errorContext.expectedErrorMessage); 1606 | }); 1607 | }); 1608 | 1609 | it("it should pass when value is greater than zero and less than size", function () { 1610 | errorContext.setupInputParams = function () { 1611 | errorContext.inputParams = [8, 10]; 1612 | }; 1613 | 1614 | errorContext.setupTest(); 1615 | errorContext.setupInputParams(); 1616 | 1617 | new Scenario() 1618 | .withEntryPoint(errorContext.entryPointObject, "checkPositionIndex") 1619 | .withInputParams(errorContext.inputParams) 1620 | .test(function (err) { 1621 | expect(err).to.be.undefined; // eslint-disable-line 1622 | }); 1623 | }); 1624 | it("it should pass when value is equal to zero", function () { 1625 | errorContext.setupInputParams = function () { 1626 | errorContext.inputParams = [0, 10]; 1627 | }; 1628 | 1629 | errorContext.setupTest(); 1630 | errorContext.setupInputParams(); 1631 | 1632 | new Scenario() 1633 | .withEntryPoint(errorContext.entryPointObject, "checkPositionIndex") 1634 | .withInputParams(errorContext.inputParams) 1635 | .test(function (err) { 1636 | expect(err).to.be.undefined; // eslint-disable-line 1637 | }); 1638 | }); 1639 | it("it should pass when value is equal to size", function () { 1640 | errorContext.setupInputParams = function () { 1641 | errorContext.inputParams = [10, 10]; 1642 | }; 1643 | 1644 | errorContext.setupTest(); 1645 | errorContext.setupInputParams(); 1646 | 1647 | new Scenario() 1648 | .withEntryPoint(errorContext.entryPointObject, "checkPositionIndex") 1649 | .withInputParams(errorContext.inputParams) 1650 | .test(function (err) { 1651 | expect(err).to.be.undefined; // eslint-disable-line 1652 | }); 1653 | }); 1654 | }); 1655 | 1656 | describe("checkPositionIndexes", function () { 1657 | it("it should fail when start is less than 0", function () { 1658 | errorContext.setupErrorMessages = function () { 1659 | errorContext.expectedErrorMessage = new Error(constants.ShouldHaveValidPositions).message; 1660 | }; 1661 | errorContext.setupInputParams = function () { 1662 | errorContext.inputParams = [-4, 10, 12]; 1663 | }; 1664 | 1665 | errorContext.setupTest(); 1666 | errorContext.setupErrorMessages(); 1667 | errorContext.setupInputParams(); 1668 | 1669 | new Scenario() 1670 | .withEntryPoint(errorContext.entryPointObject, "checkPositionIndexes") 1671 | .withInputParams(errorContext.inputParams) 1672 | .test(function (err) { 1673 | expect(err.message).eql(errorContext.expectedErrorMessage); 1674 | }); 1675 | }); 1676 | it("it should fail when end is less than start", function () { 1677 | errorContext.setupErrorMessages = function () { 1678 | errorContext.expectedErrorMessage = new Error(constants.ShouldHaveValidPositions).message; 1679 | }; 1680 | errorContext.setupInputParams = function () { 1681 | errorContext.inputParams = [5, 3, 12]; 1682 | }; 1683 | 1684 | errorContext.setupTest(); 1685 | errorContext.setupErrorMessages(); 1686 | errorContext.setupInputParams(); 1687 | 1688 | new Scenario() 1689 | .withEntryPoint(errorContext.entryPointObject, "checkPositionIndexes") 1690 | .withInputParams(errorContext.inputParams) 1691 | .test(function (err) { 1692 | expect(err.message).eql(errorContext.expectedErrorMessage); 1693 | }); 1694 | }); 1695 | it("it should fail when end is greater than size", function () { 1696 | errorContext.setupErrorMessages = function () { 1697 | errorContext.expectedErrorMessage = new Error(constants.ShouldHaveValidPositions).message; 1698 | }; 1699 | errorContext.setupInputParams = function () { 1700 | errorContext.inputParams = [3, 13, 12]; 1701 | }; 1702 | 1703 | errorContext.setupTest(); 1704 | errorContext.setupErrorMessages(); 1705 | errorContext.setupInputParams(); 1706 | 1707 | new Scenario() 1708 | .withEntryPoint(errorContext.entryPointObject, "checkPositionIndexes") 1709 | .withInputParams(errorContext.inputParams) 1710 | .test(function (err) { 1711 | expect(err.message).eql(errorContext.expectedErrorMessage); 1712 | }); 1713 | }); 1714 | 1715 | describe("start is greater than 0", function () { 1716 | it("it should pass when end is less than size and greater than start", function () { 1717 | errorContext.setupInputParams = function () { 1718 | errorContext.inputParams = [2, 10, 12]; 1719 | }; 1720 | 1721 | errorContext.setupTest(); 1722 | errorContext.setupInputParams(); 1723 | 1724 | new Scenario() 1725 | .withEntryPoint(errorContext.entryPointObject, "checkPositionIndexes") 1726 | .withInputParams(errorContext.inputParams) 1727 | .test(function (err) { 1728 | expect(err).to.be.undefined; // eslint-disable-line 1729 | }); 1730 | }); 1731 | it("it should pass when end is equal to size and greater than start", function () { 1732 | errorContext.setupInputParams = function () { 1733 | errorContext.inputParams = [2, 12, 12]; 1734 | }; 1735 | 1736 | errorContext.setupTest(); 1737 | errorContext.setupInputParams(); 1738 | 1739 | new Scenario() 1740 | .withEntryPoint(errorContext.entryPointObject, "checkPositionIndexes") 1741 | .withInputParams(errorContext.inputParams) 1742 | .test(function (err) { 1743 | expect(err).to.be.undefined; // eslint-disable-line 1744 | }); 1745 | }); 1746 | }); 1747 | describe("start is equal 0", function () { 1748 | it("it should pass when end is less than size and greater than start", function () { 1749 | errorContext.setupInputParams = function () { 1750 | errorContext.inputParams = [0, 10, 12]; 1751 | }; 1752 | 1753 | errorContext.setupTest(); 1754 | errorContext.setupInputParams(); 1755 | 1756 | new Scenario() 1757 | .withEntryPoint(errorContext.entryPointObject, "checkPositionIndexes") 1758 | .withInputParams(errorContext.inputParams) 1759 | .test(function (err) { 1760 | expect(err).to.be.undefined; // eslint-disable-line 1761 | }); 1762 | }); 1763 | it("it should pass when end is equal to size and greater than start", function () { 1764 | errorContext.setupInputParams = function () { 1765 | errorContext.inputParams = [0, 12, 12]; 1766 | }; 1767 | 1768 | errorContext.setupTest(); 1769 | errorContext.setupInputParams(); 1770 | 1771 | new Scenario() 1772 | .withEntryPoint(errorContext.entryPointObject, "checkPositionIndexes") 1773 | .withInputParams(errorContext.inputParams) 1774 | .test(function (err) { 1775 | expect(err).to.be.undefined; // eslint-disable-line 1776 | }); 1777 | }); 1778 | }); 1779 | }); 1780 | 1781 | describe("when chaining commands", function () { 1782 | 1783 | it("it should work using chaining commands for success", function () { 1784 | errorContext.setupChain = function () { 1785 | let stringValue = errorContext.out.foo.deep.stringValue; 1786 | let numberValue = errorContext.out.foo.deep.numberValue; 1787 | 1788 | errorContext.entryPointObject = errorContext.sut.shouldNotBeFalsey(stringValue) 1789 | .shouldBeDefined(stringValue) 1790 | .shouldBeString(stringValue) 1791 | .shouldNotBeFalsey(numberValue) 1792 | .shouldBeDefined(numberValue); 1793 | }; 1794 | 1795 | errorContext.setupInputParams = function () { 1796 | errorContext.inputParams = [errorContext.out.foo.deep.numberValue]; 1797 | }; 1798 | 1799 | errorContext.setupTest(); 1800 | errorContext.setupInputParams(); 1801 | errorContext.setupChain(); 1802 | 1803 | new Scenario() 1804 | .withEntryPoint(errorContext.entryPointObject, "shouldBeNumber") 1805 | .withInputParams(errorContext.inputParams) 1806 | .test(function (err) { 1807 | expect(err).to.be.undefined; // eslint-disable-line 1808 | }); 1809 | }); 1810 | 1811 | it("it should work using chaining commands when fail at end of chain", function () { 1812 | errorContext.setupErrorMessages = function () { 1813 | errorContext.expectedErrorMessage = new Error(constants.ShouldBeNumber).message; 1814 | }; 1815 | errorContext.setupChain = function () { 1816 | let stringValue = errorContext.out.foo.deep.stringValue; 1817 | let numberValue = errorContext.out.foo.deep.numberValue; 1818 | 1819 | errorContext.entryPointObject = errorContext.sut.shouldNotBeFalsey(stringValue) 1820 | .shouldBeDefined(stringValue) 1821 | .shouldBeString(stringValue) 1822 | .shouldNotBeFalsey(numberValue) 1823 | .shouldBeDefined(numberValue); 1824 | }; 1825 | 1826 | errorContext.setupInputParams = function () { 1827 | errorContext.inputParams = [errorContext.out.foo.deep.stringValue]; 1828 | }; 1829 | 1830 | errorContext.setupTest(); 1831 | errorContext.setupErrorMessages(); 1832 | errorContext.setupInputParams(); 1833 | errorContext.setupChain(); 1834 | 1835 | new Scenario() 1836 | .withEntryPoint(errorContext.entryPointObject, "shouldBeNumber") 1837 | .withInputParams(errorContext.inputParams) 1838 | .test(function (err) { 1839 | expect(err.message).eql(errorContext.expectedErrorMessage); 1840 | }); 1841 | }); 1842 | }); 1843 | }); 1844 | --------------------------------------------------------------------------------