@trayio/commons 3.10.0 → 3.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.
@@ -0,0 +1,15 @@
1
+ import * as E from 'fp-ts/Either';
2
+ import * as t from 'io-ts';
3
+ import { DynamicObject } from '../dynamictype/DynamicType';
4
+ declare const dynamicSchemaTypeDescriptor: t.BrandC<t.Type<DynamicObject, DynamicObject, unknown>, DynamicSchemaBrand>;
5
+ export type DynamicSchema = t.TypeOf<typeof dynamicSchemaTypeDescriptor>;
6
+ export interface DynamicSchemaBrand {
7
+ readonly DynamicSchema: unique symbol;
8
+ }
9
+ export interface DynamicSchemaInterface {
10
+ fromDynamicObject: (schemaObj: DynamicObject) => E.Either<Error, DynamicSchema>;
11
+ validate: (schema: DynamicSchema, value: DynamicObject) => E.Either<Error, undefined>;
12
+ }
13
+ export declare const DynamicSchema: DynamicSchemaInterface;
14
+ export {};
15
+ //# sourceMappingURL=DynamicSchema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DynamicSchema.d.ts","sourceRoot":"","sources":["../../src/dynamicschema/DynamicSchema.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAClC,OAAO,KAAK,CAAC,MAAM,OAAO,CAAC;AAE3B,OAAO,EACN,aAAa,EAEb,MAAM,4BAA4B,CAAC;AAoBpC,QAAA,MAAM,2BAA2B,6EAKhC,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,2BAA2B,CAAC,CAAC;AAMzE,MAAM,WAAW,kBAAkB;IAClC,QAAQ,CAAC,aAAa,EAAE,OAAO,MAAM,CAAC;CACtC;AAED,MAAM,WAAW,sBAAsB;IACtC,iBAAiB,EAAE,CAClB,SAAS,EAAE,aAAa,KACpB,CAAC,CAAC,MAAM,CAAC,KAAK,EAAE,aAAa,CAAC,CAAC;IACpC,QAAQ,EAAE,CACT,MAAM,EAAE,aAAa,EACrB,KAAK,EAAE,aAAa,KAChB,CAAC,CAAC,MAAM,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC;CAChC;AAED,eAAO,MAAM,aAAa,EAAE,sBAkB3B,CAAC"}
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.DynamicSchema = void 0;
30
+ const ajv_draft_04_1 = __importDefault(require("ajv-draft-04"));
31
+ const ajv_formats_1 = __importDefault(require("ajv-formats"));
32
+ const E = __importStar(require("fp-ts/Either"));
33
+ const t = __importStar(require("io-ts"));
34
+ const function_1 = require("fp-ts/function");
35
+ const DynamicType_1 = require("../dynamictype/DynamicType");
36
+ const TypeCodec_1 = require("../codec/TypeCodec");
37
+ const ajv = new ajv_draft_04_1.default();
38
+ /*
39
+ We define additional formats for the JSON schema
40
+ See: https://ajv.js.org/packages/ajv-formats.html#formats
41
+ */
42
+ (0, ajv_formats_1.default)(ajv);
43
+ const isDynamicSchema = (o) => {
44
+ try {
45
+ ajv.compile(o);
46
+ return true;
47
+ }
48
+ catch (error) {
49
+ return false;
50
+ }
51
+ };
52
+ const dynamicSchemaTypeDescriptor = t.brand(DynamicType_1.dynamicObjectTypeDescriptor, (input) => isDynamicSchema(input), 'DynamicSchema');
53
+ const dynamicSchemaTypeCodec = TypeCodec_1.TypeCodec.fromDescriptor(dynamicSchemaTypeDescriptor);
54
+ exports.DynamicSchema = {
55
+ fromDynamicObject: (schemaObj) => (0, function_1.pipe)(dynamicSchemaTypeCodec.decode(schemaObj), E.mapLeft((e) => new Error(`Failed to create DynamicSchema: ${e}`))),
56
+ validate: (schema, value) => {
57
+ const validate = ajv.compile(schema);
58
+ if (validate(value)) {
59
+ return E.right(undefined);
60
+ }
61
+ return E.left(new Error(ajv.errorsText(validate.errors)));
62
+ },
63
+ };
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=DynamicSchema.unit.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DynamicSchema.unit.test.d.ts","sourceRoot":"","sources":["../../src/dynamicschema/DynamicSchema.unit.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ const E = __importStar(require("fp-ts/Either"));
27
+ const DynamicSchema_1 = require("./DynamicSchema");
28
+ describe('DynamicSchema', () => {
29
+ let validSchema;
30
+ let invalidSchema;
31
+ let value;
32
+ beforeEach(() => {
33
+ validSchema = {
34
+ type: 'object',
35
+ properties: {
36
+ name: {
37
+ type: 'string',
38
+ },
39
+ age: {
40
+ type: 'number',
41
+ },
42
+ },
43
+ required: ['name', 'age'],
44
+ };
45
+ invalidSchema = {
46
+ type: 'invalidType',
47
+ properties: {
48
+ name: {
49
+ type: 'string',
50
+ },
51
+ age: {
52
+ type: 'number',
53
+ },
54
+ },
55
+ required: ['name', 'age'],
56
+ };
57
+ value = {
58
+ name: 'John Doe',
59
+ age: 30,
60
+ };
61
+ });
62
+ describe('fromDynamicObject', () => {
63
+ it('should return a Right DynamicSchema when given a valid schema object', () => {
64
+ const result = DynamicSchema_1.DynamicSchema.fromDynamicObject(validSchema);
65
+ expect(E.isRight(result)).toBe(true);
66
+ if (E.isRight(result)) {
67
+ expect(result.right).toEqual(validSchema);
68
+ }
69
+ });
70
+ it('should return a Left Error when given an invalid schema object', () => {
71
+ const result = DynamicSchema_1.DynamicSchema.fromDynamicObject(invalidSchema);
72
+ expect(E.isLeft(result)).toBe(true);
73
+ if (E.isLeft(result)) {
74
+ expect(result.left).toBeInstanceOf(Error);
75
+ expect(result.left.message).toContain('Failed to create DynamicSchema:');
76
+ }
77
+ });
78
+ });
79
+ describe('validate', () => {
80
+ it('should return a Right undefined when the value matches the schema', () => {
81
+ const schema = DynamicSchema_1.DynamicSchema.fromDynamicObject(validSchema);
82
+ if (E.isRight(schema)) {
83
+ const result = DynamicSchema_1.DynamicSchema.validate(schema.right, value);
84
+ expect(E.isRight(result)).toBe(true);
85
+ if (E.isRight(result)) {
86
+ expect(result.right).toBeUndefined();
87
+ }
88
+ }
89
+ });
90
+ it('should return a Left Error when the value does not match the schema', () => {
91
+ const schema = DynamicSchema_1.DynamicSchema.fromDynamicObject(validSchema);
92
+ if (E.isRight(schema)) {
93
+ const result = DynamicSchema_1.DynamicSchema.validate(schema.right, invalidSchema);
94
+ expect(E.isLeft(result)).toBe(true);
95
+ if (E.isLeft(result)) {
96
+ expect(result.left).toBeInstanceOf(Error);
97
+ }
98
+ }
99
+ });
100
+ });
101
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trayio/commons",
3
- "version": "3.10.0",
3
+ "version": "3.11.0",
4
4
  "description": "Extensions to the standard/core libraries and basic features",
5
5
  "exports": {
6
6
  "./*": "./dist/*.js"
@@ -17,7 +17,10 @@
17
17
  "@types/formidable": "3.4.5",
18
18
  "@types/node": "18.11.9",
19
19
  "@types/uuid": "9.0.2",
20
- "formidable": "^3.5.1",
20
+ "ajv": "^8.12.0",
21
+ "ajv-draft-04": "^1.0.0",
22
+ "ajv-formats": "^2.1.1",
23
+ "formidable": "3.5.1",
21
24
  "fp-ts": "2.12.3",
22
25
  "fs-extra": "11.1.0",
23
26
  "io-ts": "2.2.20",