• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

teableio / teable / 8389034572

22 Mar 2024 10:38AM CUT coverage: 26.087% (-2.1%) from 28.208%
8389034572

Pull #487

github

web-flow
Merge 3045b1f94 into a06c6afb1
Pull Request #487: refactor: move zod schema to openapi

2100 of 3363 branches covered (62.44%)

282 of 757 new or added lines in 74 files covered. (37.25%)

224 existing lines in 8 files now uncovered.

25574 of 98035 relevant lines covered (26.09%)

5.17 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/packages/openapi/src/table/create.ts
1
import type { RouteConfig } from '@asteasolutions/zod-to-openapi';
×
NEW
2
import {
×
NEW
3
  createFieldRoSchema,
×
NEW
4
  fieldVoSchema,
×
NEW
5
  IdPrefix,
×
NEW
6
  viewRoSchema,
×
NEW
7
  viewVoSchema,
×
NEW
8
  recordSchema,
×
NEW
9
} from '@teable/core';
×
10
import { axios } from '../axios';
×
NEW
11
import { createRecordsRoSchema, fieldKeyTypeRoSchema } from '../record';
×
12
import { registerRoute, urlBuilder } from '../utils';
×
13
import { z } from '../zod';
×
14

×
NEW
15
export const tableFullVoSchema = z
×
NEW
16
  .object({
×
NEW
17
    id: z.string().startsWith(IdPrefix.Table).openapi({
×
NEW
18
      description: 'The id of table.',
×
NEW
19
    }),
×
NEW
20
    name: z.string().openapi({
×
NEW
21
      description: 'The name of the table.',
×
NEW
22
    }),
×
NEW
23
    dbTableName: z
×
NEW
24
      .string()
×
NEW
25
      .regex(/^[a-z]\w{0,62}$/i, {
×
NEW
26
        message: 'Invalid name format',
×
NEW
27
      })
×
NEW
28
      .openapi({
×
NEW
29
        description:
×
NEW
30
          'Table name in backend database. Limitation: 1-63 characters, start with letter, can only contain letters, numbers and underscore, case insensitive, cannot be duplicated with existing db table name in the base.',
×
NEW
31
      }),
×
NEW
32
    description: z.string().optional().openapi({
×
NEW
33
      description: 'The description of the table.',
×
NEW
34
    }),
×
NEW
35
    icon: z.string().emoji().optional().openapi({
×
NEW
36
      description: 'The emoji icon string of the table.',
×
NEW
37
    }),
×
NEW
38
    fields: fieldVoSchema.array().openapi({
×
NEW
39
      description: 'The fields of the table.',
×
NEW
40
    }),
×
NEW
41
    views: viewVoSchema.array().openapi({
×
NEW
42
      description: 'The views of the table.',
×
NEW
43
    }),
×
NEW
44
    records: recordSchema.array().openapi({
×
NEW
45
      description: 'The records of the table.',
×
NEW
46
    }),
×
NEW
47
    order: z.number().optional(),
×
NEW
48
    lastModifiedTime: z.string().optional().openapi({
×
NEW
49
      description: 'The last modified time of the table.',
×
NEW
50
    }),
×
NEW
51
    defaultViewId: z.string().startsWith(IdPrefix.View).optional().openapi({
×
NEW
52
      description: 'The default view id of the table.',
×
NEW
53
    }),
×
NEW
54
  })
×
NEW
55
  .openapi({
×
NEW
56
    description: 'Complete table structure data and initial record data.',
×
NEW
57
  });
×
NEW
58

×
NEW
59
export type ITableFullVo = z.infer<typeof tableFullVoSchema>;
×
NEW
60

×
NEW
61
export const tableVoSchema = tableFullVoSchema.partial({
×
NEW
62
  fields: true,
×
NEW
63
  views: true,
×
NEW
64
  records: true,
×
NEW
65
});
×
NEW
66

×
NEW
67
export type ITableVo = z.infer<typeof tableVoSchema>;
×
NEW
68

