Source: src/main/javascript/oracle/oj/ojvalidation/LengthValidator.js

Oracle® JavaScript Extension Toolkit (JET)
1.1.2

E65298-01

/**
 * Copyright (c) 2014, Oracle and/or its affiliates.
 * All rights reserved.
 */

/**
 * Constructs a LengthValidator that ensures the value entered is within a given length. 
 * 
 * @param {Object=} options an object literal used to provide:<p>
 * @param {number=} options.min - a number that is the minimum length of the value.
 * @param {number=} options.max - a number that is the maximum length of the value.
 * @param {Object=} options.hint - an optional object literal of hints to be used. 
 * @param {string=} options.hint.max - a hint message to be used to indicate the allowed maximum. 
 * When not present, the default hint is the resource defined with the key 
 * <code class="prettyprint">oj-validator.length.hint.max</code>.<p>
 * Tokens: <br/>
 * {max} - the maximum<p>
 * Usage: <br/>
 * Enter {max} or fewer characters
 * @param {string=} options.hint.min - a hint message to be used to indicate the allowed minimum. 
 * When not present, the default hint is the resource defined with the key 
 * <code class="prettyprint">oj-validator.length.hint.min</code>.<p>
 * Tokens: <br/>
 * {min} the minimum<p>
 * Usage: <br/>
 * Enter {min} or more characters 
 * @param {string=} options.hint.inRange - a hint message to be used to indicate the allowed range. 
 * When not present, the default hint is the resource defined with the key 
 * <code class="prettyprint">oj-validator.length.hint.inRange</code>.<p>
 * Tokens: <br/>
 * {min} the minimum<p>
 * {max} - the maximum<p>
 * Usage: <br/>
 * Enter between {min} and {max} characters
 * @param {string=} options.hint.exact - a hint message to be used, to indicate the exact length. 
 * When not present, the default hint is the resource defined with the key 
 * <code class="prettyprint">oj-validator.length.hint.exact</code>.<p>
 * Tokens: <br/>
 * {length} the length<p>
 * Usage: <br/>
 * Enter {length} characters
 * @param {Object=} options.messageDetail - an optional object literal of custom error messages to 
 * be used.
 * @param {string=} options.messageDetail.tooLong - the detail error message to be used as the error 
 * message, when the length of the input value exceeds the maximum value set. When not present, the 
 * default detail message is the resource defined with the key 
 * <code class="prettyprint">oj-validator.length.messageDetail.tooLong</code>.<p>
 * Tokens:<br/>
 * {value} - value entered by the user<br/>
 * {max} - the maximum allowed value<p>
 * Usage: <br/>
 * The {value} has too many characters. Enter {max} or fewer characters, not more.
 * @param {string=} options.messageDetail.tooShort - the detail error message to be used as the error 
 * message, when the length of the input value is less the minimum value set. When not present, the 
 * default detail message is the resource defined with the key 
 * <code class="prettyprint">oj-validator.length.messageDetail.tooShort</code>.<p>
 * Tokens:<br/>
 * {value} - value entered by the user<br/>
 * {min} - the minimum allowed value<p>
 * Usage: <br/>
 * The {value} has too few characters. Enter {min} or more characters, not less.
 * @param {Object=} options.messageSummary - optional object literal of custom error summary message 
 * to be used. 
 * @param {string=} options.messageSummary.tooLong - the message to be used as the summary error 
 * message, when the length of the input value exceeds the maximum value set. When not present, the 
 * default message summary is the resource defined with the key 
 * <code class="prettyprint">oj-validator.length.messageSummary.tooLong</code>.
 * @param {string=} options.messageSummary.tooShort - the message to be used as the summary error 
 * message, when input value is less than the set minimum value. When not present, the default 
 * message summary is the resource defined with the key 
 * <code class="prettyprint">oj-validator.length.messageSummary.tooShort</code>.
 * @export
 * @constructor
 * @since 0.7
 */
oj.LengthValidator = function (options)
{
  this.Init(options);
};

