Projekt

Obecné

Profil

Stáhnout (959 Bajtů) Statistiky
| Větev: | Revize:
1
'use strict';
2

    
3
module.exports = function defFunc(ajv) {
4
  defFunc.definition = {
5
    type: 'string',
6
    inline: function (it, keyword, schema) {
7
      return getRegExp() + '.test(data' + (it.dataLevel || '') + ')';
8

    
9
      function getRegExp() {
10
        try {
11
          if (typeof schema == 'object')
12
            return new RegExp(schema.pattern, schema.flags);
13

    
14
          var rx = schema.match(/^\/(.*)\/([gimuy]*)$/);
15
          if (rx) return new RegExp(rx[1], rx[2]);
16
          throw new Error('cannot parse string into RegExp');
17
        } catch(e) {
18
          console.error('regular expression', schema, 'is invalid');
19
          throw e;
20
        }
21
      }
22
    },
23
    metaSchema: {
24
      type: ['string', 'object'],
25
      properties: {
26
        pattern: { type: 'string' },
27
        flags: { type: 'string' }
28
      },
29
      required: ['pattern'],
30
      additionalProperties: false
31
    }
32
  };
33

    
34
  ajv.addKeyword('regexp', defFunc.definition);
35
  return ajv;
36
};
(16-16/21)