Exemple #1
0
bool Int::isValid(
   DynamicObject& obj,
   ValidatorContext* context)
{
   bool rval;
   DynamicObjectType objType;

   rval = !obj.isNull();

   if(rval)
   {
      objType = obj->getType();
      if(objType == String)
      {
         objType = DynamicObject::determineType(obj->getString());
      }

      // type check
      rval =
         objType == Int32 ||
         objType == UInt32 ||
         objType == Int64 ||
         objType == UInt64;
   }

   if(!rval)
   {
      DynamicObject detail =
         context->addError("monarch.validation.ValueError", &obj);
      detail["validator"] = "monarch.validator.Int";
      detail["message"] =
         mErrorMessage ? mErrorMessage :
            "The given value type is required to be an integer.";
   }

   // absolute value of dyno value
   uint64_t val = 0;
   // flag if val is negative
   bool valneg = false;

   // get value for min/max check
   if(rval)
   {
      // get value and sign
      switch(objType)
      {
         case Int32:
         case Int64:
         {
            int64_t raw = obj->getInt64();
            valneg = (raw < 0);
            val = (uint64_t)(valneg ? -raw : raw);
            break;
         }
         case UInt32:
         case UInt64:
         {
            valneg = false;
            val = obj->getUInt64();
            break;
         }
         default:
            // never get here
            break;
      }
   }

   // min check
   if(rval)
   {
      if(mMinNegative != valneg)
      {
         // signs are different
         // val meets the minimum unless val is negative
         rval = !valneg;
      }
      else
      {
         // signs are the same
         // val meets the minimum if:
         // 1. val is positive and larger than mMin
         // 2. val is negative and smaller than mMin
         rval = (!valneg ? val >= mMin : val <= mMin);
      }

      // set exception on failure
      if(!rval)
      {
         DynamicObject detail =
            context->addError("monarch.validation.ValueError", &obj);
         detail["validator"] = "monarch.validator.Int";
         detail["message"] = mErrorMessage ? mErrorMessage :
            "The given integer value is less than the required minimum "
            "integer value.";
         detail["expectedMin"] = mMin;
      }
   }

   // max check
   if(rval)
   {
      if(mMaxNegative != valneg)
      {
         // signs are different
         // val meets the maximum unless val is positive
         rval = valneg;
      }
      else
      {
         // signs are the same
         // val meets the maximum if:
         // 1. val is positive and smaller than mMax
         // 2. val is negative and larger than mMax
         rval = (valneg ? val >= mMax : val <= mMax);
      }

      // set exception on failure
      if(!rval)
      {
         DynamicObject detail =
            context->addError("monarch.validation.ValueError", &obj);
         detail["validator"] = "monarch.validator.Int";
         detail["message"] = mErrorMessage ? mErrorMessage :
            "The given integer value is greater than the allowable maximum "
            "integer value.";
         detail["expectedMax"] = mMax;
      }
   }

   if(rval)
   {
      context->addSuccess();
   }

   return rval;
}