×
NEW
69
export const tableRoSchema = tableFullVoSchema
×
NEW
70
  .omit({
×
NEW
71
    id: true,
×
NEW
72
    lastModifiedTime: true,
×
NEW
73
    defaultViewId: true,
×
NEW
74
  })
×
NEW
75
  .partial({
×
NEW
76
    name: true,
×
NEW
77
    dbTableName: true,
×
NEW
78
  })
×
NEW
79
  .merge(
×
NEW
80
    z.object({
×
NEW
81
      name: tableFullVoSchema.shape.name.min(1).optional(),
×
NEW
82
      description: tableFullVoSchema.shape.description.nullable(),
×
NEW
83
      icon: tableFullVoSchema.shape.icon.nullable(),
×
NEW
84
      fieldKeyType: fieldKeyTypeRoSchema,
×
NEW
85
      fields: createFieldRoSchema.array().optional().openapi({
×
NEW
86
        description:
×
NEW
87
          'The fields of the table. If it is empty, 3 fields include SingleLineText, Number, SingleSelect will and 3 empty records be generated by default.',
×
NEW
88
      }),
×
NEW
89
      views: viewRoSchema.array().optional().openapi({
×
NEW
90
        description:
×
NEW
91
          'The views of the table. If it is empty, a grid view will be generated by default.',
×
NEW
92
      }),
×
NEW
93
      records: createRecordsRoSchema.shape.records.optional().openapi({
×
NEW
94
        description:
×
NEW
95
          'The record data of the table. If it is empty, 3 empty records will be generated by default.',
×
NEW
96
      }),
×
NEW
97
    })
×
NEW
98
  )
×
NEW
99
  .openapi({
×
NEW
100
    description: 'params for create a table',
×
NEW
101
  });
×
NEW
102

×
NEW
103
export type ICreateTableRo = z.infer<typeof tableRoSchema>;
×
NEW
104

×
NEW
105
export const tableRoWithDefaultSchema = tableRoSchema.required({
×
NEW
106
  fields: true,
×
NEW
107
  views: true,
×
NEW
108
});
×
NEW
109

×
NEW
110
export type ICreateTableWithDefault = z.infer<typeof tableRoWithDefaultSchema>;
×
NEW
111

×
NEW
112
export const tablePropertyKeySchema = tableRoSchema.pick({
×
NEW
113
  name: true,
×
NEW
114
  dbTableName: true,
×
NEW
115
  description: true,
×
NEW
116
  icon: true,
×
NEW
117
  order: true,
×
NEW
118
});
×
NEW
119

×
NEW
120
export const tableOpSchema = tableVoSchema.pick({
×
NEW
121
  id: true,
×
NEW
122
  name: true,
×
NEW
123
  description: true,
×
NEW
124
  order: true,
×
NEW
125
  icon: true,
×
NEW
126
  lastModifiedTime: true,
×
NEW
127
});
×
NEW
128

×
NEW
129
export const tableListVoSchema = tableVoSchema.array().openapi({
×
NEW
130
  description: 'The list of tables.',
×
NEW
131
});
×
NEW
132

×
133
export const CREATE_TABLE = '/base/{baseId}/table/';
×
134

×
135
export const CreateTableRoute: RouteConfig = registerRoute({
×
136
  method: 'post',
×
137
  path: CREATE_TABLE,
×
138
  description: 'Create a table',
×
139
  request: {
×
140
    params: z.object({
×
141
      baseId: z.string(),
×
142
    }),
×
143
    body: {
×
144
      content: {
×
145
        'application/json': {
×
146
          schema: tableRoSchema,
×
147
        },
×
148
      },
×
149
    },
×
150
  },
×
151
  responses: {
×
152
    201: {
×
153
      description: 'Returns data about a table.',
×
154
      content: {
×
155
        'application/json': {
×
156
          schema: tableFullVoSchema,
×
157
        },
×
158
      },
×
159
    },
×
160
  },
×
161
  tags: ['table'],
×
162
});
×
163

×
164
export const createTable = async (baseId: string, tableRo: ICreateTableRo = {}) => {
×
165
  return axios.post<ITableFullVo>(urlBuilder(CREATE_TABLE, { baseId }), tableRo);
×
166
};
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc