@firecms/schema_inference 3.0.0-canary.24 → 3.0.0-canary.241

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