# all files / keystone/fields/types/boolean/ BooleanType.js

100% Statements 39/39
94.12% Branches 32/34
100% Functions 6/6
100% Lines 39/39

    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
    841×
    1×
    1×

    1×
    10×
    10×
    10×
    10×
    10×

    1×

    1×
    13×
    13×
    13×

    2×

    13×

    1×
    9×
    9×
    9×

    1×
    3×
    3×
    2×

    1×

    3×

    1×
    12×
    6×

    6×

    1×
    13×
    13×
    6×
    6×

    7×
    7×

    13×

    1×
     var FieldType = require('../Type');
    var utils = require('keystone-utils');
    var util = require('util');

    /**
     * Boolean FieldType Constructor
     * @extends Field
     * @api public
     */
    function boolean (list, path, options) {
    	this._nativeType = Boolean;
    	this._properties = ['indent'];
    	this._fixedSize = 'full';
    	this.indent = (options.indent) ? true : false;
    	boolean.super_.call(this, list, path, options);
    }
    util.inherits(boolean, FieldType);

    boolean.prototype.validateInput = function (data, callback) {
    	var value = this.getValueFromData(data);
    	var result = true;
    	if (value !== undefined
    		&& value !== null
    		&& typeof value !== 'string'
    		&& typeof value !== 'number'
    		&& typeof value !== 'boolean') {
    		result = false;
    	}
    	utils.defer(callback, result);
    };

    boolean.prototype.validateRequiredInput = function (item, data, callback) {
    	var value = this.getValueFromData(data);
    	var result = (value && value !== 'false') || item.get(this.path) ? true : false;
    	utils.defer(callback, result);
    };

    /**
     * Add filters to a query
     */
    boolean.prototype.addFilterToQuery = function (filter, query) {
    	query = query || {};
    	if (!filter.value || filter.value === 'false') {
    		query[this.path] = { $ne: true };
    	} else {
    		query[this.path] = true;
    	}
    	return query;
    };

    /**
     * Validates that a truthy value for this field has been provided in a data object.
     * Useful for checkboxes that are required to be true (e.g. agreed to terms and cond's)
     *
     * Deprecated
     */
    boolean.prototype.inputIsValid = function (data, required) {
    	if (required) {
    		return (data[this.path] === true || data[this.path] === 'true') ? true : false;
    	} else {
    		return true;
    	}
    };

    /**
     * Updates the value for this field in the item from a data object.
     * Only updates the value if it has changed.
     * Treats a falsy value or the string "false" as false, everything else as true.
     */
    boolean.prototype.updateItem = function (item, data, callback) {
    	var value = this.getValueFromData(data);
    	if (!value || value === 'false') {
    		Eif (item.get(this.path) !== false) {
    			item.set(this.path, false);
    		}
    	} else Eif (!item.get(this.path)) {
    		item.set(this.path, true);
    	}
    	process.nextTick(callback);
    };

    /* Export Field Type */
    module.exports = boolean;