function schemaValueToIoTs()

in src/client/src/schema/validation/schema_to_io_ts.ts [25:65]


function schemaValueToIoTs<Value>(value: SchemaValue<Value>): t.Mixed {
  // We need to check the pass_through type on top of everything
  if ((value as { type: 'pass_through' }).type === 'pass_through') {
    return t.unknown;
  }

  if ('properties' in value) {
    const { DYNAMIC_KEY, ...properties } = value.properties as SchemaObject<Value>['properties'] & {
      DYNAMIC_KEY?: SchemaValue<unknown>;
    };
    const schemas: t.Mixed[] = [schemaObjectToIoTs<Record<string, unknown>>({ properties })];
    if (DYNAMIC_KEY) {
      schemas.push(t.record(t.string, schemaValueToIoTs(DYNAMIC_KEY)));
    }
    return isOneOfCandidate(schemas) ? t.union(schemas) : schemas[0];
  } else {
    const valueType = value.type; // Copied in here because of TS reasons, it's not available in the `default` case
    switch (valueType) {
      case 'boolean':
        return t.boolean;
      case 'keyword':
      case 'text':
      case 'date':
        return t.string;
      case 'byte':
      case 'double':
      case 'float':
      case 'integer':
      case 'long':
      case 'short':
        return t.number;
      case 'array':
        if ('items' in value) {
          return t.array(schemaValueToIoTs((value as SchemaArray<unknown, unknown>).items));
        }
        throw new Error(`Schema type must include the "items" declaration.`);
      default:
        throw new Error(`Unsupported schema type ${valueType}.`);
    }
  }
}