@trayio/commons 4.12.0 → 4.13.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,7 @@
1
+ import { SchemaParser } from './SchemaParser';
2
+ export type TestType = {
3
+ strField: string;
4
+ intField?: number;
5
+ };
6
+ export declare function testSchemaParser(testTypeParser: SchemaParser<TestType>): void;
7
+ //# sourceMappingURL=SchemaParser.abstract.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SchemaParser.abstract.test.d.ts","sourceRoot":"","sources":["../../src/zod/SchemaParser.abstract.test.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C,MAAM,MAAM,QAAQ,GAAG;IACtB,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,wBAAgB,gBAAgB,CAAC,cAAc,EAAE,YAAY,CAAC,QAAQ,CAAC,QAkEtE"}
@@ -0,0 +1,80 @@
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
+ exports.testSchemaParser = void 0;
27
+ const E = __importStar(require("fp-ts/Either"));
28
+ const O = __importStar(require("fp-ts/Option"));
29
+ function testSchemaParser(testTypeParser) {
30
+ describe('Schema Parser', () => {
31
+ test('should parse a schema', () => {
32
+ const testValue = {
33
+ strField: 'str',
34
+ intField: 123,
35
+ };
36
+ const parsedOutput = {
37
+ strField: 'str',
38
+ intField: O.some(123),
39
+ };
40
+ const result = testTypeParser.parse(testValue);
41
+ expect(result).toStrictEqual(E.right(parsedOutput));
42
+ });
43
+ test('should parse a schema with optional field', () => {
44
+ const testValue = {
45
+ strField: 'str',
46
+ };
47
+ const parsedOutput = {
48
+ strField: 'str',
49
+ intField: O.none,
50
+ };
51
+ const result = testTypeParser.parse(testValue);
52
+ expect(result).toStrictEqual(E.right(parsedOutput));
53
+ });
54
+ test('should fail to parse a schema with invalid type', () => {
55
+ const testValue = {
56
+ strField: 'str',
57
+ intField: '123',
58
+ };
59
+ const result = testTypeParser.parse(testValue);
60
+ expect(result._tag).toBe('Left');
61
+ });
62
+ test('should fail to parse a schema with missing required field', () => {
63
+ const testValue = {
64
+ intField: 123,
65
+ };
66
+ const result = testTypeParser.parse(testValue);
67
+ expect(result._tag).toBe('Left');
68
+ });
69
+ test('should fail to parse a schema with extra field', () => {
70
+ const testValue = {
71
+ strField: 'str',
72
+ intField: 123,
73
+ extraField: 'extra',
74
+ };
75
+ const result = testTypeParser.parse(testValue);
76
+ expect(result._tag).toBe('Left');
77
+ });
78
+ });
79
+ }
80
+ exports.testSchemaParser = testSchemaParser;
@@ -0,0 +1,5 @@
1
+ import * as E from 'fp-ts/Either';
2
+ export interface SchemaParser<T> {
3
+ parse(value: unknown): E.Either<Error, T>;
4
+ }
5
+ //# sourceMappingURL=SchemaParser.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SchemaParser.d.ts","sourceRoot":"","sources":["../../src/zod/SchemaParser.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAElC,MAAM,WAAW,YAAY,CAAC,CAAC;IAC9B,KAAK,CAAC,KAAK,EAAE,OAAO,GAAG,CAAC,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;CAC1C"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,13 @@
1
+ import { z, ZodSchema } from 'zod';
2
+ import * as E from 'fp-ts/Either';
3
+ import { Option } from 'fp-ts/Option';
4
+ import { SchemaParser } from './SchemaParser';
5
+ export declare const zodOptionFromNullable: <T>(schema: z.ZodType<T, any, any>) => z.ZodType<Option<T>, any, any>;
6
+ export declare class ZodSchemaParser<T> implements SchemaParser<T> {
7
+ private readonly schema;
8
+ constructor(schema: ZodSchema<T>);
9
+ static fromSchema<T>(schema: ZodSchema): SchemaParser<T>;
10
+ parse(value: unknown): E.Either<Error, T>;
11
+ private errorTransformer;
12
+ }
13
+ //# sourceMappingURL=ZodSchemaParser.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ZodSchemaParser.d.ts","sourceRoot":"","sources":["../../src/zod/ZodSchemaParser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,SAAS,EAAsB,MAAM,KAAK,CAAC;AACvD,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAClC,OAAO,EAAE,MAAM,EAAc,MAAM,cAAc,CAAC;AAClD,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C,eAAO,MAAM,qBAAqB,uEAKiC,CAAC;AAEpE,qBAAa,eAAe,CAAC,CAAC,CAAE,YAAW,YAAY,CAAC,CAAC,CAAC;IAC7C,OAAO,CAAC,QAAQ,CAAC,MAAM;gBAAN,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC;IAEjD,MAAM,CAAC,UAAU,CAAC,CAAC,EAAE,MAAM,EAAE,SAAS,GAAG,YAAY,CAAC,CAAC,CAAC;IAIxD,KAAK,CAAC,KAAK,EAAE,OAAO,GAAG,CAAC,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC;IAOzC,OAAO,CAAC,gBAAgB;CAmCxB"}
@@ -0,0 +1,79 @@
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
+ exports.ZodSchemaParser = exports.zodOptionFromNullable = void 0;
27
+ const E = __importStar(require("fp-ts/Either"));
28
+ const Option_1 = require("fp-ts/Option");
29
+ const zodOptionFromNullable = (schema) => schema
30
+ .optional()
31
+ .transform((value) => (value === undefined ? Option_1.none : (0, Option_1.some)(value)));
32
+ exports.zodOptionFromNullable = zodOptionFromNullable;
33
+ class ZodSchemaParser {
34
+ schema;
35
+ constructor(schema) {
36
+ this.schema = schema;
37
+ }
38
+ static fromSchema(schema) {
39
+ return new ZodSchemaParser(schema);
40
+ }
41
+ parse(value) {
42
+ const parseResult = this.schema.safeParse(value);
43
+ return parseResult.success
44
+ ? E.right(parseResult.data)
45
+ : E.left(this.errorTransformer(parseResult.error));
46
+ }
47
+ errorTransformer(zodError) {
48
+ let errorString = '';
49
+ let errorIndex = 1;
50
+ const flattendError = zodError.flatten((issue) => ({
51
+ message: issue.message,
52
+ code: issue.code,
53
+ path: issue.path.join('.'),
54
+ }));
55
+ const { fieldErrors, formErrors } = flattendError;
56
+ Object.keys(fieldErrors).forEach((fieldName) => {
57
+ const errors = fieldErrors[fieldName];
58
+ errors?.forEach((error, index) => {
59
+ errorString += `${errorIndex}. ${error.code} at ${error.path}: ${error.message}`;
60
+ if (index < errors.length - 1) {
61
+ errorString += '\n';
62
+ }
63
+ errorIndex += 1;
64
+ });
65
+ errorString += '\n';
66
+ });
67
+ formErrors.forEach((error, index) => {
68
+ errorString += `${errorIndex}. ${error.code}: ${error.message}`;
69
+ if (index < formErrors.length - 1) {
70
+ errorString += '\n';
71
+ }
72
+ errorIndex += 1;
73
+ errorString += '\n';
74
+ });
75
+ errorString = errorString.trim();
76
+ return new Error(errorString);
77
+ }
78
+ }
79
+ exports.ZodSchemaParser = ZodSchemaParser;
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=ZodSchemaParser.unit.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ZodSchemaParser.unit.test.d.ts","sourceRoot":"","sources":["../../src/zod/ZodSchemaParser.unit.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,46 @@
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 zod_1 = require("zod");
28
+ const SchemaParser_abstract_test_1 = require("./SchemaParser.abstract.test");
29
+ const ZodSchemaParser_1 = require("./ZodSchemaParser");
30
+ const TestSchema = zod_1.z
31
+ .object({
32
+ strField: zod_1.z.string(),
33
+ intField: (0, ZodSchemaParser_1.zodOptionFromNullable)(zod_1.z.number()),
34
+ })
35
+ .strict();
36
+ describe('Zod Schema Parser', () => {
37
+ (0, SchemaParser_abstract_test_1.testSchemaParser)(ZodSchemaParser_1.ZodSchemaParser.fromSchema(TestSchema));
38
+ test('should fail to parse a schema with invalid type', () => {
39
+ const testValue = {
40
+ strField: 'str',
41
+ intField: '123',
42
+ };
43
+ const result = ZodSchemaParser_1.ZodSchemaParser.fromSchema(TestSchema).parse(testValue);
44
+ expect(result).toStrictEqual(E.left(new Error('1. invalid_type at intField: Expected number, received string')));
45
+ });
46
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trayio/commons",
3
- "version": "4.12.0",
3
+ "version": "4.13.0",
4
4
  "description": "Extensions to the standard/core libraries and basic features",
5
5
  "exports": {
6
6
  "./*": "./dist/*.js"
@@ -30,7 +30,8 @@
30
30
  "lodash": "4.17.21",
31
31
  "monocle-ts": "2.3.13",
32
32
  "newtype-ts": "0.3.5",
33
- "uuid": "9.0.0"
33
+ "uuid": "9.0.0",
34
+ "zod": "3.23.5"
34
35
  },
35
36
  "typesVersions": {
36
37
  "*": {