@hestia-earth/schema-validation 16.0.0 → 17.0.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": "16.0.0",
3
+ "version": "17.0.0",
4
4
  "description": "Hestia Schema Validation",
5
5
  "main": "index.js",
6
6
  "typings": "index.d.ts",
package/validate.d.ts CHANGED
@@ -13,8 +13,9 @@ export declare const validateContent: (ajv: Ajv.Ajv, schemas: definitions) => (c
13
13
  errors: Ajv.ErrorObject[];
14
14
  }>;
15
15
  declare function uniqueArrayItemValidate(keys: string[], items: any[], _schema?: any, dataPath?: string): boolean;
16
- declare function arraySameSizeValidate(keys: string[], items: any[], _schema: any, dataPath: any, item: any): boolean;
17
- export { uniqueArrayItemValidate, arraySameSizeValidate };
16
+ declare function arraySameSizeValidate(keys: string[], items: any[], _schema: any, dataPath: string, item: any): boolean;
17
+ declare function datePatternValidate(_enabled: boolean, value: string | string[], _schema: any, dataPath: string): boolean;
18
+ export { uniqueArrayItemValidate, arraySameSizeValidate, datePatternValidate };
18
19
  export declare const initAjv: () => Ajv.Ajv;
19
20
  /**
20
21
  * Create a validator instance. Call with a Node to validate it.
package/validate.js CHANGED
@@ -47,7 +47,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
47
47
  }
48
48
  };
49
49
  Object.defineProperty(exports, "__esModule", { value: true });
50
- exports.validator = exports.initAjv = exports.arraySameSizeValidate = exports.uniqueArrayItemValidate = exports.validateContent = void 0;
50
+ exports.validator = exports.initAjv = exports.datePatternValidate = exports.arraySameSizeValidate = exports.uniqueArrayItemValidate = exports.validateContent = void 0;
51
51
  var Ajv = require("ajv");
52
52
  var schema_1 = require("@hestia-earth/schema");
53
53
  var json_schema_1 = require("@hestia-earth/json-schema");
@@ -132,6 +132,21 @@ function arraySameSizeValidate(keys, items, _schema, dataPath, item) {
132
132
  return errors.length === 0;
133
133
  }
134
134
  exports.arraySameSizeValidate = arraySameSizeValidate;
135
+ var datePatternKeyword = 'datePattern';
136
+ var isValidDate = function (date) { return isFinite(new Date(date).getTime()); };
137
+ function datePatternValidate(_enabled, value, _schema, dataPath) {
138
+ var values = Array.isArray(value) ? value : [value];
139
+ var errors = values
140
+ .map(function (v, index) { return isValidDate(v) ? null : {
141
+ dataPath: "".concat(dataPath).concat(Array.isArray(value) ? "[".concat(index, "]") : ''),
142
+ keyword: datePatternKeyword,
143
+ message: 'not a valid date'
144
+ }; })
145
+ .filter(Boolean);
146
+ datePatternValidate.errors = errors;
147
+ return errors.length === 0;
148
+ }
149
+ exports.datePatternValidate = datePatternValidate;
135
150
  var initAjv = function () {
136
151
  var ajv = new Ajv({
137
152
  schemaId: 'auto',
@@ -149,6 +164,11 @@ var initAjv = function () {
149
164
  errors: 'full',
150
165
  validate: arraySameSizeValidate
151
166
  });
167
+ ajv.addKeyword(datePatternKeyword, {
168
+ type: ['string', 'array'],
169
+ errors: 'full',
170
+ validate: datePatternValidate
171
+ });
152
172
  return ajv;
153
173
  };
154
174
  exports.initAjv = initAjv;