Skip to content

Commit 6337c84

Browse files
committed
feat: Update input create default values to match those in deepnote app
Signed-off-by: Tomas Kislan <[email protected]>
1 parent 4c1c4e2 commit 6337c84

File tree

2 files changed

+81
-121
lines changed

2 files changed

+81
-121
lines changed

src/notebooks/deepnote/converters/inputConverters.unit.test.ts

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ suite('InputTextBlockConverter', () => {
6565
assert.strictEqual(cell.languageId, 'json');
6666

6767
const parsed = JSON.parse(cell.value);
68-
assert.isNull(parsed.deepnote_input_label);
69-
assert.isNull(parsed.deepnote_variable_name);
70-
assert.isNull(parsed.deepnote_variable_value);
68+
assert.strictEqual(parsed.deepnote_input_label, '');
69+
assert.strictEqual(parsed.deepnote_variable_name, '');
70+
assert.strictEqual(parsed.deepnote_variable_value, '');
7171
assert.isNull(parsed.deepnote_variable_default_value);
7272
});
7373

@@ -223,9 +223,9 @@ suite('InputTextareaBlockConverter', () => {
223223
const cell = converter.convertToCell(block);
224224

225225
const parsed = JSON.parse(cell.value);
226-
assert.isNull(parsed.deepnote_variable_name);
227-
assert.isNull(parsed.deepnote_variable_value);
228-
assert.isNull(parsed.deepnote_input_label);
226+
assert.strictEqual(parsed.deepnote_variable_name, '');
227+
assert.strictEqual(parsed.deepnote_variable_value, '');
228+
assert.strictEqual(parsed.deepnote_input_label, '');
229229
});
230230
});
231231

@@ -371,8 +371,8 @@ suite('InputSelectBlockConverter', () => {
371371
const cell = converter.convertToCell(block);
372372

373373
const parsed = JSON.parse(cell.value);
374-
assert.isNull(parsed.deepnote_variable_name);
375-
assert.isNull(parsed.deepnote_variable_value);
374+
assert.strictEqual(parsed.deepnote_variable_name, '');
375+
assert.strictEqual(parsed.deepnote_variable_value, 'Option 1');
376376
});
377377
});
378378

@@ -513,9 +513,9 @@ suite('InputSliderBlockConverter', () => {
513513
const cell = converter.convertToCell(block);
514514

515515
const parsed = JSON.parse(cell.value);
516-
assert.isNull(parsed.deepnote_variable_name);
517-
assert.isNull(parsed.deepnote_slider_min_value);
518-
assert.isNull(parsed.deepnote_slider_max_value);
516+
assert.strictEqual(parsed.deepnote_variable_name, '');
517+
assert.strictEqual(parsed.deepnote_slider_min_value, 0);
518+
assert.strictEqual(parsed.deepnote_slider_max_value, 10);
519519
});
520520
});
521521

@@ -561,7 +561,8 @@ suite('InputSliderBlockConverter', () => {
561561

562562
converter.applyChangesToBlock(block, cell);
563563

564-
assert.strictEqual(block.metadata?.deepnote_variable_value, 42);
564+
// Numeric values fail string schema validation, so stored in raw content
565+
assert.strictEqual(block.metadata?.[DEEPNOTE_VSCODE_RAW_CONTENT_KEY], cellValue);
565566
});
566567

567568
test('handles invalid JSON', () => {
@@ -645,8 +646,8 @@ suite('InputCheckboxBlockConverter', () => {
645646
const cell = converter.convertToCell(block);
646647

647648
const parsed = JSON.parse(cell.value);
648-
assert.isNull(parsed.deepnote_variable_name);
649-
assert.isNull(parsed.deepnote_variable_value);
649+
assert.strictEqual(parsed.deepnote_variable_name, '');
650+
assert.strictEqual(parsed.deepnote_variable_value, false);
650651
});
651652
});
652653

@@ -755,8 +756,9 @@ suite('InputDateBlockConverter', () => {
755756
const cell = converter.convertToCell(block);
756757

757758
const parsed = JSON.parse(cell.value);
758-
assert.isNull(parsed.deepnote_variable_name);
759-
assert.isNull(parsed.deepnote_variable_value);
759+
assert.strictEqual(parsed.deepnote_variable_name, '');
760+
// Default value should be an ISO date string
761+
assert.match(parsed.deepnote_variable_value, /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/);
760762
});
761763
});
762764

