all files / dist/satellites/ validator.js

96.65% Statements 173/179
90.41% Branches 132/146
97.67% Functions 42/43
98.14% Lines 158/161
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825            33×   18×                                                                                                                       130×     130×       129×     129×     129×     129×     129×   129×     129×         129×     129× 23×       106× 49× 49×         129×   129×                                 129×     129×         129× 64×       129×                                                                                                                                                                                                                                                                                                                                                                               16×                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 10×                                                                                                                      
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
 
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
 
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; Eif ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { Eif (protoProps) defineProperties(Constructor.prototype, protoProps); Iif (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
 
function _classCallCheck(instance, Constructor) { Iif (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
 
/**
 * This class allow developers testes values against a
 * set of validators.
 *
 * This is used by the ActionProcessor to validator the
 * action input params.
 *
 * You can use this manually like this:
 *
 * <code>
 *   api.validator.validate(validatorString, params, keyToValidate)
 * </code>
 */
 
var Validator = function () {
 
  /**
   * Create a new Validator instance.
   *
   * @param api API reference object.
   */
 
 
  /**
   * Request parameters.
   *
   * @type {{}}
   */
 
  function Validator(api) {
    _classCallCheck(this, Validator);
 
    this.api = null;
    this.params = {};
    this.api = api;
  }
 
  /**
   * Validate a set of parameters using a validator string.
   *
   *
   * @param validatorString   Validator string
   * @param params            Set of params.
   * @param key               Param under validation.
   * @returns {*}             Validator response. True if it's okay
   */
 
 
  /**
   * Array with the implicit validators.
   *
   * @type {string[]}
   */
 
 
  /**
   * API reference object.
   *
   * @type {null}
   */
 
 
  _createClass(Validator, [{
    key: 'validate',
    value: function validate(validatorString, params, key) {
      var self = this;
 
      // the key must be present, otherwise return an error string
      if (key === undefined || key === null || key === '') {
        return 'a key must be specified';
      }
 
      // save parameters request parameters
      self.params = params;
 
      // gets the value of the parameter to be validated
      var value = params[key];
 
      // the validator string can have many validators separated by '|', we need to split them
      var validators = validatorString.split('|');
 
      // save the validator response
      var validatorResponse = void 0;
 
      // iterate all validators and execute them
      for (var index in validators) {
        // split by ':' to get the validator arguments
        var validatorParts = validators[index].split(':');
 
        // if the property has undefined only implicit validators can be applied
        Iif (value === undefined && Validator.implicitValidators.indexOf(validatorParts[0]) === -1) {
          continue;
        }
 
        // call the validator
        validatorResponse = self.execValidator(validatorParts[0], validatorParts[1], value, key);
 
        // if the response is a string that means we found a invalid validator
        if (typeof validatorResponse === 'string') {
          break;
        }
 
        // if the validator fails return a fail message
        if (validatorResponse === false) {
          validatorResponse = 'don\'t match with the validator';
          break;
        }
      }
 
      // clean the parameters property
      self.params = {};
 
      return validatorResponse;
    }
 
    /**
     * Execute the request validator and apply it to the passed value.
     *
     * @param validator   Validator name.
     * @param args        Validator arguments.
     * @param value       Value to be validated.
     * @param key         Parameter key name.
     * @returns {*}
     */
 
  }, {
    key: 'execValidator',
    value: function execValidator(validator, args, value, key) {
      // call the validator function
      var funcName = 'validator_' + validator;
 
      // check if the validator exists
      Iif (this[funcName] === undefined) {
        return 'invalid validator';
      }
 
      // split the arguments by ',' if exists
      if (args !== undefined) {
        args = args.split(',');
      }
 
      // call the validator function
      return this[funcName](value, args, key);
    }
 
    // ------------------------------------------------------------------------------------------------------ [Validators]
 
    /**
     * Check if the value is a string only with alpha characters.
     *
     * @param value
     * @returns {boolean}
     */
 
  }, {
    key: 'validator_alpha',
    value: function validator_alpha(value) {
      return (/^[a-zA-Z]*$/.test(value)
      );
    }
 
    /**
     * Check if the value is a number.
     *
     * @param value
     * @returns {boolean}
     */
 
  }, {
    key: 'validator_alpha_num',
    value: function validator_alpha_num(value) {
      return (/^[a-zA-Z0-9]*$/.test(value)
      );
    }
 
    /**
     * Check if the value is a string only with alpha or (_, -) characters.
     *
     * @param value
     * @returns {boolean}
     */
 
  }, {
    key: 'validator_alpha_dash',
    value: function validator_alpha_dash(value) {
      return (/^[a-zA-Z0-9-_]*$/.test(value)
      );
    }
 
    /**
     * Check if the value is an array.
     *
     * @param value
     * @returns {boolean}
     */
 
  }, {
    key: 'validator_array',
    value: function validator_array(value) {
      return Array.isArray(value);
    }
 
    /**
     * Check if the value is before than the specified date.
     *
     * @param value
     * @param args
     * @returns {*}
     */
 
  }, {
    key: 'validator_before',
    value: function validator_before(value, args) {
      // check if the developer specify an argument
      if (args === undefined) {
        return 'you need to specify an argument';
      }
 
      // check if the argument is a date
      if (isNaN(Date.parse(args))) {
        return 'the specified argument is not a valid date';
      }
 
      // check if the value if a date
      if (isNaN(Date.parse(value))) {
        return 'the specified value is not a valid date';
      }
 
      // check if the specified date is less than the required date
      return Date.parse(value) < Date.parse(args);
    }
 
    /**
     * Check if the value is between the two intervals.
     *
     * @param value
     * @param args
     * @returns {*}
     */
 
  }, {
    key: 'validator_between',
    value: function validator_between(value, args) {
      // check if the developer specify the valid number of arguments
      if (!Array.isArray(args) || args.length !== 2) {
        return 'invalid validator arguments';
      }
 
      // check if the value is valid
      if (typeof value === 'string') {
        return value.length >= args[0] && value.length <= args[1];
      } else if (typeof value === 'number') {
        return value >= args[0] && value <= args[1];
      } else {
        return 'invalid data type';
      }
    }
 
    /**
     * Check if the value is a boolean.
     *
     * @param value
     * @returns {boolean}
     */
 
  }, {
    key: 'validator_boolean',
    value: function validator_boolean(value) {
      return typeof value === 'boolean';
    }
 
    /**
     * Check if exists a confirmation fields to the testing key with the same name.
     *
     * @param value
     * @param args
     * @param key
     * @returns {*}
     */
 
  }, {
    key: 'validator_confirmed',
    value: function validator_confirmed(value, args, key) {
      // build the confirmation field name
      var confirmationFieldName = key + '_confirmation';
 
      // check if the confirmation field are not present
      if (this.params[confirmationFieldName] === undefined) {
        return 'the confirmation field are not present';
      }
 
      // check if the values of two fields match
      if (this.params[confirmationFieldName] !== value) {
        return 'the values not match';
      }
 
      return true;
    }
 
    /**
     * Check if the param is a date.
     *
     * @param value
     * @returns {*}
     */
 
  }, {
    key: 'validator_date',
    value: function validator_date(value) {
      if (isNaN(Date.parse(value))) {
        return 'the specified value is not a valid date';
      }
      return true;
    }
 
    /**
     * Check if the value is different of the other field.
     *
     * @param value
     * @param args
     * @returns {*}
     */
 
  }, {
    key: 'validator_different',
    value: function validator_different(value, args) {
      // check if the validator has the correct parameter number
      if (args === undefined) {
        return 'the validator need one argument';
      }
 
      return value !== this.params[args];
    }
 
    /**
     * Check if the value is an email.
     *
     * @param value
     * @returns {boolean}
     */
 
  }, {
    key: 'validator_email',
    value: function validator_email(value) {
      return (/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(value)
      );
    }
 
    /**
     * Check if the value is filled.
     *
     * @param value
     * @returns {boolean}
     */
 
  }, {
    key: 'validator_filled',
    value: function validator_filled(value) {
      return value !== undefined && value !== null && value !== '';
    }
 
    /**
     * Check if the value are included in the array.
     *
     * @param value
     * @param args
     * @returns {*}
     */
 
  }, {
    key: 'validator_in',
    value: function validator_in(value, args) {
      // check if the validator have a name
      if (args === undefined && !Array.isArray(args)) {
        return 'validator needs an array';
      }
 
      // check if the array contains the value
      return args.indexOf(String(value)) > -1;
    }
 
    /**
     * Check if the value are not included in the array.
     * @param value
     * @param args
     * @returns {*}
     */
 
  }, {
    key: 'validator_not_in',
    value: function validator_not_in(value, args) {
      // check if the validator have a name
      if (args === undefined && !Array.isArray(args)) {
        return 'validator needs an array';
      }
 
      // check if the array not contains the value
      return args.indexOf(String(value)) === -1;
    }
 
    /**
     * Check if the value is an integer.
     *
     * @param value
     * @returns {boolean}
     */
 
  }, {
    key: 'validator_integer',
    value: function validator_integer(value) {
      // try parse to pin
      var parsedValue = Number.parseInt(value);
 
      // check if is a number
      return Number.isInteger(parsedValue);
    }
 
    /**
     * Check if the value is an IP.
     *
     * @param value
     * @returns {boolean}
     */
 
  }, {
    key: 'validator_ip',
    value: function validator_ip(value) {
      return (/^(?!0)(?!.*\.$)((1?\d?\d|25[0-5]|2[0-4]\d)(\.|$)){4}$/.test(value)
      );
    }
 
    /**
     * Check if the field is a valid JSON.
     *
     * @param value
     * @returns {boolean}
     */
 
  }, {
    key: 'validator_json',
    value: function validator_json(value) {
      try {
        var o = JSON.parse(value);
 
        if (o && (typeof o === 'undefined' ? 'undefined' : _typeof(o)) === "object" && o !== null) {
          return true;
        }
      } catch (e) {}
 
      return false;
    }
 
    /**
     * Check if the parameter match with a max value.
     *
     * @param value
     * @param args
     * @returns {*}
     */
 
  }, {
    key: 'validator_max',
    value: function validator_max(value, args) {
      // the validator needs one argument
      if (args === undefined) {
        return 'validator need at least one argument';
      }
 
      if (typeof value === 'string' || value instanceof Array) {
        return value.length <= args[0];
      } else if (typeof value === 'number') {
        return value <= args[0];
      } else {
        return 'invalid type';
      }
    }
 
    /**
     * Check if the parameter match with a min value.
     *
     * @param value
     * @param args
     * @returns {*}
     */
 
  }, {
    key: 'validator_min',
    value: function validator_min(value, args) {
      // the validator needs one argument
      if (args === undefined) {
        return 'validator need at least one argument';
      }
 
      if (typeof value === 'string' || value instanceof Array) {
        return value.length >= args[0];
      } else if (typeof value === 'number') {
        return value >= args[0];
      } else {
        return 'invalid type';
      }
    }
 
    /**
     * Check if the value exists.
     *
     * @param value
     * @returns {boolean}
     */
 
  }, {
    key: 'validator_required',
    value: function validator_required(value) {
      return value !== undefined;
    }
 
    /**
     * Check if the value is numeric.
     *
     * @param value
     * @returns {boolean}
     */
 
  }, {
    key: 'validator_numeric',
    value: function validator_numeric(value) {
      return typeof value === 'number';
    }
 
    /**
     * Check if the field is required taking into account
     * the parameters.
     *
     * @param value
     * @param args
     * @returns {*}
     */
 
  }, {
    key: 'validator_required_if',
    value: function validator_required_if(value, args) {
      // check if we have the needs arguments
      if (!(args instanceof Array) || args.length < 2) {
        return 'validator need two arguments';
      }
 
      // get the parameter to test
      var parameterToCheck = args.shift();
 
      // if the args[0] param value is present in the values array the value is required
      if (args.indexOf(String(this.params[parameterToCheck])) > -1) {
        return this.validator_filled(value);
      }
 
      return true;
    }
 
    /**
     * The field under validation must be present unless
     * the args[0] is equal to any value.
     *
     * @param value
     * @param args
     * @returns {*}
     */
 
  }, {
    key: 'validator_required_unless',
    value: function validator_required_unless(value, args) {
      // check if we have the needs arguments
      if (!(args instanceof Array) || args.length < 2) {
        return 'validator need two arguments';
      }
 
      // get the parameter to test
      var parameterToCheck = args.shift();
 
      // if the parameter not have a valid value the current parameter is required
      if (args.indexOf(String(this.params[parameterToCheck])) === -1) {
        return this.validator_filled(value);
      }
 
      return true;
    }
 
    /**
     * The field under validation must be present only if
     * any of the other specified fields are present.
     *
     * @param value
     * @param args
     * @returns {*}
     */
 
  }, {
    key: 'validator_required_with',
    value: function validator_required_with(value, args) {
      // check if we have the needs arguments
      if (!(args instanceof Array) || args.length < 2) {
        return 'validator need two arguments';
      }
 
      // check if one of the parameters are present
      for (var index in args) {
        // get parameter name
        var paramName = args[index];
 
        // check if the value is filled
        if (this.params[paramName] !== undefined) {
          return this.validator_filled(value);
        }
      }
 
      return true;
    }
 
    /**
     * The field under validation must be present only if
     * all of the other specified fields are present.
     *
     * @param value
     * @param args
     * @returns {*}
     */
 
  }, {
    key: 'validator_required_with_all',
    value: function validator_required_with_all(value, args) {
      // check if we have the needs arguments
      if (!(args instanceof Array) || args.length < 2) {
        return 'validator need two arguments';
      }
 
      // check if all the parameters are present
      for (var index in args) {
        // get parameter name
        var paramName = args[index];
 
        if (this.params[paramName] === undefined) {
          return true;
        }
      }
 
      // if all the fields are present the fields under validation is required
      return this.validator_filled(value);
    }
 
    /**
     * The field under validation must be present only when
     * any of the other specified fields are not present.
     *
     * @param value
     * @param args
     * @returns {*}
     */
 
  }, {
    key: 'validator_required_without',
    value: function validator_required_without(value, args) {
      // check if we have the needs arguments
      if (!(args instanceof Array) || args.length < 2) {
        return 'validator need two arguments';
      }
 
      // if one of the fields are not present the field under validation is required
      for (var index in args) {
        // get parameter name
        var paramName = args[index];
 
        if (this.params[paramName] === undefined) {
          return this.validator_filled(value);
        }
      }
 
      return true;
    }
 
    /**
     * The field under validation must be present only when
     * all of the other specified fields are not present.
     *
     * @param value
     * @param args
     * @returns {*}
     */
 
  }, {
    key: 'validator_required_without_all',
    value: function validator_required_without_all(value, args) {
      // check if we have the needs arguments
      if (!(args instanceof Array) || args.length < 2) {
        return 'validator need two arguments';
      }
 
      for (var index in args) {
        // get parameter name
        var paramName = args[index];
 
        // if one of the fields are not present we can stop right here
        if (this.params[paramName] !== undefined) {
          return true;
        }
      }
 
      return this.validator_filled(value);
    }
 
    /**
     * The given field must match the field under validation.
     *
     * @param value
     * @param args
     * @returns {*}
     */
 
  }, {
    key: 'validator_same',
    value: function validator_same(value, args) {
      // check if we have the needs arguments
      if (!(args instanceof Array) || args.length < 1) {
        return 'validator need one argument';
      }
 
      return this.params[args[0]] === value;
    }
 
    /**
     * The field under validation must have a size matching the given value.
     *
     * @param value
     * @param args
     */
 
  }, {
    key: 'validator_size',
    value: function validator_size(value, args) {
      // check if we have the needs arguments
      if (!(args instanceof Array) || isNaN(args[0])) {
        return 'validator need one numeric argument';
      }
 
      if (typeof value === 'string' || value instanceof Array) {
        return value.length == args[0];
      } else Eif (typeof value === 'number') {
        return value == args[0];
      } else {
        return 'invalid type';
      }
    }
 
    /**
     * The field under validation must be a valid URL.
     *
     * @param value
     * @returns {boolean}
     */
 
  }, {
    key: 'validator_url',
    value: function validator_url(value) {
      return (/^(http|ftp|https):\/\/[\w-]+(\.[\w-]*)+([\w.,@?^=%&amp;:/~+#-]*[\w@?^=%&amp;/~+#-])?$/.test(value)
      );
    }
  }]);
 
  return Validator;
}();
 
/**
 * Validator satellite.
 */
 
 
Validator.implicitValidators = ['required_if', 'required', 'required_unless', 'filled', 'required_with', 'required_with_all', 'required_without', 'required_without_all'];
 
var _class = function () {
  function _class() {
    _classCallCheck(this, _class);
 
    this.loadPriority = 400;
  }
 
  /**
   * Satellite priority.
   *
   * @type {number}
   */
 
 
  _createClass(_class, [{
    key: 'load',
 
 
    /**
     * Satellite load function.
     *
     * @param api   API reference object.
     * @param next  Callback function.
     */
    value: function load(api, next) {
      // load validator logic into the API object
      api.validator = new Validator(api);
 
      // finish the load process
      next();
    }
  }]);
 
  return _class;
}();
 
exports.default = _class;
//# sourceMappingURL=validator.js.map