File size: 3,601 Bytes
2fcec7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// GPLv3 or later
// Copyright (c) 2015 Genome Research Limited

var isJSType   = require('./isJSType'),
    parseEMail = require('email-addresses');

// Convert subtype specification string into object
module.exports = (function() {
  // Complex patterns
  var is = {
    regExp: function(type) {
      var pattern = /^\/(.*)$/;
      return (type.match(pattern) || [, false])[1];
    },

    mediaType: function(type) {
      // per RFC6838
      var pattern = /^[a-z0-9][a-z0-9!#$&^_.+-]{0,126}\/[a-z0-9][a-z0-9!#$&^_.+-]{0,126}$/i;
      return pattern.test(type) && type;
    }
  };

  // Standard patterns
  ['datetime', 'iri', 'email'].forEach(function(t) {
    is[t] = function(type) { return type == t && type; };
  });

  // Subtype classifier
  var classify = function(type) {
    return type && Object.keys(is).reduce(function(a, t) {
      return !a && is[t](type) !== false ? t : a;
    }, undefined);
  };

  // Validation functions
  var validate = {
    // Validate by regular expression
    regExp: function(pattern) {
      var re = new RegExp(pattern);
      return function(input) { return re.test(input); }
    },

    // Validate base64 encoded data
    mediaType: function() {
      var re = /^(?:[a-z0-9+/]{4})*(?:[a-z0-9+/]{2}==|[a-z0-9+/]{3}=)?$/i;
      return function(input) { return re.test(input); }
    },

    // Validate ISO 8601 date-time per RFC3339
    datetime: function() {
      var fullDate = /^\d{4}-\d{2}-\d{2}$/,
          fullTime = /^(\d{2}):(\d{2}):(\d{2})(?:\.\d+)?z[+-](\d{2}):(\d{2})$/i;

      var between = function(x, a, b) {
        return x >= a && x <= b;
      };

      var checkDate = function(input) {
        // Check the format by regexp, but use JS to parse...
        // That said, Date.parse is really bloody stupid: It will parse
        // every month as having 31 days, then just add the difference
        // to the next month! For example, 2000-02-31 = 2nd March 2000
        return fullDate.test(input)
            && Date.parse(input) ? true : false;
      };

      var checkDateTime = function(input) {
        // Check the time by regexp
        var parts = input.split(/t/i),
            match = (parts[1] || '').match(fullTime);

        // $1 = hours
        // $2 = minutes
        // $3 = seconds
        // $4 = hour offset
        // $5 = minute offset

        return checkDate(parts[0])
            && (match ? true : false)
            && between(parseInt(match[1], 10), 0, 23)
            && between(parseInt(match[2], 10), 0, 59)
            && between(parseInt(match[3], 10), 0, 59)
            && between(parseInt(match[4], 10), 0, 23)
            && between(parseInt(match[5], 10), 0, 59);
      };

      return function(input) {
        return checkDate(input) || checkDateTime(input);
      }
    },

    // Vacuously validate IRIs
    // i.e., Any string could be an IRI, but we just mark it as such
    // so that is has semantics that differ from free text
    iri: function() { return function() { return true; } },

    // Validate e-mails
    email: function() {
      return function(input) {
        return parseEMail(input) ? true : false;
      }
    }
  };
  
  // Classify and Checker
  return function(subtype) {
    var output = {}
        t = classify(subtype);
  
    if (t) {
      output.pattern = is[t](subtype);
      output.test = function(x) {
        return isJSType.string(x)
            && validate[t](output.pattern)(x);
      }

    } else {
      // Vacuously validate free text (presuming it's a string)
      output.test = isJSType.string;
    }

    return output;
  };
})();