Esempio n. 1
0
TypeDate.prototype.validate = function(date, prefix, options) {
  options = util.mergeOptions(this._options, options);

  if (util.validateIfUndefined(date, prefix, "date", options)) return;

  if ((typeof this._validator === "function") && (this._validator(date) === false)) {
    throw new Errors.ValidationError("Validator for the field "+prefix+" returned `false`.");
  }

  var jsDate;
  if (util.isPlainObject(date) && (date["$reql_type$"] === "TIME")) {
    if (date.epoch_time === undefined) {
      util.pseudoTypeError("date", "epoch_time", prefix);
    }
    else if (date.timezone === undefined) {
      util.pseudoTypeError("date", "timezone", prefix);
    }

    jsDate = new Date(0);
    jsDate.setUTCSeconds(date.epoch_time)
  }
  else if ((typeof date === 'function') && (date._query !== undefined)) {
    // TOIMPROVE -- we currently just check if it's a term from the driver
    // We suppose for now that this is enough and we don't throw an error
  }
  else if (typeof date === 'string') {
    jsDate = new Date(date);
    if (jsDate.getTime() !== jsDate.getTime()) {
      if (options.enforce_type === "strict") {
        util.strictType(prefix, "date or a valid string");
      }
      else if (options.enforce_type !== "none") {
        util.looseType(prefix, "date or a valid string");
      }
    }
  }
  else if ((date instanceof Date) === false) { // We have a non valid date
    if (options.enforce_type === "strict") {
      util.strictType(prefix, "date");
    }
    else if ((options.enforce_type === "loose") && (date !== null)) {
      util.looseType(prefix, "date");
    }
  }
  else {
    jsDate = date;
  }

  // We check for min/max only if we could create a javascript date from the value
  if (jsDate !== undefined) {
    if ((this._min instanceof Date) && (this._min > jsDate)){
      throw new Errors.ValidationError("Value for "+prefix+" must be after "+this._min+".")
    }
    if ((this._max instanceof Date) && (this._max < jsDate)){
      throw new Errors.ValidationError("Value for "+prefix+" must be before "+this._max+".")
    }
  }
}
Esempio n. 2
0
TypeArray.prototype.validate = function(array, prefix, options) {
  var self = this;
  var localOptions = util.mergeOptions(this._options, options);

  if (util.validateIfUndefined(array, prefix, "array", localOptions)) return;

  if ((typeof self._validator === "function") && (self._validator(array) === false)) {
    throw new Errors.ValidationError("Validator for the field "+prefix+" returned `false`.");
  }

  if ((typeof array === 'function') && (array._query !== undefined)) {
    // We do not check ReQL terms
  }
  else if (Array.isArray(array) === false) {
    if (localOptions.enforce_type === "strict") {
      util.strictType(prefix, "array");
    }
    else if ((localOptions.enforce_type === "loose") && (array !== null)) {
      util.looseType(prefix, "array");
    }
  }
  else {
    if ((this._min !== -1) && (this._min > array.length)){
      throw new Errors.ValidationError("Value for "+prefix+" must have at least "+this._min+" elements.")
    }
    if ((this._max !== -1) && (this._max < array.length)){
      throw new Errors.ValidationError("Value for "+prefix+" must have at most "+this._max+" elements.")
    }
    if ((this._length !== -1) && (this._length !== array.length)){
      throw new Errors.ValidationError("Value for "+prefix+" must be an array with "+this._length+" elements.")
    }

    for(var i=0; i<array.length; i++) {
      if (array[i] === undefined) {
        throw new Errors.ValidationError("The element in the array "+prefix+" (position "+i+") cannot be `undefined`.");
      }
      if (this._schema !== undefined) {
        this._schema.validate(array[i], prefix+"["+i+"]", options);
      }
    }
  }
}
Esempio n. 3
0
TypeNumber.prototype.validate = function(number, prefix, options) {
  options = util.mergeOptions(this._options, options);

  if (util.validateIfUndefined(number, prefix, "number", options)) return;

  if ((typeof this._validator === "function") && (this._validator(number) === false)) {
    throw new Errors.ValidationError("Validator for the field "+prefix+" returned `false`.");
  }

  if(typeof number === 'string'){
    var numericString = parseFloat(number);
    if(!isNaN(numericString)){
      number = numericString;
    }
  }

  if ((typeof number === 'function') && (number._query !== undefined)) {
    // We do not check ReQL terms
  }
  else if ((typeof number !== "number") || (isFinite(number) === false)) {
    if (options.enforce_type === "strict") {
      util.strictType(prefix, "finite number");
    }
    else if ((options.enforce_type === "loose") && (number !== null)) {
      util.looseType(prefix, "finite number");
    }
  }
  else {
    if ((this._min !== undefined) && (this._min > number)){
      throw new Errors.ValidationError("Value for "+prefix+" must be greater than or equal to "+this._min+".")
    }
    if ((this._max !== undefined) && (this._max < number)){
      throw new Errors.ValidationError("Value for "+prefix+" must be less than or equal to "+this._max+".")
    }
    if ((this._integer === true) && (number % 1 !== 0)){
      throw new Errors.ValidationError("Value for "+prefix+" must be an integer.")
    }
  }
}