@makehq/forman-schema 0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Make
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # Forman Schema Tools
@@ -0,0 +1,22 @@
1
+ export type FormanSchemaField = {
2
+ name?: string;
3
+ type: string;
4
+ required?: boolean;
5
+ default?: string | number | boolean | null;
6
+ options?: {
7
+ value: string;
8
+ }[];
9
+ help?: string;
10
+ spec?: FormanSchemaField[] | FormanSchemaField;
11
+ };
12
+ export type JSONSchemaField = {
13
+ type: string;
14
+ description?: string;
15
+ default?: string | number | boolean | null;
16
+ enum?: string[];
17
+ properties?: Record<string, JSONSchemaField>;
18
+ items?: JSONSchemaField;
19
+ required?: string[];
20
+ };
21
+ export declare function toJSONSchema(field: FormanSchemaField): JSONSchemaField;
22
+ export declare function toFormanSchema(field: JSONSchemaField): FormanSchemaField;
package/dist/index.js ADDED
@@ -0,0 +1,114 @@
1
+ const FORMAN_PRIMITIVE_TYPE_MAP = {
2
+ text: 'string',
3
+ number: 'number',
4
+ boolean: 'boolean',
5
+ date: 'string',
6
+ json: 'string',
7
+ };
8
+ const JSON_PRIMITIVE_TYPE_MAP = {
9
+ string: 'text',
10
+ number: 'number',
11
+ boolean: 'boolean',
12
+ };
13
+ function noEmpty(text) {
14
+ if (!text)
15
+ return undefined;
16
+ return text;
17
+ }
18
+ export function toJSONSchema(field) {
19
+ switch (field.type) {
20
+ case 'collection':
21
+ const required = [];
22
+ const properties = (Array.isArray(field.spec) ? field.spec : []).reduce((object, subField) => {
23
+ if (!subField.name)
24
+ return object;
25
+ if (subField.required)
26
+ required.push(subField.name);
27
+ return Object.defineProperty(object, subField.name, {
28
+ enumerable: true,
29
+ value: toJSONSchema(subField),
30
+ });
31
+ }, {});
32
+ return {
33
+ type: 'object',
34
+ description: noEmpty(field.help),
35
+ properties,
36
+ required,
37
+ };
38
+ case 'array':
39
+ return {
40
+ type: 'array',
41
+ description: noEmpty(field.help),
42
+ items: field.spec &&
43
+ toJSONSchema(Array.isArray(field.spec)
44
+ ? {
45
+ type: 'collection',
46
+ spec: field.spec,
47
+ }
48
+ : field.spec),
49
+ };
50
+ case 'select':
51
+ return {
52
+ type: 'string',
53
+ description: noEmpty(field.help),
54
+ enum: (field.options || []).map(option => option.value),
55
+ };
56
+ default:
57
+ return {
58
+ type: FORMAN_PRIMITIVE_TYPE_MAP[field.type],
59
+ default: field.default != '' && field.default != null ? field.default : undefined,
60
+ description: noEmpty(field.help),
61
+ };
62
+ }
63
+ }
64
+ export function toFormanSchema(field) {
65
+ switch (field.type) {
66
+ case 'object':
67
+ const spec = field.properties
68
+ ? Object.entries(field.properties).map(([name, property]) => {
69
+ const subField = toFormanSchema(property);
70
+ subField.name = name;
71
+ subField.required = field.required?.includes(name) || false;
72
+ return subField;
73
+ })
74
+ : [];
75
+ return {
76
+ type: 'collection',
77
+ help: field.description,
78
+ spec,
79
+ };
80
+ case 'array':
81
+ return {
82
+ type: 'array',
83
+ help: field.description,
84
+ spec: field.items &&
85
+ (field.items?.type === 'object' && field.items.properties
86
+ ? Object.entries(field.items.properties).map(([name, property]) => {
87
+ const subField = toFormanSchema(property);
88
+ subField.name = name;
89
+ subField.required = field.items?.required?.includes(name) || false;
90
+ return subField;
91
+ })
92
+ : toFormanSchema(field.items)),
93
+ };
94
+ case 'string':
95
+ if (field.enum) {
96
+ return {
97
+ type: 'select',
98
+ help: field.description,
99
+ options: field.enum.map(value => ({ value })),
100
+ };
101
+ }
102
+ return {
103
+ type: 'text',
104
+ help: field.description,
105
+ default: field.default,
106
+ };
107
+ default:
108
+ return {
109
+ type: JSON_PRIMITIVE_TYPE_MAP[field.type],
110
+ help: field.description,
111
+ default: field.default,
112
+ };
113
+ }
114
+ }
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@makehq/forman-schema",
3
+ "version": "0.1.0",
4
+ "description": "Foman Schema Tools",
5
+ "license": "MIT",
6
+ "author": "Make",
7
+ "repository": "github:integromat/forman-schema",
8
+ "homepage": "https://www.make.com",
9
+ "type": "module",
10
+ "module": "./dist/index.js",
11
+ "types": "./dist/index.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "import": "./dist/index.js",
15
+ "types": "./dist/index.d.ts"
16
+ }
17
+ },
18
+ "engines": {
19
+ "node": ">=18"
20
+ },
21
+ "files": [
22
+ "dist"
23
+ ],
24
+ "scripts": {
25
+ "test": "jest --runInBand --forceExit --verbose false",
26
+ "build": "tsc"
27
+ },
28
+ "devDependencies": {
29
+ "@jest/globals": "^29.7.0",
30
+ "@types/node": "^22.13.10",
31
+ "jest": "^29.7.0",
32
+ "ts-jest": "^29.2.6",
33
+ "ts-node": "^10.9.2",
34
+ "typescript": "^5.8.2"
35
+ }
36
+ }