File size: 2,107 Bytes
d9494a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { type InputJsonSchema } from '@/logic-function/input-json-schema.type';
import {
  type InputSchema,
  type InputSchemaProperty,
} from '@/workflow/types/InputSchema';
import { isNonEmptyString } from '@sniptt/guards';

const convertProperty = (jsonSchema: InputJsonSchema): InputSchemaProperty => {
  const property: InputSchemaProperty = { type: 'unknown' };

  switch (jsonSchema.type) {
    case 'string':
      property.type = 'string';
      break;
    case 'number':
    case 'integer':
      property.type = 'number';
      break;
    case 'boolean':
      property.type = 'boolean';
      break;
    case 'array':
      property.type = 'array';
      if (jsonSchema.items) {
        property.items = convertProperty(jsonSchema.items);
      }
      break;
    case 'object':
      property.type = 'object';
      if (jsonSchema.properties) {
        property.properties = Object.fromEntries(
          Object.entries(jsonSchema.properties).map(([key, value]) => [
            key,
            convertProperty(value),
          ]),
        );
      }
      break;
    case 'record':
      property.type = 'record';
      break;
    case 'records':
      property.type = 'records';
      break;
    case 'null':
    default:
      property.type = 'unknown';
  }

  if (Array.isArray(jsonSchema.enum)) {
    property.enum = jsonSchema.enum.filter(
      (value): value is string => typeof value === 'string',
    );
  }

  if (jsonSchema.multiline === true) {
    property.multiline = true;
  }

  if (isNonEmptyString(jsonSchema.label)) {
    property.label = jsonSchema.label;
  }

  if (isNonEmptyString(jsonSchema.objectUniversalIdentifier)) {
    property.objectUniversalIdentifier = jsonSchema.objectUniversalIdentifier;
  }

  return property;
};

// Wraps in a single-element array because Twenty's InputSchema represents
// the parameter list of a function -- logic functions take a single params
// object, hence a one-element array containing it.
export const jsonSchemaToInputSchema = (
  jsonSchema: InputJsonSchema,
): InputSchema => {
  return [convertProperty(jsonSchema)];
};