// Subclass from oj.Object 
oj.Object.createSubclass(oj.LengthValidator, oj.Validator, "oj.LengthValidator");

/**
 * Initializes validator instance with the set options
 * @param {Object=} options
 * @export
 */
oj.LengthValidator.prototype.Init = function (options)
{
  oj.LengthValidator.superclass.Init.call(this);
  this._min = options["min"];
  this._max = options["max"];
  
  if (options)
  {
    this._hint = options['hint'] || {};
    this._customMessageSummary = options['messageSummary'] || {};
    this._customMessageDetail = options['messageDetail'] || {};

  }
};

oj.LengthValidator.prototype.getHint = function()
{
  var hint = null, hints = this._hint, 
      hintInRange = hints["inRange"], hintMinimum = hints["min"], 
      hintMaximum = hints["max"], hintExact = hints["exact"],
      translations = oj.Translations,
      min = this._min !== undefined ? parseInt(this._min, 10) : null, 
      max = this._max !== undefined ? parseInt(this._max, 10) : null, params;
	  
  if (min !== null && max !== null) 
  {
    if (min !== max)
    {
      params = {"min": min, "max": max};
      hint = hintInRange ? translations.applyParameters(hintInRange, params) : 
	                       translations.getTranslatedString('oj-validator.length.hint.inRange', params);
    }
    else
    {
      params = {'length': min};
      hint = hintExact ? translations.applyParameters(hintExact, params) :
                          translations.getTranslatedString('oj-validator.length.hint.exact', params);
    }
  }
  else if (min !== null)
  {
    params = {"min": min};
    hint = hintMinimum ?  translations.applyParameters(hintMinimum, params) :
	                      translations.getTranslatedString('oj-validator.length.hint.min', params);
  }
  else if (max !== null)
  {
    params = {"max": max};
    hint = hintMaximum ?  translations.applyParameters(hintMaximum, params) :
                          translations.getTranslatedString('oj-validator.length.hint.max', params);
  }

  return hint;
};

/**
 * Validates the length of vaue is greater than minimum and/or less than maximum.
 *
 * @param {string} value that is being validated
 * @returns {string} original if validation was successful
 *
 * @throws {Error} when the length is out of range.
 * @export
 */
oj.LengthValidator.prototype.validate  = function(value)
{
  var summary = "", detail = "", string = "" + value, length = string.length,
      customMessageDetail = this._customMessageDetail, 
      customMessageSummary = this._customMessageSummary,
      messageTooShort = customMessageDetail["tooShort"], 
      messageTooLong = customMessageDetail["tooLong"],
      messageSummaryTooShort = customMessageSummary["tooShort"], 
      messageSummaryTooLong = customMessageSummary["tooLong"],
      translations = oj.Translations, params,
      min = this._min !== undefined ? parseInt(this._min, 10) : null, 
      max = this._max !== undefined ? parseInt(this._max, 10) : null;
  
  // If only min is set and length is at least min, or 
  // if only max is set and length is at most max, or
  // if length is between min and max or
  // if neither min or max is set return with no error.
  if ((min === null || length >= this._min) &&
     ((max === null) || (length <= this._max)))
  {
    return string;
  }
  else
  {
    if (length < this._min) //too short
    {
      params = {"value": value, "min": min};
      summary = messageSummaryTooShort ? translations.applyParameters(messageSummaryTooShort, params) :
                  translations.getTranslatedString('oj-validator.length.messageSummary.tooShort');
      detail = messageTooShort ? translations.applyParameters(messageTooShort, params) : 
                  translations.getTranslatedString('oj-validator.length.messageDetail.tooShort', params);
    }
    else // too long
    {
      params = {"value": value, "max": max};
      summary = messageSummaryTooLong ? translations.applyParameters(messageSummaryTooLong, params) :
                  translations.getTranslatedString('oj-validator.length.messageSummary.tooLong');
      detail = messageTooLong ? translations.applyParameters(messageTooLong, params) : 
                    translations.getTranslatedString('oj-validator.length.messageDetail.tooLong', params);
    }
    
    throw new oj.ValidatorError(summary, detail);
  }
};