MCPcopy Create free account
hub / github.com/cryptii/cryptii / validateValue

Method validateValue

src/Field/Text.js:183–248  ·  view source on GitHub ↗

* Validates given raw value. * @param {mixed} rawValue Value to be validated * @return {boolean|object} True if valid, message object or false if invalid

(rawValue)

Source from the content-addressed store, hash-verified

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:

Callers 1

applyModelMethod · 0.45

Calls 9

filterValueMethod · 0.95
isUniqueMethod · 0.80
getCodePointsMethod · 0.80
getCodePointAtMethod · 0.80
indexOfMethod · 0.80
getCharAtMethod · 0.80
uniqueMethod · 0.80
joinMethod · 0.80
getLengthMethod · 0.45

Tested by

no test coverage detected