Code coverage report for src/Common/Schema/Mixins/SchemaExecution.js

Statements: 100% (40 / 40)      Branches: 95.65% (22 / 23)      Functions: 100% (4 / 4)      Lines: 100% (40 / 40)      Ignored: none     

All files » src/Common/Schema/Mixins/ » SchemaExecution.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152    1                                     1     141     141 141 117     24 24         117 10       107 12       95 60         35 35     4 4       31     31     64     64 4 4       60     60 2       58       31                     1                           1     134       134     134       32   7       7 7             134           1    
"use strict";
 
var _                       = require("lodash"),
    Safe                    = require("../../Safe"),
    Type                    = require("../../Type"),
    Assert                  = require("../../Assert"),
    Exception               = require("../../Exception/Exception"),
    Types                   = require("../Types"),
    SchemaResult            = require("../SchemaResult"),
    SchemaExecutionHelper   = require("./SchemaExecutionHelper");
 
/**
 *
 * Iterate over the complex value
 *
 * @param  {SchemaDefinition} schema
 * @param  {*} value
 *
 * @return {SchemaResult}
 *
 */
var execute = function(schema, value, validationFns){
 
    /* jshint -W064 */
    var result = SchemaResult(schema, value);
 
    /// get the value from the schema
    try {
        value = SchemaExecutionHelper.prepareValue(schema, value, validationFns);
        result.setValue(value);
    }
    catch(e){
        result.addError(e);
        return result;
    }
 
    /// if value is null there's no need to iterate
    /* jshint -W041 */
    if(value == null){
        return result;
    }
 
    /// if is expecting any value return it straight away
    if(schema.any){
        return result;
    }
 
    /// if is not a complex value return the value
    if(!schema.isArray() && !schema.isObject()){
        return result;
    }
 
    /// prepare the schema for execution against the value. This will expand
    /// the schema to match the value
    try {
        schema = SchemaExecutionHelper.prepareSchema(schema, value, validationFns);
    }
    catch(e){
        result.addError(e);
        return result;
    }
 
    /// reset the result value before iterating
    result = SchemaResult(schema, schema.isObject() ? {} : []);
 
    /// recursion over the inner values of the schema
    _.each(schema.schema, function(innerSchema, key){
 
        /// recursive execute the schema
        var innerResult = innerSchema.execute(value[key], validationFns);
 
        /// add result to errors
        if(!innerResult.isValid()){
            result.addError(innerResult.getErrors());
            return;
        }
 
        /// get the inner result value
        var innerValue = innerResult.getValue();
 
        /// if is an object and value is not required ignore!
        if(schema.isObject() && innerValue == null && !innerSchema.required){
            return;
        }
 
        /// set the value
        result.setValue(innerValue, { index: key });
 
    });
 
    return result;
 
};
 
/**
 *
 * Schema execution methods.
 *
 * @type {Object}
 *
 */
var Execution = function(){
 
    /**
     *
     * Execute Schema against the value. This will perform a top-down recursion on
     * the given structure.
     *
     * @param {SchemaDefinition} schema
     * @param {*} value
     * @param {Object} options
     *
     * @return {SchemaResult}
     *
     */
    this.execute = function(value, validationFns){
 
        /// validate and normalize arguments
        Assert.instanceOf(Types.SchemaDefinition)
            .assert(this);
 
        /// if it has no errors iterate over the object
        var result = execute(this, value, validationFns);
 
        /// if there's error try to recover by applying the default value
        if(!result.isValid()){
 
            /// try to fallback to the default value if it can
            /* jshint -W041 */
            if(this.default != null){
 
                var dresult = execute(this, this.default, validationFns);
 
                /// if default value is valid use it, otherwise use the given
                /// value
                Eif(dresult.isValid()){
                    result = dresult;
                }
 
            }
 
        }
 
        return result;
 
    };
 
};
 
module.exports = Execution;