@hestia-earth/schema-validation 30.6.2 → 30.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Ajv, SchemaValidateFunction } from 'ajv';
|
|
2
|
-
export declare const isValidDate: (date: string) => boolean;
|
|
2
|
+
export declare const isValidDate: (date: string, withTime?: boolean) => boolean;
|
|
3
3
|
export declare const keyword = "datePattern";
|
|
4
4
|
export declare const validate: SchemaValidateFunction;
|
|
5
5
|
export declare const init: (ajv: Ajv) => Ajv;
|
|
@@ -1,20 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
3
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
4
|
-
if (!m) return o;
|
|
5
|
-
var i = m.call(o), r, ar = [], e;
|
|
6
|
-
try {
|
|
7
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
8
|
-
}
|
|
9
|
-
catch (error) { e = { error: error }; }
|
|
10
|
-
finally {
|
|
11
|
-
try {
|
|
12
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
13
|
-
}
|
|
14
|
-
finally { if (e) throw e.error; }
|
|
15
|
-
}
|
|
16
|
-
return ar;
|
|
17
|
-
};
|
|
18
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
3
|
exports.init = exports.validate = exports.keyword = exports.isValidDate = void 0;
|
|
20
4
|
/**
|
|
@@ -25,12 +9,19 @@ exports.init = exports.validate = exports.keyword = exports.isValidDate = void 0
|
|
|
25
9
|
* @returns
|
|
26
10
|
*/
|
|
27
11
|
var isValidDateRange = function (value) {
|
|
28
|
-
var
|
|
12
|
+
var values = value.substring(0, 10).split('-').map(Number);
|
|
13
|
+
var year = values[0] || 1970;
|
|
14
|
+
var month = values[1] || 1;
|
|
15
|
+
var day = values[2] || 1;
|
|
29
16
|
var date = new Date(year, month - 1, day, 12); // Months are 0-indexed
|
|
30
|
-
return date
|
|
17
|
+
return (date <= new Date() && // date cannot be in the future
|
|
18
|
+
date.getFullYear() === year &&
|
|
19
|
+
date.getMonth() === month - 1 && // Months are 0-indexed
|
|
20
|
+
date.getDate() === day);
|
|
31
21
|
};
|
|
32
|
-
var isValidDate = function (date) {
|
|
33
|
-
|
|
22
|
+
var isValidDate = function (date, withTime) {
|
|
23
|
+
if (withTime === void 0) { withTime = true; }
|
|
24
|
+
return (date.length <= 10 || withTime) && isFinite(new Date(date).getTime()) && isValidDateRange(date);
|
|
34
25
|
};
|
|
35
26
|
exports.isValidDate = isValidDate;
|
|
36
27
|
exports.keyword = 'datePattern';
|
|
@@ -38,7 +29,7 @@ var validate = function (_schema, value, _parentSchema, dataPath) {
|
|
|
38
29
|
var values = Array.isArray(value) ? value : [value];
|
|
39
30
|
var errors = values
|
|
40
31
|
.map(function (v, index) {
|
|
41
|
-
return (0, exports.isValidDate)(v)
|
|
32
|
+
return (0, exports.isValidDate)(v, false)
|
|
42
33
|
? null
|
|
43
34
|
: {
|
|
44
35
|
dataPath: "".concat(dataPath).concat(Array.isArray(value) ? "[".concat(index, "]") : ''),
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.init = exports.validate = exports.keyword = void 0;
|
|
4
|
+
var datePattern_1 = require("./datePattern");
|
|
5
|
+
exports.keyword = 'dateTimePattern';
|
|
6
|
+
var validate = function (_schema, value, _parentSchema, dataPath) {
|
|
7
|
+
var values = Array.isArray(value) ? value : [value];
|
|
8
|
+
var errors = values
|
|
9
|
+
.map(function (v, index) {
|
|
10
|
+
return (0, datePattern_1.isValidDate)(v)
|
|
11
|
+
? null
|
|
12
|
+
: {
|
|
13
|
+
dataPath: "".concat(dataPath).concat(Array.isArray(value) ? "[".concat(index, "]") : ''),
|
|
14
|
+
keyword: exports.keyword,
|
|
15
|
+
schemaPath: '#/invalid',
|
|
16
|
+
message: 'not a valid date',
|
|
17
|
+
params: {}
|
|
18
|
+
};
|
|
19
|
+
})
|
|
20
|
+
.filter(Boolean);
|
|
21
|
+
exports.validate.errors = errors;
|
|
22
|
+
return errors.length === 0;
|
|
23
|
+
};
|
|
24
|
+
exports.validate = validate;
|
|
25
|
+
var init = function (ajv) {
|
|
26
|
+
return ajv.addKeyword(exports.keyword, {
|
|
27
|
+
type: ['string', 'array'],
|
|
28
|
+
errors: 'full',
|
|
29
|
+
validate: exports.validate
|
|
30
|
+
});
|
|
31
|
+
};
|
|
32
|
+
exports.init = init;
|
package/validators/index.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { Ajv } from 'ajv';
|
|
2
2
|
import { keyword as arraySameSize } from './arraySameSize';
|
|
3
3
|
import { keyword as datePattern } from './datePattern';
|
|
4
|
+
import { keyword as dateTimePattern } from './dateTimePattern';
|
|
4
5
|
import { keyword as geojson } from './geojson';
|
|
5
6
|
import { keyword as uniqueArrayItem } from './uniqueArrayItem';
|
|
6
|
-
export { arraySameSize, datePattern, geojson, uniqueArrayItem };
|
|
7
|
+
export { arraySameSize, datePattern, dateTimePattern, geojson, uniqueArrayItem };
|
|
7
8
|
export declare const init: (ajv: Ajv) => void;
|
package/validators/index.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.init = exports.uniqueArrayItem = exports.geojson = exports.datePattern = exports.arraySameSize = void 0;
|
|
3
|
+
exports.init = exports.uniqueArrayItem = exports.geojson = exports.dateTimePattern = exports.datePattern = exports.arraySameSize = void 0;
|
|
4
4
|
var arraySameSize_1 = require("./arraySameSize");
|
|
5
5
|
Object.defineProperty(exports, "arraySameSize", { enumerable: true, get: function () { return arraySameSize_1.keyword; } });
|
|
6
6
|
var datePattern_1 = require("./datePattern");
|
|
7
7
|
Object.defineProperty(exports, "datePattern", { enumerable: true, get: function () { return datePattern_1.keyword; } });
|
|
8
|
+
var dateTimePattern_1 = require("./dateTimePattern");
|
|
9
|
+
Object.defineProperty(exports, "dateTimePattern", { enumerable: true, get: function () { return dateTimePattern_1.keyword; } });
|
|
8
10
|
var geojson_1 = require("./geojson");
|
|
9
11
|
Object.defineProperty(exports, "geojson", { enumerable: true, get: function () { return geojson_1.keyword; } });
|
|
10
12
|
var uniqueArrayItem_1 = require("./uniqueArrayItem");
|
|
@@ -12,6 +14,7 @@ Object.defineProperty(exports, "uniqueArrayItem", { enumerable: true, get: funct
|
|
|
12
14
|
var init = function (ajv) {
|
|
13
15
|
(0, arraySameSize_1.init)(ajv);
|
|
14
16
|
(0, datePattern_1.init)(ajv);
|
|
17
|
+
(0, dateTimePattern_1.init)(ajv);
|
|
15
18
|
(0, geojson_1.init)(ajv);
|
|
16
19
|
(0, uniqueArrayItem_1.init)(ajv);
|
|
17
20
|
};
|