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