@hestia-earth/schema-validation 22.4.0 → 23.1.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": "22.4.0",
3
+ "version": "23.1.0",
4
4
  "description": "Hestia Schema Validation",
5
5
  "main": "index.js",
6
6
  "typings": "index.d.ts",
package/validate.d.ts CHANGED
@@ -12,10 +12,6 @@ export declare const validateContent: (ajv: Ajv.Ajv, schemas: definitions) => (c
12
12
  success: any;
13
13
  errors: Ajv.ErrorObject[];
14
14
  }>;
15
- declare function uniqueArrayItemValidate(keys: string[], items: any[], _schema?: any, dataPath?: string): boolean;
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 };
19
15
  export declare const initAjv: () => Ajv.Ajv;
20
16
  /**
21
17
  * Create a validator instance. Call with a Node to validate it.
package/validate.js CHANGED
@@ -47,10 +47,11 @@ 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.datePatternValidate = exports.arraySameSizeValidate = exports.uniqueArrayItemValidate = exports.validateContent = void 0;
50
+ exports.validator = exports.initAjv = 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");
54
+ var validators_1 = require("./validators");
54
55
  var geojsonSchema = 'http://json.schemastore.org/geojson';
55
56
  var existingNodeRequired = Object.freeze({ required: ['@id', '@type'] });
56
57
  var validateSchemaType = function (schemas, content) {
@@ -87,66 +88,6 @@ var validateContent = function (ajv, schemas) { return function (content) { retu
87
88
  });
88
89
  }); }; };
89
90
  exports.validateContent = validateContent;
90
- var getByKey = function (prev, curr) { return !curr || !prev ? prev : (Array.isArray(prev) ? prev.map(function (item) { return getItemValue(item, curr); }) : (prev ? prev[curr] : undefined)); };
91
- var getItemValue = function (item, key) {
92
- return key.split('.').reduce(getByKey, item);
93
- };
94
- var uniqueArrayItemKeyword = 'uniqueArrayItem';
95
- function uniqueArrayItemValidate(keys, items, _schema, dataPath) {
96
- if (dataPath === void 0) { dataPath = ''; }
97
- var values = items.map(function (item) { return JSON.stringify(keys.reduce(function (prev, key) {
98
- var _a;
99
- return (__assign(__assign({}, prev), (_a = {}, _a[key] = getItemValue(item, key), _a)));
100
- }, {})); });
101
- var indexes = values
102
- .map(function (value, index) { return values.findIndex(function (otherValue, otherValueIndex) {
103
- return otherValueIndex !== index && otherValue === value;
104
- }); })
105
- .filter(function (index) { return index >= 0; })
106
- .sort();
107
- uniqueArrayItemValidate.errors = indexes.map(function (index) { return ({
108
- dataPath: "".concat(dataPath, "[").concat(index, "]"),
109
- keyword: uniqueArrayItemKeyword,
110
- message: 'every item in the list should be unique',
111
- params: { keyword: uniqueArrayItemKeyword, keys: keys }
112
- }); });
113
- return indexes.length === 0;
114
- }
115
- exports.uniqueArrayItemValidate = uniqueArrayItemValidate;
116
- var arraySameSizeKeyword = 'arraySameSize';
117
- function arraySameSizeValidate(keys, items, _schema, dataPath, item) {
118
- var current = items.length;
119
- var errors = keys
120
- .filter(function (key) { return key in item; })
121
- .map(function (key) {
122
- var expected = item[key].length;
123
- return current !== expected ? {
124
- dataPath: dataPath,
125
- keyword: arraySameSizeKeyword,
126
- message: "must contain as many items as ".concat(key, "s"),
127
- params: { keyword: arraySameSizeKeyword, current: current, expected: expected }
128
- } : null;
129
- })
130
- .filter(Boolean);
131
- arraySameSizeValidate.errors = errors;
132
- return errors.length === 0;
133
- }
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;
150
91
  var initAjv = function () {
151
92
  var ajv = new Ajv({
152
93
  schemaId: 'auto',
@@ -154,21 +95,7 @@ var initAjv = function () {
154
95
  });
155
96
  ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json'));
156
97
  ajv.addSchema(require('./json-schema/geojson.json'), geojsonSchema);
157
- ajv.addKeyword(uniqueArrayItemKeyword, {
158
- type: 'array',
159
- errors: 'full',
160
- validate: uniqueArrayItemValidate
161
- });
162
- ajv.addKeyword(arraySameSizeKeyword, {
163
- type: 'array',
164
- errors: 'full',
165
- validate: arraySameSizeValidate
166
- });
167
- ajv.addKeyword(datePatternKeyword, {
168
- type: ['string', 'array'],
169
- errors: 'full',
170
- validate: datePatternValidate
171
- });
98
+ (0, validators_1.init)(ajv);
172
99
  return ajv;
173
100
  };
174
101
  exports.initAjv = initAjv;
@@ -0,0 +1,2 @@
1
+ export declare const keyword = "arraySameSize";
2
+ export declare function arraySameSizeValidate(keys: string[], items: any[], _schema: any, dataPath: string, item: any): boolean;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.arraySameSizeValidate = exports.keyword = void 0;
4
+ exports.keyword = 'arraySameSize';
5
+ function arraySameSizeValidate(keys, items, _schema, dataPath, item) {
6
+ var current = items.length;
7
+ var errors = keys
8
+ .filter(function (key) { return key in item; })
9
+ .map(function (key) {
10
+ var expected = item[key].length;
11
+ return current !== expected ? {
12
+ dataPath: dataPath,
13
+ keyword: exports.keyword,
14
+ message: "must contain as many items as ".concat(key, "s"),
15
+ params: { keyword: exports.keyword, current: current, expected: expected }
16
+ } : null;
17
+ })
18
+ .filter(Boolean);
19
+ arraySameSizeValidate.errors = errors;
20
+ return errors.length === 0;
21
+ }
22
+ exports.arraySameSizeValidate = arraySameSizeValidate;
@@ -0,0 +1,2 @@
1
+ export declare const keyword = "datePattern";
2
+ export declare function datePatternValidate(_enabled: boolean, value: string | string[], _schema: any, dataPath: string): boolean;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.datePatternValidate = exports.keyword = void 0;
4
+ var isValidDate = function (date) { return isFinite(new Date(date).getTime()); };
5
+ exports.keyword = 'datePattern';
6
+ function datePatternValidate(_enabled, value, _schema, dataPath) {
7
+ var values = Array.isArray(value) ? value : [value];
8
+ var errors = values
9
+ .map(function (v, index) { return isValidDate(v) ? null : {
10
+ dataPath: "".concat(dataPath).concat(Array.isArray(value) ? "[".concat(index, "]") : ''),
11
+ keyword: exports.keyword,
12
+ message: 'not a valid date'
13
+ }; })
14
+ .filter(Boolean);
15
+ datePatternValidate.errors = errors;
16
+ return errors.length === 0;
17
+ }
18
+ exports.datePatternValidate = datePatternValidate;
@@ -0,0 +1,2 @@
1
+ import { Ajv } from 'ajv';
2
+ export declare const init: (ajv: Ajv) => void;
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.init = void 0;
4
+ var arraySameSize_1 = require("./arraySameSize");
5
+ var datePattern_1 = require("./datePattern");
6
+ var uniqueArrayItem_1 = require("./uniqueArrayItem");
7
+ var init = function (ajv) {
8
+ ajv.addKeyword(arraySameSize_1.keyword, {
9
+ type: 'array',
10
+ errors: 'full',
11
+ validate: arraySameSize_1.arraySameSizeValidate
12
+ });
13
+ ajv.addKeyword(datePattern_1.keyword, {
14
+ type: ['string', 'array'],
15
+ errors: 'full',
16
+ validate: datePattern_1.datePatternValidate
17
+ });
18
+ ajv.addKeyword(uniqueArrayItem_1.keyword, {
19
+ type: 'array',
20
+ errors: 'full',
21
+ validate: uniqueArrayItem_1.uniqueArrayItemValidate
22
+ });
23
+ };
24
+ exports.init = init;
@@ -0,0 +1,2 @@
1
+ export declare const keyword = "uniqueArrayItem";
2
+ export declare function uniqueArrayItemValidate(keys: string[], items: any[], _schema?: any, dataPath?: string): boolean;
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.uniqueArrayItemValidate = exports.keyword = void 0;
15
+ var getByKey = function (prev, curr) { return !curr || !prev ? prev : (Array.isArray(prev) ? prev.map(function (item) { return getItemValue(item, curr); }) : (prev ? prev[curr] : undefined)); };
16
+ var getItemValue = function (item, key) {
17
+ return key.split('.').reduce(getByKey, item);
18
+ };
19
+ exports.keyword = 'uniqueArrayItem';
20
+ function uniqueArrayItemValidate(keys, items, _schema, dataPath) {
21
+ if (dataPath === void 0) { dataPath = ''; }
22
+ var values = items.map(function (item) { return JSON.stringify(keys.reduce(function (prev, key) {
23
+ var _a;
24
+ return (__assign(__assign({}, prev), (_a = {}, _a[key] = getItemValue(item, key), _a)));
25
+ }, {})); });
26
+ var indexes = values
27
+ .map(function (value, index) { return values.findIndex(function (otherValue, otherValueIndex) {
28
+ return otherValueIndex !== index && otherValue === value;
29
+ }); })
30
+ .filter(function (index) { return index >= 0; })
31
+ .sort();
32
+ uniqueArrayItemValidate.errors = indexes.map(function (index) { return ({
33
+ dataPath: "".concat(dataPath, "[").concat(index, "]"),
34
+ keyword: exports.keyword,
35
+ message: 'every item in the list should be unique',
36
+ params: { keyword: exports.keyword, keys: keys }
37
+ }); });
38
+ return indexes.length === 0;
39
+ }
40
+ exports.uniqueArrayItemValidate = uniqueArrayItemValidate;