@openfn/language-common 1.10.3 → 1.11.1

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 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");
@@ -118,6 +121,9 @@ var isStream = (value) => {
118
121
  return false;
119
122
  };
120
123
  function expandReference(state, value) {
124
+ if (Buffer.isBuffer(value)) {
125
+ return value;
126
+ }
121
127
  if (Array.isArray(value)) {
122
128
  return value.map((v) => expandReference(state, v));
123
129
  }
@@ -261,6 +267,7 @@ __export(dateFns_exports, {
261
267
  var import_date_fns = require("date-fns");
262
268
 
263
269
  // src/Adaptor.js
270
+ var schemaCache = {};
264
271
  function execute(...operations) {
265
272
  return (state) => {
266
273
  const start = Promise.resolve(state);
@@ -517,6 +524,75 @@ function parseCsv(csvData, parsingOptions = {}, callback) {
517
524
  return result;
518
525
  };
519
526
  }
527
+ var ajvVersions = {};
528
+ var getAjvVersion = async (schema) => {
529
+ if (/^https?:\/\/json-schema.org\/draft\/2019/.test(schema)) {
530
+ if (!ajvVersions["2019"]) {
531
+ const Ajv = (await import("ajv/dist/2019.js")).default;
532
+ ajvVersions["2019"] = new Ajv();
533
+ }
534
+ return ajvVersions["2019"];
535
+ }
536
+ if (/^https?:\/\/json-schema.org\/draft\/2020/.test(schema)) {
537
+ if (!ajvVersions["2020"]) {
538
+ const Ajv = (await import("ajv/dist/2020.js")).default;
539
+ ajvVersions["2020"] = new Ajv();
540
+ }
541
+ return ajvVersions["2020"];
542
+ }
543
+ if (!ajvVersions["default"]) {
544
+ const Ajv = (await import("ajv")).default;
545
+ ajvVersions["default"] = new Ajv();
546
+ }
547
+ return ajvVersions["default"];
548
+ };
549
+ function validate(schema = "schema", data = "data") {
550
+ return async (state) => {
551
+ if (!state.validationErrors) {
552
+ state.validationErrors = [];
553
+ }
554
+ const resolvedData = resolveData();
555
+ const resolvedSchema = await resolveSchema();
556
+ const schemaId = resolvedSchema.$id || "schema";
557
+ if (!schemaCache[schemaId]) {
558
+ const ajv = await getAjvVersion(resolvedSchema.$schema);
559
+ schemaCache[schemaId] = ajv.compile(resolvedSchema);
560
+ }
561
+ const validate2 = schemaCache[schemaId];
562
+ if (!validate2(resolvedData)) {
563
+ state.validationErrors.push({
564
+ data: state.data,
565
+ errors: validate2.errors
566
+ });
567
+ }
568
+ return state;
569
+ async function resolveSchema() {
570
+ const [schemaOrUrl] = expandReferences(state, schema);
571
+ if (typeof schemaOrUrl === "string") {
572
+ try {
573
+ const url = new URL(schemaOrUrl);
574
+ const response = await (0, import_undici.request)(url);
575
+ return response.body.json();
576
+ } catch (e) {
577
+ if (e instanceof TypeError) {
578
+ return (0, import_jsonpath_plus.JSONPath)({ path: schemaOrUrl, json: state })[0];
579
+ } else {
580
+ console.error("Error fetching schema from ", schemaOrUrl);
581
+ console.error(e);
582
+ }
583
+ }
584
+ }
585
+ return schemaOrUrl;
586
+ }
587
+ function resolveData() {
588
+ const [d] = expandReferences(state, data);
589
+ if (typeof d === "string") {
590
+ return (0, import_jsonpath_plus.JSONPath)({ path: d, json: state })[0];
591
+ }
592
+ return d;
593
+ }
594
+ };
595
+ }
520
596
 
521
597
  // src/index.js
522
598
  var src_default = Adaptor_exports;
@@ -552,5 +628,6 @@ var src_default = Adaptor_exports;
552
628
  source,
553
629
  sourceValue,
554
630
  splitKeys,
555
- toArray
631
+ toArray,
632
+ validate
556
633
  });
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";
@@ -62,6 +64,9 @@ var isStream = (value) => {
62
64
  return false;
63
65
  };
64
66
  function expandReference(state, value) {
67
+ if (Buffer.isBuffer(value)) {
68
+ return value;
69
+ }
65
70
  if (Array.isArray(value)) {
66
71
  return value.map((v) => expandReference(state, v));
67
72
  }
@@ -205,6 +210,7 @@ __export(dateFns_exports, {
205
210
  import { parse, format } from "date-fns";
206
211
 
207
212
  // src/Adaptor.js
213
+ var schemaCache = {};
208
214
  function execute(...operations) {
209
215
  return (state) => {
210
216
  const start = Promise.resolve(state);
@@ -461,6 +467,75 @@ function parseCsv(csvData, parsingOptions = {}, callback) {
461
467
  return result;
462
468
  };
463
469
  }
470
+ var ajvVersions = {};
471
+ var getAjvVersion = async (schema) => {
472
+ if (/^https?:\/\/json-schema.org\/draft\/2019/.test(schema)) {
473
+ if (!ajvVersions["2019"]) {
474
+ const Ajv = (await import("ajv/dist/2019.js")).default;
475
+ ajvVersions["2019"] = new Ajv();
476
+ }
477
+ return ajvVersions["2019"];
478
+ }
479
+ if (/^https?:\/\/json-schema.org\/draft\/2020/.test(schema)) {
480
+ if (!ajvVersions["2020"]) {
481
+ const Ajv = (await import("ajv/dist/2020.js")).default;
482
+ ajvVersions["2020"] = new Ajv();
483
+ }
484
+ return ajvVersions["2020"];
485
+ }
486
+ if (!ajvVersions["default"]) {
487
+ const Ajv = (await import("ajv")).default;
488
+ ajvVersions["default"] = new Ajv();
489
+ }
490
+ return ajvVersions["default"];
491
+ };
492
+ function validate(schema = "schema", data = "data") {
493
+ return async (state) => {
494
+ if (!state.validationErrors) {
495
+ state.validationErrors = [];
496
+ }
497
+ const resolvedData = resolveData();
498
+ const resolvedSchema = await resolveSchema();
499
+ const schemaId = resolvedSchema.$id || "schema";
500
+ if (!schemaCache[schemaId]) {
501
+ const ajv = await getAjvVersion(resolvedSchema.$schema);
502
+ schemaCache[schemaId] = ajv.compile(resolvedSchema);
503
+ }
504
+ const validate2 = schemaCache[schemaId];
505
+ if (!validate2(resolvedData)) {
506
+ state.validationErrors.push({
507
+ data: state.data,
508
+ errors: validate2.errors
509
+ });
510
+ }
511
+ return state;
512
+ async function resolveSchema() {
513
+ const [schemaOrUrl] = expandReferences(state, schema);
514
+ if (typeof schemaOrUrl === "string") {
515
+ try {
516
+ const url = new URL(schemaOrUrl);
517
+ const response = await request(url);
518
+ return response.body.json();
519
+ } catch (e) {
520
+ if (e instanceof TypeError) {
521
+ return JSONPath({ path: schemaOrUrl, json: state })[0];
522
+ } else {
523
+ console.error("Error fetching schema from ", schemaOrUrl);
524
+ console.error(e);
525
+ }
526
+ }
527
+ }
528
+ return schemaOrUrl;
529
+ }
530
+ function resolveData() {
531
+ const [d] = expandReferences(state, data);
532
+ if (typeof d === "string") {
533
+ return JSONPath({ path: d, json: state })[0];
534
+ }
535
+ return d;
536
+ }
537
+ };
538
+ }
464
539
 
465
540
  // src/index.js
466
541
  var src_default = Adaptor_exports;
@@ -496,5 +571,6 @@ export {
496
571
  source,
497
572
  sourceValue,
498
573
  splitKeys,
499
- toArray
574
+ toArray,
575
+ validate
500
576
  };
package/dist/util.cjs CHANGED
@@ -39,6 +39,9 @@ var isStream = (value) => {
39
39
  return false;
40
40
  };
41
41
  function expandReference(state, value) {
42
+ if (Buffer.isBuffer(value)) {
43
+ return value;
44
+ }
42
45
  if (Array.isArray(value)) {
43
46
  return value.map((v) => expandReference(state, v));
44
47
  }
package/dist/util.js CHANGED
@@ -15,6 +15,9 @@ var isStream = (value) => {
15
15
  return false;
16
16
  };
17
17
  function expandReference(state, value) {
18
+ if (Buffer.isBuffer(value)) {
19
+ return value;
20
+ }
18
21
  if (Array.isArray(value)) {
19
22
  return value.map((v) => expandReference(state, v));
20
23
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfn/language-common",
3
- "version": "1.10.3",
3
+ "version": "1.11.1",
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": {
@@ -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";