@openfn/language-common 1.10.3 → 1.11.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/dist/index.cjs +77 -3
- package/dist/index.js +75 -2
- package/package.json +5 -4
- package/types/Adaptor.d.ts +16 -0
package/dist/index.cjs
CHANGED
|
@@ -56,7 +56,8 @@ __export(src_exports, {
|
|
|
56
56
|
source: () => source,
|
|
57
57
|
sourceValue: () => sourceValue,
|
|
58
58
|
splitKeys: () => splitKeys,
|
|
59
|
-
toArray: () => toArray
|
|
59
|
+
toArray: () => toArray,
|
|
60
|
+
validate: () => validate
|
|
60
61
|
});
|
|
61
62
|
module.exports = __toCommonJS(src_exports);
|
|
62
63
|
|
|
@@ -93,13 +94,15 @@ __export(Adaptor_exports, {
|
|
|
93
94
|
source: () => source,
|
|
94
95
|
sourceValue: () => sourceValue,
|
|
95
96
|
splitKeys: () => splitKeys,
|
|
96
|
-
toArray: () => toArray
|
|
97
|
+
toArray: () => toArray,
|
|
98
|
+
validate: () => validate
|
|
97
99
|
});
|
|
98
100
|
var import_curry = __toESM(require("lodash/fp/curry.js"), 1);
|
|
99
101
|
var import_fromPairs = __toESM(require("lodash/fp/fromPairs.js"), 1);
|
|
100
102
|
var import_jsonpath_plus = require("jsonpath-plus");
|
|
101
103
|
var import_csv_parse = require("csv-parse");
|
|
102
104
|
var import_node_stream2 = require("stream");
|
|
105
|
+
var import_undici = require("undici");
|
|
103
106
|
|
|
104
107
|
// src/util.js
|
|
105
108
|
var import_node_stream = require("stream");
|
|
@@ -261,6 +264,7 @@ __export(dateFns_exports, {
|
|
|
261
264
|
var import_date_fns = require("date-fns");
|
|
262
265
|
|
|
263
266
|
// src/Adaptor.js
|
|
267
|
+
var schemaCache = {};
|
|
264
268
|
function execute(...operations) {
|
|
265
269
|
return (state) => {
|
|
266
270
|
const start = Promise.resolve(state);
|
|
@@ -517,6 +521,75 @@ function parseCsv(csvData, parsingOptions = {}, callback) {
|
|
|
517
521
|
return result;
|
|
518
522
|
};
|
|
519
523
|
}
|
|
524
|
+
var ajvVersions = {};
|
|
525
|
+
var getAjvVersion = async (schema) => {
|
|
526
|
+
if (/^https?:\/\/json-schema.org\/draft\/2019/.test(schema)) {
|
|
527
|
+
if (!ajvVersions["2019"]) {
|
|
528
|
+
const Ajv = (await import("ajv/dist/2019.js")).default;
|
|
529
|
+
ajvVersions["2019"] = new Ajv();
|
|
530
|
+
}
|
|
531
|
+
return ajvVersions["2019"];
|
|
532
|
+
}
|
|
533
|
+
if (/^https?:\/\/json-schema.org\/draft\/2020/.test(schema)) {
|
|
534
|
+
if (!ajvVersions["2020"]) {
|
|
535
|
+
const Ajv = (await import("ajv/dist/2020.js")).default;
|
|
536
|
+
ajvVersions["2020"] = new Ajv();
|
|
537
|
+
}
|
|
538
|
+
return ajvVersions["2020"];
|
|
539
|
+
}
|
|
540
|
+
if (!ajvVersions["default"]) {
|
|
541
|
+
const Ajv = (await import("ajv")).default;
|
|
542
|
+
ajvVersions["default"] = new Ajv();
|
|
543
|
+
}
|
|
544
|
+
return ajvVersions["default"];
|
|
545
|
+
};
|
|
546
|
+
function validate(schema = "schema", data = "data") {
|
|
547
|
+
return async (state) => {
|
|
548
|
+
if (!state.validationErrors) {
|
|
549
|
+
state.validationErrors = [];
|
|
550
|
+
}
|
|
551
|
+
const resolvedData = resolveData();
|
|
552
|
+
const resolvedSchema = await resolveSchema();
|
|
553
|
+
const schemaId = resolvedSchema.$id || "schema";
|
|
554
|
+
if (!schemaCache[schemaId]) {
|
|
555
|
+
const ajv = await getAjvVersion(resolvedSchema.$schema);
|
|
556
|
+
schemaCache[schemaId] = ajv.compile(resolvedSchema);
|
|
557
|
+
}
|
|
558
|
+
const validate2 = schemaCache[schemaId];
|
|
559
|
+
if (!validate2(resolvedData)) {
|
|
560
|
+
state.validationErrors.push({
|
|
561
|
+
data: state.data,
|
|
562
|
+
errors: validate2.errors
|
|
563
|
+
});
|
|
564
|
+
}
|
|
565
|
+
return state;
|
|
566
|
+
async function resolveSchema() {
|
|
567
|
+
const [schemaOrUrl] = expandReferences(state, schema);
|
|
568
|
+
if (typeof schemaOrUrl === "string") {
|
|
569
|
+
try {
|
|
570
|
+
const url = new URL(schemaOrUrl);
|
|
571
|
+
const response = await (0, import_undici.request)(url);
|
|
572
|
+
return response.body.json();
|
|
573
|
+
} catch (e) {
|
|
574
|
+
if (e instanceof TypeError) {
|
|
575
|
+
return (0, import_jsonpath_plus.JSONPath)({ path: schemaOrUrl, json: state })[0];
|
|
576
|
+
} else {
|
|
577
|
+
console.error("Error fetching schema from ", schemaOrUrl);
|
|
578
|
+
console.error(e);
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
return schemaOrUrl;
|
|
583
|
+
}
|
|
584
|
+
function resolveData() {
|
|
585
|
+
const [d] = expandReferences(state, data);
|
|
586
|
+
if (typeof d === "string") {
|
|
587
|
+
return (0, import_jsonpath_plus.JSONPath)({ path: d, json: state })[0];
|
|
588
|
+
}
|
|
589
|
+
return d;
|
|
590
|
+
}
|
|
591
|
+
};
|
|
592
|
+
}
|
|
520
593
|
|
|
521
594
|
// src/index.js
|
|
522
595
|
var src_default = Adaptor_exports;
|
|
@@ -552,5 +625,6 @@ var src_default = Adaptor_exports;
|
|
|
552
625
|
source,
|
|
553
626
|
sourceValue,
|
|
554
627
|
splitKeys,
|
|
555
|
-
toArray
|
|
628
|
+
toArray,
|
|
629
|
+
validate
|
|
556
630
|
});
|
package/dist/index.js
CHANGED
|
@@ -37,13 +37,15 @@ __export(Adaptor_exports, {
|
|
|
37
37
|
source: () => source,
|
|
38
38
|
sourceValue: () => sourceValue,
|
|
39
39
|
splitKeys: () => splitKeys,
|
|
40
|
-
toArray: () => toArray
|
|
40
|
+
toArray: () => toArray,
|
|
41
|
+
validate: () => validate
|
|
41
42
|
});
|
|
42
43
|
import curry from "lodash/fp/curry.js";
|
|
43
44
|
import fromPairs from "lodash/fp/fromPairs.js";
|
|
44
45
|
import { JSONPath } from "jsonpath-plus";
|
|
45
46
|
import { parse as parse2 } from "csv-parse";
|
|
46
47
|
import { Readable as Readable2 } from "stream";
|
|
48
|
+
import { request } from "undici";
|
|
47
49
|
|
|
48
50
|
// src/util.js
|
|
49
51
|
import { Readable, Writable } from "stream";
|
|
@@ -205,6 +207,7 @@ __export(dateFns_exports, {
|
|
|
205
207
|
import { parse, format } from "date-fns";
|
|
206
208
|
|
|
207
209
|
// src/Adaptor.js
|
|
210
|
+
var schemaCache = {};
|
|
208
211
|
function execute(...operations) {
|
|
209
212
|
return (state) => {
|
|
210
213
|
const start = Promise.resolve(state);
|
|
@@ -461,6 +464,75 @@ function parseCsv(csvData, parsingOptions = {}, callback) {
|
|
|
461
464
|
return result;
|
|
462
465
|
};
|
|
463
466
|
}
|
|
467
|
+
var ajvVersions = {};
|
|
468
|
+
var getAjvVersion = async (schema) => {
|
|
469
|
+
if (/^https?:\/\/json-schema.org\/draft\/2019/.test(schema)) {
|
|
470
|
+
if (!ajvVersions["2019"]) {
|
|
471
|
+
const Ajv = (await import("ajv/dist/2019.js")).default;
|
|
472
|
+
ajvVersions["2019"] = new Ajv();
|
|
473
|
+
}
|
|
474
|
+
return ajvVersions["2019"];
|
|
475
|
+
}
|
|
476
|
+
if (/^https?:\/\/json-schema.org\/draft\/2020/.test(schema)) {
|
|
477
|
+
if (!ajvVersions["2020"]) {
|
|
478
|
+
const Ajv = (await import("ajv/dist/2020.js")).default;
|
|
479
|
+
ajvVersions["2020"] = new Ajv();
|
|
480
|
+
}
|
|
481
|
+
return ajvVersions["2020"];
|
|
482
|
+
}
|
|
483
|
+
if (!ajvVersions["default"]) {
|
|
484
|
+
const Ajv = (await import("ajv")).default;
|
|
485
|
+
ajvVersions["default"] = new Ajv();
|
|
486
|
+
}
|
|
487
|
+
return ajvVersions["default"];
|
|
488
|
+
};
|
|
489
|
+
function validate(schema = "schema", data = "data") {
|
|
490
|
+
return async (state) => {
|
|
491
|
+
if (!state.validationErrors) {
|
|
492
|
+
state.validationErrors = [];
|
|
493
|
+
}
|
|
494
|
+
const resolvedData = resolveData();
|
|
495
|
+
const resolvedSchema = await resolveSchema();
|
|
496
|
+
const schemaId = resolvedSchema.$id || "schema";
|
|
497
|
+
if (!schemaCache[schemaId]) {
|
|
498
|
+
const ajv = await getAjvVersion(resolvedSchema.$schema);
|
|
499
|
+
schemaCache[schemaId] = ajv.compile(resolvedSchema);
|
|
500
|
+
}
|
|
501
|
+
const validate2 = schemaCache[schemaId];
|
|
502
|
+
if (!validate2(resolvedData)) {
|
|
503
|
+
state.validationErrors.push({
|
|
504
|
+
data: state.data,
|
|
505
|
+
errors: validate2.errors
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
return state;
|
|
509
|
+
async function resolveSchema() {
|
|
510
|
+
const [schemaOrUrl] = expandReferences(state, schema);
|
|
511
|
+
if (typeof schemaOrUrl === "string") {
|
|
512
|
+
try {
|
|
513
|
+
const url = new URL(schemaOrUrl);
|
|
514
|
+
const response = await request(url);
|
|
515
|
+
return response.body.json();
|
|
516
|
+
} catch (e) {
|
|
517
|
+
if (e instanceof TypeError) {
|
|
518
|
+
return JSONPath({ path: schemaOrUrl, json: state })[0];
|
|
519
|
+
} else {
|
|
520
|
+
console.error("Error fetching schema from ", schemaOrUrl);
|
|
521
|
+
console.error(e);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
return schemaOrUrl;
|
|
526
|
+
}
|
|
527
|
+
function resolveData() {
|
|
528
|
+
const [d] = expandReferences(state, data);
|
|
529
|
+
if (typeof d === "string") {
|
|
530
|
+
return JSONPath({ path: d, json: state })[0];
|
|
531
|
+
}
|
|
532
|
+
return d;
|
|
533
|
+
}
|
|
534
|
+
};
|
|
535
|
+
}
|
|
464
536
|
|
|
465
537
|
// src/index.js
|
|
466
538
|
var src_default = Adaptor_exports;
|
|
@@ -496,5 +568,6 @@ export {
|
|
|
496
568
|
source,
|
|
497
569
|
sourceValue,
|
|
498
570
|
splitKeys,
|
|
499
|
-
toArray
|
|
571
|
+
toArray,
|
|
572
|
+
validate
|
|
500
573
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openfn/language-common",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.11.0",
|
|
4
4
|
"description": "Common Expressions for OpenFn",
|
|
5
5
|
"homepage": "https://docs.openfn.org",
|
|
6
6
|
"repository": {
|
|
@@ -33,12 +33,14 @@
|
|
|
33
33
|
"type": "module",
|
|
34
34
|
"types": "types/index.d.ts",
|
|
35
35
|
"dependencies": {
|
|
36
|
+
"ajv": "^8.12.0",
|
|
36
37
|
"axios": "1.1.3",
|
|
37
38
|
"csv-parse": "^5.4.0",
|
|
38
39
|
"csvtojson": "^2.0.10",
|
|
39
40
|
"date-fns": "^2.25.0",
|
|
40
41
|
"jsonpath-plus": "^4.0.0",
|
|
41
|
-
"lodash": "^4.17.19"
|
|
42
|
+
"lodash": "^4.17.19",
|
|
43
|
+
"undici": "^5.22.1"
|
|
42
44
|
},
|
|
43
45
|
"devDependencies": {
|
|
44
46
|
"@openfn/buildtools": "^1.0.2",
|
|
@@ -48,8 +50,7 @@
|
|
|
48
50
|
"esno": "^0.16.3",
|
|
49
51
|
"mocha": "9.2.2",
|
|
50
52
|
"nock": "13.2.9",
|
|
51
|
-
"rimraf": "^3.0.2"
|
|
52
|
-
"undici": "^5.22.1"
|
|
53
|
+
"rimraf": "^3.0.2"
|
|
53
54
|
},
|
|
54
55
|
"main": "dist/index.cjs",
|
|
55
56
|
"scripts": {
|
package/types/Adaptor.d.ts
CHANGED
|
@@ -331,6 +331,22 @@ export function chunk(array: any, chunkSize: Integer): any;
|
|
|
331
331
|
* @returns {Operation} The function returns a Promise that resolves to the result of parsing a CSV `stringOrStream`.
|
|
332
332
|
*/
|
|
333
333
|
export function parseCsv(csvData: string | Stream, parsingOptions?: any, callback?: Function): Operation;
|
|
334
|
+
/**
|
|
335
|
+
* Validate against a JSON schema. Any erors are written to an array at `state.validationErrors`.
|
|
336
|
+
* Schema can be passed directly, loaded as a JSON path from state, or loaded from a URL
|
|
337
|
+
* Data can be passed directly or loaded as a JSON path from state.
|
|
338
|
+
* By default, schema is loaded from `state.schema` and data from `state.data`.
|
|
339
|
+
* @param {string|object} schema - The schema, path or URL to validate against
|
|
340
|
+
* @param {string|object} data - The data or path to validate
|
|
341
|
+
* @example <caption>Validate `state.data` with `state.schema`</caption>
|
|
342
|
+
* validate()
|
|
343
|
+
* @example <caption>Validate form data at `state.form` with a schema from a URL</caption>
|
|
344
|
+
* validate("https://www.example.com/schema/record", "form")
|
|
345
|
+
* @example <caption>Validate the each item in `state.records` with a schema from a URL</caption>
|
|
346
|
+
* each("records[*]", validate("https://www.example.com/schema/record"))
|
|
347
|
+
* @returns {Operation}
|
|
348
|
+
*/
|
|
349
|
+
export function validate(schema?: string | object, data?: string | object): Operation;
|
|
334
350
|
export * as beta from "./beta";
|
|
335
351
|
export * as http from "./http";
|
|
336
352
|
export * as dateFns from "./dateFns";
|