| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | var TIME = /^(\d\d):(\d\d):(\d\d)(\.\d+)?(z|[+-]\d\d:\d\d)?$/i;
|
|---|
| 4 | var DATE_TIME_SEPARATOR = /t|\s/i;
|
|---|
| 5 |
|
|---|
| 6 | var COMPARE_FORMATS = {
|
|---|
| 7 | date: compareDate,
|
|---|
| 8 | time: compareTime,
|
|---|
| 9 | 'date-time': compareDateTime
|
|---|
| 10 | };
|
|---|
| 11 |
|
|---|
| 12 | var $dataMetaSchema = {
|
|---|
| 13 | type: 'object',
|
|---|
| 14 | required: [ '$data' ],
|
|---|
| 15 | properties: {
|
|---|
| 16 | $data: {
|
|---|
| 17 | type: 'string',
|
|---|
| 18 | anyOf: [
|
|---|
| 19 | { format: 'relative-json-pointer' },
|
|---|
| 20 | { format: 'json-pointer' }
|
|---|
| 21 | ]
|
|---|
| 22 | }
|
|---|
| 23 | },
|
|---|
| 24 | additionalProperties: false
|
|---|
| 25 | };
|
|---|
| 26 |
|
|---|
| 27 | module.exports = function (minMax) {
|
|---|
| 28 | var keyword = 'format' + minMax;
|
|---|
| 29 | return function defFunc(ajv) {
|
|---|
| 30 | defFunc.definition = {
|
|---|
| 31 | type: 'string',
|
|---|
| 32 | inline: require('./dotjs/_formatLimit'),
|
|---|
| 33 | statements: true,
|
|---|
| 34 | errors: 'full',
|
|---|
| 35 | dependencies: ['format'],
|
|---|
| 36 | metaSchema: {
|
|---|
| 37 | anyOf: [
|
|---|
| 38 | {type: 'string'},
|
|---|
| 39 | $dataMetaSchema
|
|---|
| 40 | ]
|
|---|
| 41 | }
|
|---|
| 42 | };
|
|---|
| 43 |
|
|---|
| 44 | ajv.addKeyword(keyword, defFunc.definition);
|
|---|
| 45 | ajv.addKeyword('formatExclusive' + minMax, {
|
|---|
| 46 | dependencies: ['format' + minMax],
|
|---|
| 47 | metaSchema: {
|
|---|
| 48 | anyOf: [
|
|---|
| 49 | {type: 'boolean'},
|
|---|
| 50 | $dataMetaSchema
|
|---|
| 51 | ]
|
|---|
| 52 | }
|
|---|
| 53 | });
|
|---|
| 54 | extendFormats(ajv);
|
|---|
| 55 | return ajv;
|
|---|
| 56 | };
|
|---|
| 57 | };
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 | function extendFormats(ajv) {
|
|---|
| 61 | var formats = ajv._formats;
|
|---|
| 62 | for (var name in COMPARE_FORMATS) {
|
|---|
| 63 | var format = formats[name];
|
|---|
| 64 | // the last condition is needed if it's RegExp from another window
|
|---|
| 65 | if (typeof format != 'object' || format instanceof RegExp || !format.validate)
|
|---|
| 66 | format = formats[name] = { validate: format };
|
|---|
| 67 | if (!format.compare)
|
|---|
| 68 | format.compare = COMPARE_FORMATS[name];
|
|---|
| 69 | }
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 | function compareDate(d1, d2) {
|
|---|
| 74 | if (!(d1 && d2)) return;
|
|---|
| 75 | if (d1 > d2) return 1;
|
|---|
| 76 | if (d1 < d2) return -1;
|
|---|
| 77 | if (d1 === d2) return 0;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 | function compareTime(t1, t2) {
|
|---|
| 82 | if (!(t1 && t2)) return;
|
|---|
| 83 | t1 = t1.match(TIME);
|
|---|
| 84 | t2 = t2.match(TIME);
|
|---|
| 85 | if (!(t1 && t2)) return;
|
|---|
| 86 | t1 = t1[1] + t1[2] + t1[3] + (t1[4]||'');
|
|---|
| 87 | t2 = t2[1] + t2[2] + t2[3] + (t2[4]||'');
|
|---|
| 88 | if (t1 > t2) return 1;
|
|---|
| 89 | if (t1 < t2) return -1;
|
|---|
| 90 | if (t1 === t2) return 0;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 | function compareDateTime(dt1, dt2) {
|
|---|
| 95 | if (!(dt1 && dt2)) return;
|
|---|
| 96 | dt1 = dt1.split(DATE_TIME_SEPARATOR);
|
|---|
| 97 | dt2 = dt2.split(DATE_TIME_SEPARATOR);
|
|---|
| 98 | var res = compareDate(dt1[0], dt2[0]);
|
|---|
| 99 | if (res === undefined) return;
|
|---|
| 100 | return res || compareTime(dt1[1], dt2[1]);
|
|---|
| 101 | }
|
|---|