Code coverage report for src/Common/Schema/SchemaResultNode.js

Statements: 93.33% (28 / 30)      Branches: 100% (26 / 26)      Functions: 50% (3 / 6)      Lines: 93.33% (28 / 30)      Ignored: none     

All files » src/Common/Schema/ » SchemaResultNode.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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186    1                                         1   179                   178                 178                   178     178 32   146 44                     178                 178                     2                                   9         9 2         7 3         4 1       3 1   2 1     1     3                                                                           178       1  
"use strict";
 
var _                   = require("lodash"),
    Type                = require("../Type"),
    Safe                = require("../Safe"),
    Assert              = require("../Assert"),
    ExceptionList       = require("../Exception/ExceptionList"),
    Types               = require("./Types");
 
/**
 *
 * SchemaResultNode stores a Schema evaluation result of a given hierarchical item.
 *
 * - Each inner value of a complex object must be an instance of SchemaResultNode.
 * - Each error of a SchemaResultNode is associated with the value itself and not
 * his inner values.
 *
 *
 * @class
 *
 * @param {SchemaDefinition} schema
 *
 */
var SchemaResultNode = function(schema) {
 
    Assert.instanceOf(Types.SchemaDefinition)
        .assert(schema);
 
    /**
     *
     * Is the value an array flag
     *
     * @type {Boolean}
     *
     */
    var isArray = Type.isArray(schema.schema);
 
    /**
     *
     * Is the value an array flag
     *
     * @type {Boolean}
     *
     */
    var isObject = Type.isObject(schema.schema);
 
    /**
     *
     * The schema associated value. This value will hold a
     * SchemaResultNode.
     *
     * @type {*}
     *
     */
    var value = null;
 
    /// initialize the value with the expected type
    if(isArray){
        value = [];
    }
    else if(isObject){
        value = {};
    }
 
    /**
     *
     * The list of errors associated with the Schema
     * conversion.
     *
     * @type {ExceptionList}
     *
     */
    var errors = new ExceptionList();
 
    /**
     *
     * Module API
     *
     * @type {Object}
     *
     */
    var self = {
 
        /**
         *
         * The schema value being evaluated.
         * Returns a cloned copy of the value is returned.
         *
         * @return {*}
         *
         */
        getValue: function(){
            return _.clone(value);
        },
 
        /**
         *
         * Set the SchemaResultNode for the given value.
         *
         * @throws {Error} If val is not an instance of SchemaResultNode
         * 
         * @param {SchemaResultNode} val
         * @param {*} index
         *
         * @return {*} The added SchemaResultNode
         *
         */
        setValue: function(val, index){
 
            /// validate setValue arguments
            var isIndexValid     = Type.isNumber(index) || Type.isString(index),
                isSchemaResultNode = val instanceof Types.SchemaResultNode;
 
            /// if current schema is expecting an index and the given index is not valid or
            /// not a SchemaResultNode, throw Error
            if(isObject && (!isIndexValid || !isSchemaResultNode)){
                throw new Error("Invalid operation!");
            }
 
            /// if the value is an array the value should be SchemaResultNode and
            /// no index should be specified
            if(isArray && (!isSchemaResultNode || isIndexValid)){
                throw new Error("Invalid operation!");
            }
 
            /// if the value is a primitive value the value shouldn't be
            /// a SchemaResultNode and no index should be specified
            if(!isObject && !isArray && (isSchemaResultNode || isIndexValid)){
                throw new Error("Invalid operation!");
            }
 
            /// set the index
            if(isObject){
                value[index] = val;
            }
            else if(isArray){
                value.push(val);
            }
            else {
                value = val;
            }
 
            return val;
        },
 
        /**
         *
         * Get the error list
         *
         * @return {ExceptionList}
         *
         */
        getErrors: function(){
            return errors;
        },
 
        /**
         *
         * Add errors to the
         *
         * @param {Error} error
         *
         */
        addErrors: function(error){
            errors.push(error);
        },
 
        /**
         *
         * Pretty print the current schema value and its errors
         *
         * @return {String}
         *
         */
        toString: function(){
 
        }
 
    };
 
    return new Types.SchemaResultNode(self);
 
};
 
module.exports = SchemaResultNode;