@hestia-earth/schema-validation 15.2.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/README.md CHANGED
@@ -1,9 +1,33 @@
1
1
  # Hestia Schema Validation
2
2
 
3
- Module to validate data using the Hestia Schema and Ajv
3
+ Module to validate data using the Hestia Schema and Ajv.
4
4
 
5
5
  ## Install
6
6
 
7
7
  ```sh
8
- npm install @hestia-earth/schema-validation
8
+ npm install @hestia-earth/schema @hestia-earth/schema-validation
9
+ ```
10
+
11
+ ## Example validating JSON
12
+
13
+ ```javascript
14
+ const { SchemaType } = require('@hestia-earth/schema');
15
+ const { validator } = require('@hestia-earth/schema-validation/validate');
16
+
17
+ // Set to `true` to validate existing Nodes (@type/@id) or `false` for Nodes to upload (type/id)
18
+ const strictMode = false;
19
+ // Initialise the validation function, do this only once
20
+ const schemaValidation = validator(undefined, strictMode);
21
+
22
+ const node = {
23
+ type: SchemaType.Cycle,
24
+ id: 'my first cycle'
25
+ };
26
+
27
+ (async () => {
28
+ const { success, errors } = await schemaValidation(node);
29
+ console.log(success);
30
+ // list of errors in Ajv formatting. See https://github.com/ajv-validator/ajv/tree/v6.12.6#validation-errors
31
+ console.log(errors);
32
+ })();
9
33
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hestia-earth/schema-validation",
3
- "version": "15.2.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;