@hestia-earth/schema-validation 30.3.2 → 30.5.0

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.3.2",
3
+ "version": "30.5.0",
4
4
  "description": "HESTIA Schema Validation",
5
5
  "main": "index.js",
6
6
  "typings": "index.d.ts",
@@ -1,4 +1,5 @@
1
1
  import { Ajv, SchemaValidateFunction } from 'ajv';
2
+ export declare const isValidDate: (date: string) => boolean;
2
3
  export declare const keyword = "datePattern";
3
4
  export declare const validate: SchemaValidateFunction;
4
5
  export declare const init: (ajv: Ajv) => Ajv;
@@ -1,13 +1,44 @@
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
+ };
2
18
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.init = exports.validate = exports.keyword = void 0;
4
- var isValidDate = function (date) { return isFinite(new Date(date).getTime()); };
19
+ exports.init = exports.validate = exports.keyword = exports.isValidDate = void 0;
20
+ /**
21
+ * JavaScript does a lenient parsing of dates, so even `2000-30-30` will return a date.
22
+ * This makes sure the parse date is the date as the given date. *
23
+ *
24
+ * @param value
25
+ * @returns
26
+ */
27
+ 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];
29
+ 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;
31
+ };
32
+ var isValidDate = function (date) {
33
+ return isFinite(new Date(date).getTime()) && (date.length < 10 || isValidDateRange(date));
34
+ };
35
+ exports.isValidDate = isValidDate;
5
36
  exports.keyword = 'datePattern';
6
37
  var validate = function (_schema, value, _parentSchema, dataPath) {
7
38
  var values = Array.isArray(value) ? value : [value];
8
39
  var errors = values
9
40
  .map(function (v, index) {
10
- return isValidDate(v)
41
+ return (0, exports.isValidDate)(v)
11
42
  ? null
12
43
  : {
13
44
  dataPath: "".concat(dataPath).concat(Array.isArray(value) ? "[".concat(index, "]") : ''),
@@ -52,14 +52,21 @@ var findIdenticalIndexes = function (values) { return function (value, index) {
52
52
  return otherIndex;
53
53
  })
54
54
  }); }; };
55
+ /**
56
+ * Make sure empty JSON are not set as `'{}'`.
57
+ *
58
+ * @param value
59
+ * @returns
60
+ */
61
+ var toString = function (value) { return Object.values(value).filter(Boolean).length ? JSON.stringify(value) : null; };
55
62
  var validate = function (schema, data, _parentSchema, dataPath) {
56
63
  if (dataPath === void 0) { dataPath = ''; }
57
64
  var values = data.map(function (item) {
58
- return JSON.stringify(schema.reduce(function (prev, key) {
65
+ return toString(schema.reduce(function (prev, key) {
59
66
  var _a;
60
67
  return (__assign(__assign({}, prev), (_a = {}, _a[key] = getItemValue(item, key), _a)));
61
68
  }, {}));
62
- });
69
+ }).filter(Boolean);
63
70
  var indexes = values
64
71
  .map(findIdenticalIndexes(values))
65
72
  .filter(function (_a) {