@@ -865,8 +867,8 @@ suite('InputDateRangeBlockConverter', () => {
865867
const cell = converter.convertToCell(block);
866868

867869
const parsed = JSON.parse(cell.value);
868-
assert.isNull(parsed.deepnote_variable_name);
869-
assert.isNull(parsed.deepnote_variable_value);
870+
assert.strictEqual(parsed.deepnote_variable_name, '');
871+
assert.strictEqual(parsed.deepnote_variable_value, '');
870872
});
871873
});
872874

@@ -973,7 +975,7 @@ suite('InputFileBlockConverter', () => {
973975
const cell = converter.convertToCell(block);
974976

975977
const parsed = JSON.parse(cell.value);
976-
assert.isNull(parsed.deepnote_variable_name);
978+
assert.strictEqual(parsed.deepnote_variable_name, '');
977979
assert.isNull(parsed.deepnote_allowed_file_extensions);
978980
});
979981
});
@@ -1085,8 +1087,8 @@ suite('ButtonBlockConverter', () => {
10851087
const cell = converter.convertToCell(block);
10861088

10871089
const parsed = JSON.parse(cell.value);
1088-
assert.isNull(parsed.deepnote_button_title);
1089-
assert.isNull(parsed.deepnote_button_behavior);
1090+
assert.strictEqual(parsed.deepnote_button_title, 'Run');
1091+
assert.strictEqual(parsed.deepnote_button_behavior, 'set_variable');
10901092
});
10911093
});
10921094

src/notebooks/deepnote/deepnoteSchemas.ts

Lines changed: 58 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -43,214 +43,172 @@ export const DeepnoteBigNumberMetadataSchema = z.object({
4343
.transform((val) => val ?? false)
4444
});
4545

