@firecms/schema_inference 3.0.0-canary.25 → 3.0.0-canary.250

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,743 @@
1
+ export type CMSType =
2
+ | string
3
+ | number
4
+ | boolean
5
+ | Date
6
+ | GeoPoint
7
+ | EntityReference
8
+ | Record<string, any>
9
+ | CMSType[];
10
+
11
+ /**
12
+ * @group Entity properties
13
+ */
14
+ export type DataType<T extends CMSType = CMSType> =
15
+ T extends string ? "string" :
16
+ T extends number ? "number" :
17
+ T extends boolean ? "boolean" :
18
+ T extends Date ? "date" :
19
+ T extends GeoPoint ? "geopoint" :
20
+ T extends Vector ? "vector" :
21
+ T extends EntityReference ? "reference" :
22
+ T extends Array<any> ? "array" :
23
+ T extends Record<string, any> ? "map" : never;
24
+
25
+ /**
26
+ * New or existing status
27
+ * @group Models
28
+ */
29
+ export type EntityStatus = "new" | "existing" | "copy";
30
+
31
+ /**
32
+ * This type represents a record of key value pairs as described in an
33
+ * entity collection.
34
+ * @group Models
35
+ */
36
+ export type EntityValues<M extends object> = M;
37
+
38
+ /**
39
+ * Class used to create a reference to an entity in a different path
40
+ */
41
+ export class EntityReference {
42
+ /**
43
+ * ID of the entity
44
+ */
45
+ readonly id: string;
46
+ /**
47
+ * A string representing the path of the referenced document (relative
48
+ * to the root of the database).
49
+ */
50
+ readonly path: string;
51
+
52
+ constructor(id: string, path: string) {
53
+ this.id = id;
54
+ this.path = path;
55
+ }
56
+
57
+ get pathWithId() {
58
+ return `${this.path}/${this.id}`;
59
+ }
60
+
61
+ isEntityReference() {
62
+ return true;
63
+ }
64
+ }
65
+
66
+ export class GeoPoint {
67
+
68
+ /**
69
+ * The latitude of this GeoPoint instance.
70
+ */
71
+ readonly latitude: number;
72
+ /**
73
+ * The longitude of this GeoPoint instance.
74
+ */
75
+ readonly longitude: number;
76
+
77
+ constructor(latitude: number, longitude: number) {
78
+ this.latitude = latitude;
79
+ this.longitude = longitude;
80
+ }
81
+ }
82
+
83
+ export class Vector {
84
+ readonly value: number[];
85
+
86
+ constructor(value: number[]) {
87
+ this.value = value;
88
+ }
89
+ }
90
+
91
+ /**
92
+ * @group Entity properties
93
+ */
94
+ export type Property<T extends CMSType = any> =
95
+ T extends string ? StringProperty :
96
+ T extends number ? NumberProperty :
97
+ T extends boolean ? BooleanProperty :
98
+ T extends Date ? DateProperty :
99
+ T extends GeoPoint ? GeopointProperty :
100
+ T extends EntityReference ? ReferenceProperty :
101
+ T extends Array<CMSType> ? ArrayProperty<T> :
102
+ T extends Record<string, any> ? MapProperty<T> : any;
103
+
104
+ /**
105
+ * @group Entity properties
106
+ */
107
+ export interface PropertyDisabledConfig {
108
+
109
+ /**
110
+ * Enable this flag if you would like to clear the value of the field
111
+ * when the corresponding property gets disabled.
112
+ *
113
+ * This is useful for keeping data consistency when you have conditional
114
+ * properties.
115
+ */
116
+ clearOnDisabled?: boolean;
117
+
118
+ /**
119
+ * Explanation of why this property is disabled (e.g. a different field
120
+ * needs to be enabled)
121
+ */
122
+ disabledMessage?: string;
123
+
124
+ /**
125
+ * Set this flag to true if you want to hide this field when disabled
126
+ */
127
+ hidden?: boolean;
128
+ }
129
+
130
+ /**
131
+ * We use this type to define mapping between string or number values in
132
+ * the data source to a label (such in a select dropdown).
133
+ * The key in this Record is the value saved in the datasource, and the value in
134
+ * this record is the label displayed in the UI.
135
+ * You can add additional customization by assigning a {@link EnumValueConfig} for the
136
+ * label instead of a simple string (for enabling or disabling options and
137
+ * choosing colors).
138
+ * If you need to ensure the order of the elements use an array of {@link EnumValueConfig}
139
+ * @group Entity properties
140
+ */
141
+ export type EnumValues = EnumValueConfig[]
142
+ | Record<string | number, string | EnumValueConfig>;
143
+
144
+ /**
145
+ * Configuration for a particular entry in an `EnumValues`
146
+ * @group Entity properties
147
+ */
148
+ export type EnumValueConfig = {
149
+ /**
150
+ * Value stored in the data source.
151
+ */
152
+ id: string | number;
153
+ /**
154
+ * Displayed label
155
+ */
156
+ label: string;
157
+ /**
158
+ * This value will not be selectable
159
+ */
160
+ disabled?: boolean;
161
+ }
162
+
163
+ /**
164
+ * Record of properties of an entity or a map property
165
+ * @group Entity properties
166
+ */
167
+ export type Properties<M extends Record<string, any> = any> = {
168
+ [k in keyof M]: Property<M[keyof M]>;
169
+ };
170
+
171
+
172
+ /**
173
+ * Interface including all common properties of a CMS property
174
+ * @group Entity properties
175
+ */
176
+ export interface BaseProperty<T extends CMSType> {
177
+
178
+ /**
179
+ * Datatype of the property
180
+ */
181
+ dataType: DataType;
182
+
183
+ /**
184
+ * Property name (e.g. Product)
185
+ */
186
+ name?: string;
187
+
188
+ /**
189
+ * Property description, always displayed under the field
190
+ */
191
+ description?: string;
192
+
193
+ /**
194
+ * Longer description of a field, displayed under a popover
195
+ */
196
+ longDescription?: string;
197
+
198
+ /**
199
+ * Width in pixels of this column in the collection view. If not set
200
+ * the width is inferred based on the other configurations
201
+ */
202
+ columnWidth?: number;
203
+
204
+ /**
205
+ * Do not show this property in the collection view
206
+ */
207
+ hideFromCollection?: boolean;
208
+
209
+ /**
210
+ * Is this a read only property. When set to true, it gets rendered as a
211
+ * preview.
212
+ */
213
+ readOnly?: boolean;
214
+
215
+ /**
216
+ * Is this field disabled.
217
+ * When set to true, it gets rendered as a
218
+ * disabled field. You can also specify a configuration for defining the
219
+ * behaviour of disabled properties (including custom messages, clear value on
220
+ * disabled or hide the field completely)
221
+ */
222
+ disabled?: boolean | PropertyDisabledConfig;
223
+
224
+ /**
225
+ * Rules for validating this property
226
+ */
227
+ validation?: PropertyValidationSchema;
228
+
229
+ /**
230
+ * This value will be set by default for new entities.
231
+ */
232
+ defaultValue?: T | null;
233
+
234
+ }
235
+
236
+ /**
237
+ * @group Entity properties
238
+ */
239
+ export interface NumberProperty extends BaseProperty<number> {
240
+
241
+ dataType: "number";
242
+
243
+ /**
244
+ * You can use the enum values providing a map of possible
245
+ * exclusive values the property can take, mapped to the label that it is
246
+ * displayed in the dropdown.
247
+ */
248
+ enumValues?: EnumValues;
249
+
250
+ /**
251
+ * Rules for validating this property
252
+ */
253
+ validation?: NumberPropertyValidationSchema,
254
+
255
+ /**
256
+ * Add an icon to clear the value and set it to `null`. Defaults to `false`
257
+ */
258
+ clearable?: boolean;
259
+ }
260
+
261
+ /**
262
+ * @group Entity properties
263
+ */
264
+ export interface BooleanProperty extends BaseProperty<boolean> {
265
+
266
+ dataType: "boolean";
267
+
268
+ /**
269
+ * Rules for validating this property
270
+ */
271
+ validation?: PropertyValidationSchema,
272
+
273
+ }
274
+
275
+ /**
276
+ * @group Entity properties
277
+ */
278
+ export interface StringProperty extends BaseProperty<string> {
279
+
280
+ dataType: "string";
281
+
282
+ /**
283
+ * Is this string property long enough so it should be displayed in
284
+ * a multiple line field. Defaults to false. If set to true,
285
+ * the number of lines adapts to the content
286
+ */
287
+ multiline?: boolean;
288
+
289
+ /**
290
+ * Should this string property be displayed as a markdown field. If true,
291
+ * the field is rendered as a text editors that supports markdown highlight
292
+ * syntax. It also includes a preview of the result.
293
+ */
294
+ markdown?: boolean;
295
+
296
+ /**
297
+ * You can use the enum values providing a map of possible
298
+ * exclusive values the property can take, mapped to the label that it is
299
+ * displayed in the dropdown. You can use a simple object with the format
300
+ * `value` => `label`, or with the format `value` => `EnumValueConfig` if you
301
+ * need extra customization, (like disabling specific options or assigning
302
+ * colors). If you need to ensure the order of the elements, you can pass
303
+ * a `Map` instead of a plain object.
304
+ *
305
+ */
306
+ enumValues?: EnumValues;
307
+
308
+ /**
309
+ * You can specify a `Storage` configuration. It is used to
310
+ * indicate that this string refers to a path in your storage provider.
311
+ */
312
+ storage?: StorageConfig;
313
+
314
+ /**
315
+ * If the value of this property is a URL, you can set this flag to true
316
+ * to add a link, or one of the supported media types to render a preview
317
+ */
318
+ url?: boolean | PreviewType;
319
+
320
+ /**
321
+ * Does this field include an email
322
+ */
323
+ email?: boolean;
324
+
325
+ /**
326
+ * Should this string be rendered as a tag instead of just text.
327
+ */
328
+ previewAsTag?: boolean;
329
+
330
+ /**
331
+ * Rules for validating this property
332
+ */
333
+ validation?: StringPropertyValidationSchema;
334
+
335
+ /**
336
+ * Add an icon to clear the value and set it to `null`. Defaults to `false`
337
+ */
338
+ clearable?: boolean;
339
+
340
+ /**
341
+ * You can use this property (a string) to behave as a reference to another
342
+ * collection. The stored value is the ID of the entity in the
343
+ * collection, and the `path` prop is used to
344
+ * define the collection this reference points to.
345
+ */
346
+ reference?: ReferenceProperty;
347
+ }
348
+
349
+ /**
350
+ * @group Entity properties
351
+ */
352
+ export interface ArrayProperty<T extends ArrayT[] = any[], ArrayT extends CMSType = any> extends BaseProperty<T> {
353
+
354
+ dataType: "array";
355
+
356
+ /**
357
+ * The property of this array.
358
+ * You can specify any property (except another Array property)
359
+ * You can leave this field empty only if you are providing a custom field,
360
+ * or using the `oneOf` prop, otherwise an error will be thrown.
361
+ */
362
+ of?: Property<ArrayT>[];
363
+
364
+ /**
365
+ * Use this field if you would like to have an array of typed objects.
366
+ * It is useful if you need to have values of different types in the same
367
+ * array.
368
+ * Each entry of the array is an object with the shape:
369
+ * ```
370
+ * { type: "YOUR_TYPE", value: "YOUR_VALUE"}
371
+ * ```
372
+ * Note that you can use any property so `value` can take any value (strings,
373
+ * numbers, array, objects...)
374
+ * You can customise the `type` and `value` fields to suit your needs.
375
+ *
376
+ * An example use case for this feature may be a blog entry, where you have
377
+ * images and text blocks using markdown.
378
+ */
379
+ oneOf?: {
380
+ /**
381
+ * Record of properties, where the key is the `type` and the value
382
+ * is the corresponding property
383
+ */
384
+ properties: Properties;
385
+
386
+ /**
387
+ * Order in which the properties are displayed.
388
+ * If you are specifying your collection as code, the order is the same as the
389
+ * one you define in `properties`, and you don't need to specify this prop.
390
+ */
391
+ propertiesOrder?: string[];
392
+
393
+ /**
394
+ * Name of the field to use as the discriminator for type
395
+ * Defaults to `type`
396
+ */
397
+ typeField?: string;
398
+
399
+ /**
400
+ * Name of the field to use as the value
401
+ * Defaults to `value`
402
+ */
403
+ valueField?: string;
404
+ };
405
+
406
+ /**
407
+ * Rules for validating this property
408
+ */
409
+ validation?: ArrayPropertyValidationSchema;
410
+
411
+ /**
412
+ * Should the field be initially expanded. Defaults to `true`
413
+ */
414
+ expanded?: boolean;
415
+
416
+ /**
417
+ * Display the child properties directly, without being wrapped in an
418
+ * extendable panel.
419
+ */
420
+ minimalistView?: boolean;
421
+
422
+ /**
423
+ * Can the elements in this array be reordered. Defaults to `true`.
424
+ * This prop has no effect if `disabled` is set to true.
425
+ */
426
+ sortable?: boolean;
427
+
428
+ /**
429
+ * Can the elements in this array be added. Defaults to `true`
430
+ * This prop has no effect if `disabled` is set to true.
431
+ */
432
+ canAddElements?: boolean;
433
+
434
+ }
435
+
436
+ /**
437
+ * @group Entity properties
438
+ */
439
+ export interface MapProperty<T extends Record<string, CMSType> = Record<string, CMSType>> extends BaseProperty<T> {
440
+
441
+ dataType: "map";
442
+
443
+ /**
444
+ * Record of properties included in this map.
445
+ */
446
+ properties?: Properties<T>;
447
+
448
+ /**
449
+ * Order in which the properties are displayed.
450
+ * If you are specifying your collection as code, the order is the same as the
451
+ * one you define in `properties`, and you don't need to specify this prop.
452
+ */
453
+ propertiesOrder?: Extract<keyof T, string>[];
454
+
455
+ /**
456
+ * Rules for validating this property.
457
+ * NOTE: If you don't set `required` in the map property, an empty object
458
+ * will be considered valid, even if you set `required` in the properties.
459
+ */
460
+ validation?: PropertyValidationSchema,
461
+
462
+ /**
463
+ * Properties that are displayed when rendered as a preview
464
+ */
465
+ previewProperties?: Partial<Extract<keyof T, string>>[];
466
+
467
+ /**
468
+ * Allow the user to add only some keys in this map.
469
+ * By default, all properties of the map have the corresponding field in
470
+ * the form view. Setting this flag to true allows to pick only some.
471
+ * Useful for map that can have a lot of sub-properties that may not be
472
+ * needed
473
+ */
474
+ pickOnlySomeKeys?: boolean;
475
+
476
+ /**
477
+ * Display the child properties as independent columns in the collection
478
+ * view
479
+ */
480
+ spreadChildren?: boolean;
481
+
482
+ /**
483
+ * Display the child properties directly, without being wrapped in an
484
+ * extendable panel. Note that this will also hide the title of this property.
485
+ */
486
+ minimalistView?: boolean;
487
+
488
+ /**
489
+ * Should the field be initially expanded. Defaults to `true`
490
+ */
491
+ expanded?: boolean;
492
+
493
+ /**
494
+ * Render this map as a key-value table that allows to use
495
+ * arbitrary keys. You don't need to define the properties in this case.
496
+ */
497
+ keyValue?: boolean;
498
+
499
+ }
500
+
501
+ /**
502
+ * @group Entity properties
503
+ */
504
+ export interface DateProperty extends BaseProperty<Date> {
505
+
506
+ dataType: "date";
507
+
508
+ /**
509
+ * Set the granularity of the field to a date or date + time.
510
+ * Defaults to `date_time`.
511
+ *
512
+ */
513
+ mode?: "date" | "date_time";
514
+
515
+ /**
516
+ * Rules for validating this property
517
+ */
518
+ validation?: DatePropertyValidationSchema;
519
+
520
+ /**
521
+ * If this flag is set to `on_create` or `on_update` this timestamp is
522
+ * updated automatically on creation of the entity only or on every
523
+ * update (including creation). Useful for creating `created_on` or
524
+ * `updated_on` fields
525
+ */
526
+ autoValue?: "on_create" | "on_update"
527
+
528
+ /**
529
+ * Add an icon to clear the value and set it to `null`. Defaults to `false`
530
+ */
531
+ clearable?: boolean;
532
+ }
533
+
534
+ /**
535
+ * @group Entity properties
536
+ */
537
+ // TODO: currently this is the only unsupported field
538
+ export interface GeopointProperty extends BaseProperty<GeoPoint> {
539
+
540
+ dataType: "geopoint";
541
+
542
+ /**
543
+ * Rules for validating this property
544
+ */
545
+ validation?: PropertyValidationSchema,
546
+
547
+ }
548
+
549
+ /**
550
+ * @group Entity properties
551
+ */
552
+ export interface ReferenceProperty extends BaseProperty<EntityReference> {
553
+
554
+ dataType: "reference";
555
+
556
+ /**
557
+ * Absolute collection path of the collection this reference points to.
558
+ * The collection of the entity is inferred based on the root navigation, so
559
+ * the filters and search delegate existing there are applied to this view
560
+ * as well.
561
+ * You can leave this prop undefined if the path is not yet know, e.g.
562
+ * you are using a property builder and the path depends on a different
563
+ * property.
564
+ * Note that you can also use a collection alias.
565
+ */
566
+ path?: string;
567
+
568
+ /**
569
+ * Properties that need to be rendered when displaying a preview of this
570
+ * reference. If not specified the first 3 are used. Only the first 3
571
+ * specified values are considered.
572
+ */
573
+ previewProperties?: string[];
574
+
575
+ /**
576
+ * Should the reference include the ID of the entity. Defaults to `true`
577
+ */
578
+ includeId?: boolean;
579
+
580
+ /**
581
+ * Should the reference include a link to the entity (open the entity details). Defaults to `true`
582
+ */
583
+ includeEntityLink?: boolean;
584
+
585
+ }
586
+
587
+ export interface PropertyValidationSchema {
588
+ /**
589
+ * Is this field required
590
+ */
591
+ required?: boolean;
592
+
593
+ /**
594
+ * Customize the required message when the property is not set
595
+ */
596
+ requiredMessage?: string;
597
+
598
+ /**
599
+ * If the unique flag is set to `true`, you can only have one entity in the
600
+ * collection with this value.
601
+ */
602
+ unique?: boolean;
603
+
604
+ /**
605
+ * If the uniqueInArray flag is set to `true`, you can only have this value
606
+ * once per entry in the parent `ArrayProperty`. It has no effect if this
607
+ * property is not a child of an `ArrayProperty`. It works on direct
608
+ * children of an `ArrayProperty` or first level children of `MapProperty`
609
+ */
610
+ uniqueInArray?: boolean;
611
+ }
612
+
613
+ /**
614
+ * Validation rules for numbers
615
+ * @group Entity properties
616
+ */
617
+ export interface NumberPropertyValidationSchema extends PropertyValidationSchema {
618
+ min?: number;
619
+ max?: number;
620
+ lessThan?: number;
621
+ moreThan?: number;
622
+ positive?: boolean;
623
+ negative?: boolean;
624
+ integer?: boolean;
625
+ }
626
+
627
+ /**
628
+ * Validation rules for strings
629
+ * @group Entity properties
630
+ */
631
+ export interface StringPropertyValidationSchema extends PropertyValidationSchema {
632
+ length?: number;
633
+ min?: number;
634
+ max?: number;
635
+ matches?: string | RegExp;
636
+ /**
637
+ * Message displayed when the input does not satisfy the regex in `matches`
638
+ */
639
+ matchesMessage?: string;
640
+ trim?: boolean;
641
+ lowercase?: boolean;
642
+ uppercase?: boolean;
643
+ }
644
+
645
+ /**
646
+ * Validation rules for dates
647
+ * @group Entity properties
648
+ */
649
+ export interface DatePropertyValidationSchema extends PropertyValidationSchema {
650
+ min?: Date;
651
+ max?: Date;
652
+ }
653
+
654
+ /**
655
+ * Validation rules for arrays
656
+ * @group Entity properties
657
+ */
658
+ export interface ArrayPropertyValidationSchema extends PropertyValidationSchema {
659
+ min?: number;
660
+ max?: number;
661
+ }
662
+
663
+ /**
664
+ * Additional configuration related to Storage related fields
665
+ * @group Entity properties
666
+ */
667
+ export type StorageConfig = {
668
+
669
+ /**
670
+ * File MIME types that can be uploaded to this reference. Don't specify for
671
+ * all.
672
+ * Note that you can also use the asterisk notation, so `image/*`
673
+ * accepts any image file, and so on.
674
+ */
675
+ acceptedFiles?: FileType[];
676
+
677
+ /**
678
+ * Specific metadata set in your uploaded file.
679
+ * For the default Firebase implementation, the values passed here are of type
680
+ * `firebase.storage.UploadMetadata`
681
+ */
682
+ metadata?: Record<string, unknown>,
683
+
684
+ /**
685
+ * You can use this prop to customize the uploaded filename.
686
+ * You can use a function as a callback or a string where you
687
+ * specify some placeholders that get replaced with the corresponding values.
688
+ * - `{file}` - Full file name
689
+ * - `{file.name}` - Name of the file without extension
690
+ * - `{file.ext}` - Extension of the file
691
+ * - `{rand}` - Random value used to avoid name collisions
692
+ * - `{entityId}` - ID of the entity
693
+ * - `{propertyKey}` - ID of this property
694
+ * - `{path}` - Path of this entity
695
+ *
696
+ * @param context
697
+ */
698
+ fileName?: string;
699
+
700
+ /**
701
+ * Absolute path in your bucket.
702
+ *
703
+ * You can use a function as a callback or a string where you
704
+ * specify some placeholders that get replaced with the corresponding values.
705
+ * - `{file}` - Full file name
706
+ * - `{file.name}` - Name of the file without extension
707
+ * - `{file.ext}` - Extension of the file
708
+ * - `{rand}` - Random value used to avoid name collisions
709
+ * - `{entityId}` - ID of the entity
710
+ * - `{propertyKey}` - ID of this property
711
+ * - `{path}` - Path of this entity
712
+ */
713
+ storagePath: string;
714
+
715
+ /**
716
+ * When set to true, this flag indicates that the download URL of the file
717
+ * will be saved in the datasource, instead of the storage path.
718
+ *
719
+ * Note that the generated URL may use a token that, if disabled, may
720
+ * make the URL unusable and lose the original reference to Cloud Storage,
721
+ * so it is not encouraged to use this flag.
722
+ *
723
+ * Defaults to false.
724
+ */
725
+ storeUrl?: boolean,
726
+
727
+ /**
728
+ * Define maximal file size in bytes
729
+ */
730
+ maxSize?: number,
731
+
732
+ }
733
+
734
+ export type FileType =
735
+ "image/*"
736
+ | "video/*"
737
+ | "audio/*"
738
+ | "application/*"
739
+ | "text/*"
740
+ | "font/*"
741
+ | string;
742
+
743
+ export type PreviewType = "image" | "video" | "audio" | "file";