# all files / keystone/fields/types/textarray/ TextArrayType.js

96.34% Statements 79/82
88.24% Branches 75/85
100% Functions 9/9
96.34% Lines 79/82

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

    1×
    3×
    3×
    3×
    3×

    1×

    1×
    3×

    1×
    8×

    8×
    2×

    2×

    6×
    6×
    1×
    5×
    1×
    4×
    1×

    6×

    6×

    6×

    1×
    14×
    14×

    14×

    8×
    4×

    8×
    9×

    9×
    5×
    5×

    14×

    1×
    8×
    8×

    8×
    2×
    1×

    8×
    1×

    7×
    4×
    4×
    8×

    8×
    2×
    2×

    4×
    2×

    8×

    1×
    7×
    7×
    4×
    1×

    3×
    3×

    3×

    1×
    7×
    7×
    7×
    1×

    7×
    1×

    7×
    16×
    13×

    16×

    16×

    7×

    7×

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

    /**
     * TextArray FieldType Constructor
     * @extends Field
     * @api public
     */
    function textarray (list, path, options) {
    	this._nativeType = [String];
    	this._underscoreMethods = ['format'];
    	this.separator = options.separator || ' | ';
    	textarray.super_.call(this, list, path, options);
    }
    util.inherits(textarray, FieldType);

    /**
     * Formats the field value
     */
    textarray.prototype.format = function (item, separator) {
    	return item.get(this.path).join(separator || this.separator);
    };

    /**
     * Add filters to a query
     */
    textarray.prototype.addFilterToQuery = function (filter, query) {
    	query = query || {};
    	// Filter empty/non-empty arrays
    	if (filter.mode === 'exactly' && !filter.value) {
    		query[this.path] = {
    			$elemMatch: filter.inverted ? {
    				$nin: ['', null],
    			} : {
    				$in: ['', null],
    			},
    		};
    		return query;
    	}
    	var value = utils.escapeRegExp(filter.value);
    	if (filter.mode === 'beginsWith') {
    		value = '^' + value;
    	} else if (filter.mode === 'endsWith') {
    		value = value + '$';
    	} else if (filter.mode === 'exactly') {
    		value = '^' + value + '$';
    	}
    	value = new RegExp(value, filter.caseSensitive ? '' : 'i');
    	// Filter if values do not exist in array
    	query[this.path] = filter.inverted ? {
    		$not: value,
    	} : {
    		$elemMatch: {
    			$regex: value,
    		},
    	};
    	return query;
    };

    /**
     * Asynchronously confirms that the provided value is valid
     */
    textarray.prototype.validateInput = function (data, callback) {
    	var value = this.getValueFromData(data);
    	var result = true;
    	// If the value is null, undefined or an empty string
    	// bail early since updateItem sanitizes that just fine
    	if (value !== undefined && value !== null && value !== '') {
    		// If the value is not an array, convert it to one
    		// e.g. if textarr = 'somestring' (which is valid)
    		if (!Array.isArray(value)) {
    			value = [value];
    		}
    		for (var i = 0; i < value.length; i++) {
    			var thisValue = value[i];
    			// If the current value is not a string and is neither false nor
    			// undefined, fail the validation
    			if (typeof thisValue !== 'string') {
    				result = false;
    				break;
    			}
    		}
    	}
    	utils.defer(callback, result);
    };

    /**
     * Asynchronously confirms that the a value is present
     */
    textarray.prototype.validateRequiredInput = function (item, data, callback) {
    	var value = this.getValueFromData(data);
    	var result = false;
    	// If the value is undefined and we have something stored already, validate
    	if (value === undefined) {
    		if (item.get(this.path) && item.get(this.path).length) {
    			result = true;
    		}
    	}
    	// If it's a string that's not empty, validate
    	if (typeof value === 'string') {
    		Iif (value !== '') {
    			result = true;
    		}
    	// If it's an array of only strings and/or strinigfy-able data, validate
    	} else if (Array.isArray(value)) {
    		var invalidContent = false;
    		for (var i = 0; i < value.length; i++) {
    			var thisValue = value[i];
    			// If even a single item is not a string or an empty string, invalidate
    			if (typeof thisValue !== 'string' || thisValue === '') {
    				invalidContent = true;
    				break;
    			}
    		}
    		if (invalidContent === false) {
    			result = true;
    		}
    	}
    	utils.defer(callback, result);
    };

    /**
     * Validates that a value for this field has been provided in a data object
     *
     * Deprecated
     */
    textarray.prototype.inputIsValid = function (data, required, item) {
    	var value = this.getValueFromData(data);
    	if (required) {
    		if (value === undefined && item && item.get(this.path) && item.get(this.path).length) {
    			return true;
    		}
    		Eif (value === undefined || !Array.isArray(value) || (typeof value !== 'string') || (typeof value !== 'number')) {
    			return false;
    		}
    		if (Array.isArray(value) && !value.length) {
    			return false;
    		}
    	}
    	return (value === undefined || Array.isArray(value) || (typeof value === 'string') || (typeof value === 'number'));
    };

    /**
     * Updates the value for this field in the item from a data object
     */
    textarray.prototype.updateItem = function (item, data, callback) {
    	var value = this.getValueFromData(data);
    	Eif (typeof value !== 'undefined') {
    		if (value === null || value === '') {
    			value = [];
    		}
    		if (!Array.isArray(value)) {
    			value = [value];
    		}
    		value = value.map(function (str) {
    			if (str && str.toString) {
    				str = str.toString();
    			}
    			return str;
    		}).filter(function (str) {
    			return (typeof str === 'string' && str);
    		});
    		item.set(this.path, value);
    	}
    	process.nextTick(callback);
    };

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