46-
export const DeepnoteTextInputMetadataSchema = z.object({
46+
// Base schema with common fields for all input types
47+
const DeepnoteBaseInputMetadataSchema = z.object({
4748
deepnote_variable_name: z
4849
.string()
4950
.nullish()
50-
.transform((val) => val ?? null),
51-
deepnote_variable_value: z
52-
.string()
53-
.nullish()
54-
.transform((val) => val ?? null),
55-
deepnote_variable_default_value: z
56-
.string()
57-
.nullish()
58-
.transform((val) => val ?? null),
51+
.transform((val) => val ?? '')
52+
});
53+
54+
// Extended base schema with label (used by most input types)
55+
const DeepnoteBaseInputWithLabelMetadataSchema = DeepnoteBaseInputMetadataSchema.extend({
5956
deepnote_input_label: z
6057
.string()
6158
.nullish()
62-
.transform((val) => val ?? null)
59+
.transform((val) => val ?? '')
6360
});
6461

65-
export const DeepnoteTextareaInputMetadataSchema = z.object({
66-
deepnote_variable_name: z
67-
.string()
68-
.nullish()
69-
.transform((val) => val ?? null),
62+
export const DeepnoteTextInputMetadataSchema = DeepnoteBaseInputWithLabelMetadataSchema.extend({
7063
deepnote_variable_value: z
7164
.string()
7265
.nullish()
73-
.transform((val) => val ?? null),
74-
deepnote_input_label: z
66+
.transform((val) => val ?? ''),
67+
deepnote_variable_default_value: z
7568
.string()
7669
.nullish()
7770
.transform((val) => val ?? null)
7871
});
7972

80-
export const DeepnoteSelectInputMetadataSchema = z.object({
81-
deepnote_input_label: z
73+
export const DeepnoteTextareaInputMetadataSchema = DeepnoteBaseInputWithLabelMetadataSchema.extend({
74+
deepnote_variable_value: z
8275
.string()
8376
.nullish()
84-
.transform((val) => val ?? null),
85-
deepnote_variable_name: z
77+
.transform((val) => val ?? ''),
78+
deepnote_variable_default_value: z
8679
.string()
8780
.nullish()
88-
.transform((val) => val ?? null),
81+
.transform((val) => val ?? '')
82+
});
83+
84+
export const DEEPNOTE_SELECT_INPUT_DEFAULT_OPTIONS = ['Option 1', 'Option 2'] as const;
85+
86+
export const DeepnoteSelectInputMetadataSchema = DeepnoteBaseInputWithLabelMetadataSchema.extend({
8987
deepnote_variable_value: z
88+
.union([z.string(), z.array(z.string())])
89+
.nullish()
90+
.transform((val) => val ?? DEEPNOTE_SELECT_INPUT_DEFAULT_OPTIONS[0]),
91+
deepnote_variable_default_value: z
9092
.union([z.string(), z.array(z.string())])
9193
.nullish()
9294
.transform((val) => val ?? null),
9395
deepnote_variable_options: z
9496
.array(z.string())
9597
.nullish()
96-
.transform((val) => val ?? null),
98+
.transform((val) => val ?? DEEPNOTE_SELECT_INPUT_DEFAULT_OPTIONS),
9799
deepnote_variable_custom_options: z
98100
.array(z.string())
99101
.nullish()
100-
.transform((val) => val ?? null),
102+
.transform((val) => val ?? DEEPNOTE_SELECT_INPUT_DEFAULT_OPTIONS),
101103
deepnote_variable_select_type: z
102-
// .enum(['from-options', 'from-variable'])
103-
.string()
104+
.enum(['from-options', 'from-variable'])
105+
// .string()
104106
.nullish()
105107
.transform((val) => val ?? null),
106108
deepnote_allow_multiple_values: z
107109
.boolean()
108110
.nullish()
109-
.transform((val) => val ?? null),
111+
.transform((val) => val ?? false),
110112
deepnote_allow_empty_values: z
111113
.boolean()
112114
.nullish()
113-
.transform((val) => val ?? null),
114-
deepnote_variable_default_value: z
115-
.union([z.string(), z.array(z.string())])
116-
.nullish()
117-
.transform((val) => val ?? null),
115+
.transform((val) => val ?? false),
118116
deepnote_variable_selected_variable: z
119117
.string()
120118
.nullish()
121-
.transform((val) => val ?? null)
119+
.transform((val) => val ?? '')
122120
});
123121

124-
export const DeepnoteSliderInputMetadataSchema = z.object({
125-
deepnote_input_label: z
126-
.string()
127-
.nullish()
128-
.transform((val) => val ?? null),
129-
deepnote_variable_name: z
130-
.string()
131-
.nullish()
132-
.transform((val) => val ?? null),
122+
export const DeepnoteSliderInputMetadataSchema = DeepnoteBaseInputWithLabelMetadataSchema.extend({
133123
deepnote_variable_value: z
134124
.string()
135125
.nullish()
136-
.transform((val) => val ?? null),
126+
.transform((val) => val ?? '5'),
137127
deepnote_slider_min_value: z
138128
.number()
139129
.nullish()
140-
.transform((val) => val ?? null),
130+
.transform((val) => val ?? 0),
141131
deepnote_slider_max_value: z
142132
.number()
143133
.nullish()
144-
.transform((val) => val ?? null),
134+
.transform((val) => val ?? 10),
145135
deepnote_slider_step: z
146136
.number()
147137
.nullish()
148-
.transform((val) => val ?? null),
138+
.transform((val) => val ?? 1),
149139
deepnote_variable_default_value: z
150140
.string()
151141
.nullish()
152142
.transform((val) => val ?? null)
153143
});
154144

155-
export const DeepnoteCheckboxInputMetadataSchema = z.object({
156-
deepnote_input_label: z
157-
.string()
158-
.nullish()
159-
.transform((val) => val ?? null),
160-
deepnote_variable_name: z
161-
.string()
162-
.nullish()
163-
.transform((val) => val ?? null),
145+
export const DeepnoteCheckboxInputMetadataSchema = DeepnoteBaseInputWithLabelMetadataSchema.extend({
164146
deepnote_variable_value: z
165147
.boolean()
166148
.nullish()
167-
.transform((val) => val ?? null),
149+
.transform((val) => val ?? false),
168150
deepnote_variable_default_value: z
169151
.boolean()
170152
.nullish()
171153
.transform((val) => val ?? null)
172154
});
173155

174-
export const DeepnoteDateInputMetadataSchema = z.object({
175-
deepnote_input_label: z
176-
.string()
177-
.nullish()
178-
.transform((val) => val ?? null),
179-
deepnote_variable_name: z
180-
.string()
181-
.nullish()
182-
.transform((val) => val ?? null),
156+
export function getStartOfDayDate(): Date {
157+
const d = new Date();
158+
d.setHours(0, 0, 0, 0);
159+
return d;
160+
}
161+
162+
export const DeepnoteDateInputMetadataSchema = DeepnoteBaseInputWithLabelMetadataSchema.extend({
183163
deepnote_variable_value: z
184164
.string()
185165
.nullish()
186-
.transform((val) => val ?? null),
166+
.transform((val) => val ?? getStartOfDayDate().toISOString()),
187167
deepnote_variable_default_value: z
188168
.string()
189169
.nullish()
190170
.transform((val) => val ?? null),
191171
deepnote_input_date_version: z
192172
.number()
193173
.nullish()
194-
.transform((val) => val ?? null)
174+
.transform((val) => val ?? 2)
195175
});
196176

197-
export const DeepnoteDateRangeInputMetadataSchema = z.object({
198-
deepnote_input_label: z
199-
.string()
200-
.nullish()
201-
.transform((val) => val ?? null),
202-
deepnote_variable_name: z
203-
.string()
204-
.nullish()
205-
.transform((val) => val ?? null),
177+
export const DeepnoteDateRangeInputMetadataSchema = DeepnoteBaseInputWithLabelMetadataSchema.extend({
206178
deepnote_variable_value: z
207179
.union([z.string(), z.tuple([z.string(), z.string()])])
208180
.nullish()
209-
.transform((val) => val ?? null),
181+
.transform((val) => val ?? ''),
210182
deepnote_variable_default_value: z
211183
.union([z.string(), z.tuple([z.string(), z.string()])])
212184
.nullish()
213185
.transform((val) => val ?? null)
214186
});
215187

216-
export const DeepnoteFileInputMetadataSchema = z.object({
217-
deepnote_input_label: z
218-
.string()
219-
.nullish()
220-
.transform((val) => val ?? null),
221-
deepnote_variable_name: z
222-
.string()
223-
.nullish()
224-
.transform((val) => val ?? null),
188+
export const DeepnoteFileInputMetadataSchema = DeepnoteBaseInputWithLabelMetadataSchema.extend({
225189
deepnote_variable_value: z
226190
.string()
227191
.nullish()
228-
.transform((val) => val ?? null),
192+
.transform((val) => val ?? ''),
229193
deepnote_allowed_file_extensions: z
230194
.string()
231195
.nullish()
232196
.transform((val) => val ?? null)
233197
});
234198

235-
export const DeepnoteButtonMetadataSchema = z.object({
199+
export const DeepnoteButtonMetadataSchema = DeepnoteBaseInputMetadataSchema.extend({
236200
deepnote_button_title: z
237201
.string()
238202
.nullish()
239-
.transform((val) => val ?? null),
240-
deepnote_variable_name: z
241-
.string()
242-
.nullish()
243-
.transform((val) => val ?? null),
203+
.transform((val) => val ?? 'Run'),
244204
deepnote_button_behavior: z
245-
// .enum(['run', 'set_variable'])
246-
.string()
205+
.enum(['run', 'set_variable'])
247206
.nullish()
248-
.transform((val) => val ?? null),
207+
.transform((val) => val ?? 'set_variable'),
249208
deepnote_button_color_scheme: z
250-
// .enum(['blue', 'red', 'neutral', 'green', 'yellow'])
251-
.string()
209+
.enum(['blue', 'red', 'neutral', 'green', 'yellow'])
252210
.nullish()
253-
.transform((val) => val ?? null)
211+
.transform((val) => val ?? 'blue')
254212
});
255213

256214
export type DeepnoteChartBigNumberOutput = z.infer<typeof DeepnoteChartBigNumberOutputSchema>;

0 commit comments

Comments
 (0)