-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschemas.ts
More file actions
181 lines (149 loc) · 4.92 KB
/
schemas.ts
File metadata and controls
181 lines (149 loc) · 4.92 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { localizeText } from 'hds-lib';
type localizableText = { en: string; fr?: string; es?: string };
// --- Type definitions ---
interface SelectOption {
value: string | number;
label: localizableText;
}
interface ConverterEngine {
key: string;
version: string;
models: string;
}
interface BaseItemData {
type: 'checkbox' | 'date' | 'text' | 'number' | 'select' | 'composite' | 'datasource-search' | 'convertible' | 'slider';
label: localizableText;
description?: localizableText;
canBeNull?: boolean;
streamId?: string;
eventType?: string;
'converter-engine'?: ConverterEngine;
}
interface CheckboxData extends BaseItemData {
type: 'checkbox';
}
interface DateData extends BaseItemData {
type: 'date';
}
interface TextData extends BaseItemData {
type: 'text';
}
interface NumberData extends BaseItemData {
type: 'number';
}
interface SelectData extends BaseItemData {
type: 'select';
options: SelectOption[];
}
interface CompositeData extends BaseItemData {
type: 'composite';
composite: Record<string, ItemData>;
}
interface DatasourceSearchData extends BaseItemData {
type: 'datasource-search';
datasource: string;
}
interface ConvertibleData extends BaseItemData {
type: 'convertible';
'converter-engine': ConverterEngine;
}
/** A per-value tick-label entry on a slider. Value keyed by the raw numeric value. */
export interface SliderLabel {
label: localizableText;
description?: localizableText;
}
/** Display-layer knobs on a slider — affect how the raw value is shown to the user only. */
export interface SliderDisplay {
/** Multiplier applied to the raw value for display. Default 1. E.g. 100 for a 0..1 raw → 0..100 displayed. */
multiplier?: number;
/** Decimal places shown. Default: 0 if multiplier >= 10, else 2. */
precision?: number;
/** Appended to the displayed value (e.g. '%'). */
suffix?: localizableText;
}
interface SliderData extends BaseItemData {
type: 'slider';
/** Lower bound in the raw (stored) scale. */
min: number;
/** Upper bound in the raw scale. */
max: number;
/** Step increment in the raw scale. Default 1. */
step?: number;
slider?: {
orientation?: 'horizontal' | 'vertical';
labels?: Record<string | number, SliderLabel>;
display?: SliderDisplay;
};
}
export type ItemData = CheckboxData | DateData | TextData | NumberData | SelectData | CompositeData | DatasourceSearchData | ConvertibleData | SliderData;
export interface JSONSchema {
title: string;
description?: string;
type?: 'boolean' | 'string' | 'number' | 'object';
format?: string;
dateSaveFormat?: string;
minLength?: number;
oneOf?: Array<{ const: string | number; title: string }>;
properties?: Record<string, JSONSchema>;
required?: string[];
}
const l = localizeText;
type SchemaAction = (schema: JSONSchema, v: ItemData) => void;
const SCHEMAS_PER_TYPE: Record<string, SchemaAction> = { checkbox, date, text, number, select, composite, 'datasource-search': datasourceSearch, convertible, slider };
export function schemaFor (v: ItemData): JSONSchema {
const schema: JSONSchema = {
title: l(v.label) || ''
};
if (v.description != null) {
schema.description = l(v.description) || undefined;
}
const action = SCHEMAS_PER_TYPE[v.type];
if (action == null) {
throw new Error(`Cannot find schema for type: "${v.type}"`);
}
action(schema, v);
return schema;
}
// -------- schemas per types ------------ //
function checkbox (schema: JSONSchema, _v: ItemData): void {
schema.type = 'boolean';
}
function date (schema: JSONSchema, _v: ItemData): void {
schema.type = 'string';
schema.format = 'date';
schema.dateSaveFormat = 'YYYY-MM-DD';
}
function text (schema: JSONSchema, v: ItemData): void {
schema.type = 'string';
if (v.canBeNull !== true) schema.minLength = 1;
}
function number (schema: JSONSchema, _v: ItemData): void {
schema.type = 'number';
}
function select (schema: JSONSchema, v: ItemData): void {
const selectData = v as SelectData;
const foundNaN = selectData.options.find(option => isNaN(option.value as number));
const options = selectData.options.map((option) => ({ const: option.value, title: l(option.label) || '' }));
schema.type = foundNaN ? 'string' : 'number';
schema.oneOf = options;
}
function datasourceSearch (schema: JSONSchema, _v: ItemData): void {
schema.type = 'object';
}
function composite (schema: JSONSchema, v: ItemData): void {
const compositeData = v as CompositeData;
schema.type = 'object';
schema.properties = {};
schema.required = [];
for (const [key, value] of Object.entries(compositeData.composite)) {
schema.properties[key] = schemaFor(value);
if (value.canBeNull !== true) schema.required.push(key);
}
}
function convertible (schema: JSONSchema, _v: ItemData): void {
schema.type = 'object';
}
function slider (schema: JSONSchema, _v: ItemData): void {
// Slider is just a numeric input at the storage layer — display is UI-only.
schema.type = 'number';
}