Source: string.js

  1. 'use strict';
  2. var assert = require('assert');
  3. var util = require('util');
  4. var ut = require('utjs');
  5. var Type = require('./Type');
  6. /**
  7. * Generates a schema object that matches a string data type.
  8. *
  9. * @constructor
  10. * @extends Type
  11. */
  12. function StringType() {
  13. StringType.super_.call(this);
  14. }
  15. util.inherits(StringType, Type);
  16. /**
  17. * Specifies the minimum number string characters.
  18. * @param {Number} limit The minimum number of string characters required.
  19. * @return {StringType} The class reference so multiple calls can be chained.
  20. */
  21. StringType.prototype.min = function (limit) {
  22. assert(ut.isInteger(limit) && limit >= 0, 'limit must be a positive integer');
  23. this._rules.min = limit;
  24. return this;
  25. };
  26. /**
  27. * Specifies the maximum number string characters.
  28. * @param {Number} limit The maximum number of string characters required.
  29. * @return {StringType} The class reference so multiple calls can be chained.
  30. */
  31. StringType.prototype.max = function (limit) {
  32. assert(ut.isInteger(limit) && limit >= 0, 'limit must be a positive integer');
  33. this._rules.max = limit;
  34. return this;
  35. };
  36. /**
  37. * Specifies the exact string length required.
  38. * @param {Number} limit the required string length.
  39. * @return {StringType} The class reference so multiple calls can be chained.
  40. */
  41. StringType.prototype.length = function (limit) {
  42. assert(ut.isInteger(limit) && limit >= 0, 'limit must be a positive integer');
  43. this._rules.length = limit;
  44. return this;
  45. };
  46. /**
  47. * Defines a regular expression rule.
  48. * @param {RegExp} pattern a regular expression object the string value must match against.
  49. * @return {StringType} The class reference so multiple calls can be chained.
  50. */
  51. StringType.prototype.regex = function (pattern) {
  52. assert(ut.isRegExp(pattern), 'pattern must be a RegExp instance');
  53. this._rules.regex = pattern;
  54. return this;
  55. };
  56. /**
  57. * Requires the string value to be in valid ISO 8601 date format.
  58. * @return {StringType} The class reference so multiple calls can be chained.
  59. */
  60. StringType.prototype.isoDate = function () {
  61. this._rules.isoDate = true;
  62. return this;
  63. };
  64. StringType.prototype._validate = function (value) {
  65. var rules = this._rules;
  66. if (typeof value !== 'string') {
  67. return !rules.required && value === undefined;
  68. }
  69. var min = rules.min;
  70. var max = rules.max;
  71. var length = rules.length;
  72. var regex = rules.regex;
  73. var isoDate = rules.isoDate;
  74. var valueLength = value.length;
  75. if (min !== undefined && valueLength < min ||
  76. max !== undefined && valueLength > max) {
  77. return false;
  78. }
  79. if (length !== undefined && valueLength !== length) {
  80. return false;
  81. }
  82. if (regex !== undefined && !regex.test(value)) {
  83. return false;
  84. }
  85. if (isoDate && !ut.isDateString(value)) {
  86. return false;
  87. }
  88. return rules.forbidden ? false : true;
  89. };
  90. /**
  91. * Creates and returns a StringType object.
  92. * @return {StringType} The StringType object.
  93. * @global
  94. * @alias string
  95. */
  96. module.exports = function string() {
  97. return new StringType();
  98. };