* Validates given raw value. * @param {mixed} rawValue Value to be validated * @return {boolean|object} True if valid, message object or false if invalid
(rawValue)
| 181 | * @return {boolean|object} True if valid, message object or false if invalid |
| 182 | */ |
| 183 | validateValue (rawValue) { |
| 184 | if (typeof rawValue.toString !== 'function') { |
| 185 | return { |
| 186 | key: 'textInvalidString', |
| 187 | message: 'The value can\'t be casted to a string' |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | const value = this.filterValue(rawValue) |
| 192 | |
| 193 | // Validate min text length |
| 194 | if (this._minLength !== null && value.getLength() < this._minLength) { |
| 195 | return { |
| 196 | key: 'textLengthTooShort', |
| 197 | message: |
| 198 | `The value must be at least ${this._minLength} ` + |
| 199 | `${this._minLength === 1 ? 'character' : 'characters'} long` |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | // Validate max text length |
| 204 | if (this._maxLength !== null && value.getLength() > this._maxLength) { |
| 205 | return { |
| 206 | key: 'textLengthTooLong', |
| 207 | message: |
| 208 | `The value must not exceed ${this._maxLength} ` + |
| 209 | `${this._maxLength === 1 ? 'character' : 'characters'} in length` |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | // Validate character uniqueness |
| 214 | if (this._uniqueChars && !ArrayUtil.isUnique(value.getCodePoints())) { |
| 215 | return { |
| 216 | key: 'textCharactersNotUnique', |
| 217 | message: 'The value must not contain duplicate characters' |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | // Validate character whitelist and blacklist |
| 222 | if (this._whitelistChars !== null || this._blacklistChars !== null) { |
| 223 | const whitelist = this._whitelistChars |
| 224 | const blacklist = this._blacklistChars |
| 225 | let invalidChars = [] |
| 226 | let c |
| 227 | |
| 228 | for (let i = 0; i < value.getLength(); i++) { |
| 229 | c = value.getCodePointAt(i) |
| 230 | if ((whitelist !== null && whitelist.indexOf(c) === -1) || |
| 231 | (blacklist !== null && blacklist.indexOf(c) !== -1)) { |
| 232 | invalidChars.push(value.getCharAt(i)) |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | if (invalidChars.length > 0) { |
| 237 | invalidChars = ArrayUtil.unique(invalidChars) |
| 238 | return { |
| 239 | key: 'textForbiddenCharacter', |
| 240 | message: |
no test coverage detected