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

Statements: 100% (21 / 21)      Branches: 100% (4 / 4)      Functions: 100% (7 / 7)      Lines: 100% (21 / 21)      Ignored: none     

All files » src/Common/Schema/ » SchemaEvaluator.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      1                                   1                   65                 65                 65                           5 5                             6 6                                 43   43 12     31                         21                             3           11 8     3         3           65         1  
"use strict";
 
 
var _                       = require("lodash"),
    Type                    = require("../Type"),
    Safe                    = require("../Safe"),
    ExceptionList           = require("../Exception/ExceptionList"),
    Types                   = require("./Types"),
    SchemaDefinition        = require("./SchemaDefinition");
 
/**
 *
 * @class
 * The schema evaluator facade. This will serve as interface to the Schema
 * execution.
 *
 * @param {*}       schema
 * @param {Object}  validationFns
 * @param {Object}  cOptions        The SchemaDefinition compilation options
 *
 */
var SchemaEvaluator = function(schema, validationFns, cOptions){
 
    /* jshint -W064 */
    /**
     *
     * The compiled schema
     *
     * @type {SchemaDefinition}
     * 
     */
    schema = SchemaDefinition(schema, cOptions);
 
    /**
     *
     * The custom validation functions
     *
     * @type {Object}
     *
     */
    validationFns = Safe.object(validationFns);
 
    /**
     *
     * SchemaEvaluator external API
     *
     * @type {Object}
     *
     */
    var self = {
 
        /**
         *
         * Evaluate and get the errors
         *
         * @param  {*}      value
         * @param  {Object} validationFns
         *
         * @return {[Error]}
         *
         */
        errors: function(value){
 
            var result = schema.execute(value, validationFns);
            return result.getErrors();
 
        },
 
        /**
         *
         * Tests if the schema is valid against the given value.
         *
         * @param  {*}  value
         *
         * @return {Boolean}
         *
         */
        isValid: function(value){
 
            var result = schema.execute(value, validationFns);
            return result.isValid();
 
        },
 
        /**
         *
         * Gets the value after applying the schema.
         *
         * @throws {ExceptionList}
         *
         * @param  {*} value
         *
         * @return {*}
         *
         */
        value: function(value){
 
            var result = schema.execute(value, validationFns);
 
            if(!result.isValid()){
                throw result.getErrors();
            }
 
            return result.getValue();
 
        },
 
        /**
         *
         * Serialize the current Schema
         *
         * @return {Object}
         *
         */
        serialize: function(){
 
            return schema.serialize();
 
        },
 
        /**
         *
         * Deserialize the value. Returns a new instance of SchemaEvaluator.
         *
         * @return {SchemaEvaluator}
         *
         */
        deserialize: function(value){
 
            /// use a prepare option that builds the SchemaDefinition to 
            /// avoid fallback on the Default object builder
            var cOptions = {
                prepare: function(val){
                    
                    /// make sure that that the return value is a SchemaDefinition
                    /// NOTE: don't compile the inner structures because the SchemaEvaluator will 
                    /// do it
                    if(!Type.instanceOf(val, Types.SchemaDefinition)){
                        return SchemaDefinition(val, { compile: false });    
                    }
 
                    return val;
                    
                }
            };
 
            return new SchemaEvaluator(value, validationFns, cOptions);
 
        }
 
    };
 
    return new Types.SchemaEvaluator(self);
 
};
 
 
module.exports = SchemaEvaluator;