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

Statements: 100% (18 / 18)      Branches: 100% (2 / 2)      Functions: 100% (6 / 6)      Lines: 100% (18 / 18)      Ignored: none     

All files » src/Common/Schema/ » SchemaResult.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    1                                 1     172       172                 172                 172                   22                     32                     270                     107                           175     175 175   175 58     117               172       1  
"use strict";
 
var Safe                = require("../Safe"),
    Type                = require("../Type"),
    Assert              = require("../Assert"),
    ExceptionList       = require("../Exception/ExceptionList"),
    Types               = require("./Types");
 
/**
 *
 * @class
 * Stores the result of a schema evaluation. It will keep reference to
 * the global structure and the evaluation tree as well.
 *
 * @param {*} schema
 * @param {*} value
 *
 *
 */
var SchemaResult = function(schema, value){
 
    /// validate schema
    Assert.instanceOf(Types.SchemaDefinition)
        .assert(schema);
 
    /// make sure its a value
    value = Safe.value(value);
 
    /**
     *
     * Top-down global error tracking
     *
     * @type {ExceptionList}
     *
     */
    var errors = new ExceptionList();
 
    /**
     *
     * SchemaResult API
     *
     * @type {Object}
     *
     */
    var self = {
 
        /**
         *
         * Get the errors
         *
         * @return {ExceptionList}
         *
         */
        getErrors: function(){
            return errors;
        },
 
        /**
         *
         * Add the given errors
         *
         * @param {ExceptionList} error
         *
         */
        addError: function(error){
            errors.push(error);
        },
 
        /**
         *
         * Checks if the result is valid
         *
         * @return {Boolean}
         *
         */
        isValid: function(){
            return errors.length === 0;
        },
 
        /**
         *
         * Get the value result.
         *
         * @return {*}
         *
         */
        getValue: function(){
            return value;
        },
 
        /**
         *
         * Set the value of the given index.
         *
         * @param {*} value
         * @param {Object} options
         *
         */
        setValue: function(val, options) {
 
            /// normalize val
            val = Safe.value(val);
 
            /// normalize options
            options = Safe.object(options);
            options.index   = Safe.value(options.index, null);
 
            if(options.index !== null){
                value[options.index] = val;
            }
            else {
                value = val;
            }
 
        }
 
    };
 
    /// expose module as 'Typed' SchemaResult instance
    return new Types.SchemaResult(self);
 
};
 
module.exports = SchemaResult;