Fighting complexity through functional composition

Code for: http://danbunea.blogspot.ro/2015/02/fighting-complexity-through-functional.html

by danbunea1

HTML

<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.12.0.css">
<script src="http://code.jquery.com/qunit/qunit-1.12.0.js"></script>
  <div id="qunit"></div>
  <div id="qunit-fixture"></div>

JavaScript

//IMPERATIVE
function validate_simplest_json(json){
    var errors = []
    if(!json.measurement){
        errors.push("measurement cannot be missing")
    }
    else{
        if(json.measurement==null){
            errors.push("measurement cannot be null");
        }
        else{
            if(typeof(json.measurement) !== 'string'){
                errors.push("measurement needs to be string");
            }
            else{
                var meas = json.measurement.trim();
                var lenm=meas.length;
                if(lenm==0){
                    errors.push("measurement cannot be an empty string");
                }
                else{
                    if(lenm<3){
                        errors.push("measurement needs at least 3 characters");
                    }
                    else if(lenm>10){
                        errors.push("measurement needs at most 10 characters");
                    }
                    else if(["archived","password"].indexOf(meas.toLowerCase())>-1){
                        errors.push("measurement has a value which is not allowed");
                    }
                }
            }
        }
    
    }
    return errors;
}

function key_exists(json,key){
    return json.hasOwnProperty(key);
}

function value_null(json, key){
    return json[key]==null;
}

function is_string_or_unicode(json, key){
    return typeof(json[key]) === 'string';
}

function is_empty_string(json, key){
    return json[key].trim().length==0;
}





//IMPERATIVE TO FUNCTIONAL -writing the above function in a more linear manner
function validate_simplest_json_imperative_linear(json){

    var errors = []
    var should_exit=false
    var key = "measurement"
    if(!key_exists(json,key)){
        errors.push(key+" cannot be missing");
        should_exit=true;
    }
    
    if(!should_exit){
        if(value_null(json, key)){
            errors.push(key+" cannot be null");
            should_exit=true;
        }
    }
    
   ...