@hestia-earth/schema-validation 30.7.0 → 30.7.2

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,6 +1,6 @@
1
1
  {
2
2
  "name": "@hestia-earth/schema-validation",
3
- "version": "30.7.0",
3
+ "version": "30.7.2",
4
4
  "description": "HESTIA Schema Validation",
5
5
  "main": "index.js",
6
6
  "typings": "index.d.ts",
@@ -19,7 +19,8 @@ var validate = function (schema, data, _parentSchema, dataPath, parentData) {
19
19
  : null;
20
20
  })
21
21
  .filter(Boolean);
22
- exports.validate.errors = errors;
22
+ // only return the first 100 errors to limit the memory footprint
23
+ exports.validate.errors = errors.slice(0, 100);
23
24
  return errors.length === 0;
24
25
  };
25
26
  exports.validate = validate;
@@ -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 _a = __read(value.substring(0, 10).split('-').map(Number), 3), year = _a[0], month = _a[1], day = _a[2];
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.getFullYear() === year && date.getMonth() === month - 1 && date.getDate() === day;
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
- return isFinite(new Date(date).getTime()) && (date.length < 10 || isValidDateRange(date));
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, "]") : ''),
@@ -49,7 +40,8 @@ var validate = function (_schema, value, _parentSchema, dataPath) {
49
40
  };
50
41
  })
51
42
  .filter(Boolean);
52
- exports.validate.errors = errors;
43
+ // only return the first 100 errors to limit the memory footprint
44
+ exports.validate.errors = errors.slice(0, 100);
53
45
  return errors.length === 0;
54
46
  };
55
47
  exports.validate = validate;
@@ -0,0 +1,4 @@
1
+ import { Ajv, SchemaValidateFunction } from 'ajv';
2
+ export declare const keyword = "dateTimePattern";
3
+ export declare const validate: SchemaValidateFunction;
4
+ export declare const init: (ajv: Ajv) => Ajv;
@@ -0,0 +1,33 @@
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
+ // only return the first 100 errors to limit the memory footprint
22
+ exports.validate.errors = errors.slice(0, 100);
23
+ return errors.length === 0;
24
+ };
25
+ exports.validate = validate;
26
+ var init = function (ajv) {
27
+ return ajv.addKeyword(exports.keyword, {
28
+ type: ['string', 'array'],
29
+ errors: 'full',
30
+ validate: exports.validate
31
+ });
32
+ };
33
+ exports.init = init;
@@ -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;
@@ -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
  };