simple-js-validator
    TypeScript icon, indicating that this package has built-in type declarations

    2.0.2 • Public • Published

    NPM version Build Status Coverage Badge

    simple-js-validator

    Perform simple javascript validation like isEmpty and isDefined in a consistent manner.

    See DOCS

    The trials and tribulations of the truthy test

    if (someVar) {
    	// do something
    }
    

    In javascript, the typical truthy test is if(someVar) or if(!someVar). In my opinion, this does not always work as expected. This is probably due to the fact that I use the truthy test to perform two distinct tests. It is used to check if someVar is true and it is used to check if something "exists". But I don't think it behaves as one would expect in either case…it's close, but not 100%

    As a "true" test

    If using this as a test to see if someVar = true, it works as expected…for the most part. It is a little odd to me that an empty object or array "equals" true. Or any integer (except 0), object, array, date, or string also "equals" true. It might be that even though it is a "truthy" test, it is not meant to be "equals" true. That just might be my c# background coming through there.

    As an "existance" test

    If using this as a test to see if something "exists", it really doesn't work as expected either. To me a value of false or 0 exist. And whether you think an empty object or an array "exist" or not…Do you consider being empty existing?…it strikes me as odd that an empty object/array returns true, but an empty string returns false. Doesn't seem consistent.

    I am sure there are reasons why the test performs the way it does and I might be just using it incorrectly, but too many times I have had to go back and debug some if statement because of the above scenarios.

    typeof === undefined

    Furthermore, the typeof undefined test is not how I would expect…or maybe prefer is the better term. Yes, it tells you if it is "undefined", but to me null is not defined. I didn't want to have to perform two checks every time I want to use an object because the following code works fin

    //THIS WORKS                         	|	//THIS FAILS
    var someVar;                         	|	var someVar = null;
    if (typeof someVar !== undefined) {  	|   if (typeof someVar !== undefined) {
    	someVar.foo = "bar"          		|      someVar.foo = "bar"
    }                                    	|   }
    

    And if you expected to have a number and you got NaN…then technically that is not defined. Probably just semantics or my OCD programming.

    simple javascript validators

    From the above scenarios, I created several simple functions that will output exactly what I expect them to. I decided to break apart the "existence" test into isDefined (meaning empty things are defined) and isEmpty (meaning it must contain something).

    In a future version, I will also add a isTrue which will only return true if the value is "true", 1, or true and an isFalse which will only return true if the value is "false", 0, or false

    Visualize….and ATTACK

    Here is a simple table to illustrate the differences.

    Please see the docs/index.html for more details. If link does not work, just open the index.html in the docs folder.

    Installation

    npm install simple-js-validator
    

    Usage

    ###client side (browser)

    <script src="../lib/simple.js.validator.js" ></script>
    <script type="text/javascript">
        $(function(){
        	var someVar;
        	if (sjv.isDefined(someVar)) {
        		// do something
        	}
    
        	if (sjv.isEmpty(someVar)) {
        		// do something else
        	}
    </script>
    

    ###server side (Node)

    var sjv = require('simple-js-validator');
    var someVar;
    if (sjv.isDefined(someVar)) {
        // do something
    }
    
    if (sjv.isEmpty(someVar)) {
        // do something else
    }
    

    Road Map

    1.0 isEmpty, isNotEmpty, isDefined, and isNotDefined functions.
    isTrue function that only returns true if "true", true, or 1 and inverse isFalse function
    isObject, isNotObject, isEmptyObject, and isNotEmptyObject functions
    isArray, isNotArray, isEmptyArray, and isNotEmptyArray functions
    validateFunctionInputs Defined/NotEmpty Sync/Async/CallbackOnError/Promise functions
    Works in client (browser) and server (Node).
    100% unit test coverage.
    Use grunt to build, minify, test, jshint, and publish package.
    Publish to npm with only required files.
    Documentation on installation, use cases, and code examples.
    2.0 isAsExpected where user can define their expected results for each case.
    Update isEmpty, isNotEmpty, isDefined, and isNotDefined functions to use config so user can redefine to their expectations (if different from default). Essentially the same thing as isAsExpected, but this way they can use the easier to read isEmpty, isNotEmpty, isDefined, and isNotDefined names.

    Note to myself

    • Make sure working in dev branch
    • When updates are complete, run yarn test or yarn test:coverage to verify all tests are passing without or with test coverage.
    • Run yarn lint to verify all lint checks are passing.
    • Run yarn grunt bump to update version (grunt bump:patch, grunt bump:minor) or update package.json directly
    • Update release history and version ref at top of sjv js file
    • Then run yarn grunt release.
    • When it is complete, git commit, git push, and git push --tags
    • Wait for travis build confirmation
    • Make pull request to master
    • Wait for travis build confirmation
    • Pull down master locally
    • Just to verify, run yarn test to verify all tests are passing.
    • Run npm publish
    • Go back to dev branch
    • Cele!!!

    Release History

    2.0.0 2021-10-01 Updated to typescript; Removed all validateFunction and validationInput functions; Removed getPropertyByString, setPropertyByString, assertNestedListIsDefined, and assertNestedIsDefined; updated package refs
    1.0.0 2020-01-10 Added isUniqueObject and isNotUniqueObject; removed isDate, isNotDate, isError, and isNotError; updated package refs
    0.13.0 2018-05-07 Added assertNestedListIsDefined and assertNestedIsDefined; updated package refs
    0.12.3 2018-03-21 Fixed bug in setPropertyByString
    0.12.2 2016-04-01 Fixed coverage badge; updated road map
    0.12.1 2016-04-01 Fixed bug in isObject populated object test. Added isNotObject, isEmptyObject, isNotEmptyObject, isNotArray, isEmptyArray, isNotEmptyArray
    0.12.0 2016-04-01 Added more test coverage; wire up coveralls.io
    0.11.5 2015-12-18 Added bower support
    0.11.3 2015-02-05 Added CallbackOnError to the validate methods
    0.10.1 2014-10-02 Added isTrue and isFalse. Minor update to improve error message. Updated all dependencies
    0.9.12 2014-03-10 Added setPropertyByString.
    0.9.9 2013-08-28 Updated isEmpty to handle new Error() correctly.
    Added isDate, isArray, isObject helper functions.
    Added functions to ease the checking of functions inputs/objects.
    Works in client (browser) and server (Node).
    0.9.8 2013/08/15 Improved documentation.
    Works in client (browser) and server (Node).
    0.9.7 2013/07/30 Initial Release.
    isEmpty, isNotEmpty, isDefined, and isNotDefined functions.
    Assert (throw an error) for the functions.
    Works in server.
    Test coverage is high, but not 100%.
    Uses grunt to build, minify, test, jshint, and publish package.
    Also using travis for CI.
    Initial documentation.

    Install

    npm i simple-js-validator

    DownloadsWeekly Downloads

    443

    Version

    2.0.2

    License

    none

    Unpacked Size

    523 kB

    Total Files

    19

    Last publish

    Collaborators

    • dkhunt27