forked from foundryvtt/pf2e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fields.d.ts
1109 lines (945 loc) · 44.1 KB
/
fields.d.ts
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import type * as abstract from "../abstract/module.d.ts";
import type Color from "../utils/color.d.ts";
import type { TombstoneDataSchema } from "./data.d.ts";
import type { DataModelValidationFailure } from "./validation-failure.d.ts";
/* ---------------------------------------- */
/* Abstract Data Field */
/* ---------------------------------------- */
/**
* @typedef DataFieldOptions
* @property [required=false] Is this field required to be populated?
* @property [nullable=false] Can this field have null values?
* @property [initial] The initial value of a field, or a function which assigns that initial value.
* @property [validate] A data validation function which accepts one argument with the current value.
* @property [choices] An array of values or an object of values/labels which represent allowed choices for the
* field. A function may be provided which dynamically returns the array of choices.
* @property [label] A localizable label displayed on forms which render this field.
* @property [hint] Localizable help text displayed on forms which render this field.
* @property [validationError] A custom validation error string. When displayed will be prepended with the document
* name, field name, and candidate value.
*/
export interface DataFieldOptions<
TSourceProp,
TRequired extends boolean,
TNullable extends boolean,
THasInitial extends boolean,
> {
required?: TRequired;
nullable?: TNullable;
initial?: THasInitial extends true
?
| TSourceProp
| ((data: Record<string, unknown>) => MaybeSchemaProp<TSourceProp, TRequired, TNullable, THasInitial>)
| null
: THasInitial extends false
? undefined
:
| TSourceProp
| ((data: Record<string, unknown>) => MaybeSchemaProp<TSourceProp, TRequired, TNullable, THasInitial>)
| null
| undefined;
validate?: (value: unknown) => DataModelValidationFailure | boolean | void;
choices?:
| readonly TSourceProp[]
| Record<string, string>
| (() => readonly TSourceProp[] | Record<string | number, string>);
readonly?: boolean;
label?: string;
hint?: string;
validationError?: string;
}
/**
* @typedef DataFieldValidationOptions
* @property [partial] Whether this is a partial schema validation, or a complete one.
* @property [fallback] Whether to allow replacing invalid values with valid fallbacks.
* @property [source] The full source object being evaluated.
* @property [dropInvalidEmbedded] If true, invalid embedded documents will emit a warning and be placed in
* the invalidDocuments collection rather than causing the parent to be
* considered invalid.
*/
interface DataFieldValidationOptions {
partial?: boolean;
fallback?: boolean;
source?: object;
dropInvalidEmbedded?: boolean;
}
/**
* An abstract class that defines the base pattern for a data field within a data schema.
*
* @property name The name of this data field within the schema that contains it
* @property [required=false] Is this field required to be populated?
* @property [nullable=false] Can this field have null values?
* @property initial The initial value of a field, or a function which assigns that initial value.
* @property validate A data validation function which accepts one argument with the current value.
* @property [readonly=false] Should the prepared value of the field be read-only, preventing it from being
* changed unless a change to the _source data is applied.
* @property {string} label A localizable label displayed on forms which render this field.
* @property {string} hint Localizable help text displayed on forms which render this field.
* @property {string} validationError A custom validation error string. When displayed will be prepended with the
* document name, field name, and candidate value.
*/
export abstract class DataField<
TSourceProp extends JSONValue = JSONValue,
TModelProp = TSourceProp,
TRequired extends boolean = boolean,
TNullable extends boolean = boolean,
THasInitial extends boolean = boolean,
> implements DataFieldOptions<TSourceProp, TRequired, TNullable, THasInitial>
{
/** @param options Options which configure the behavior of the field */
constructor(options?: DataFieldOptions<TSourceProp, TRequired, TNullable, THasInitial>);
initial: this["options"]["initial"];
/** The initially provided options which configure the data field */
options: DataFieldOptions<TSourceProp, TRequired, TNullable, THasInitial>;
/**
* The field name of this DataField instance.
* This is assigned by `SchemaField#initialize`.
* @internal
*/
name: string;
/**
* A reference to the parent schema to which this DataField belongs.
* This is assigned by `SchemaField#initialize`.
* @internal
*/
parent: DataSchema | undefined;
/** Whether this field defines part of a Document/Embedded Document hierarchy. */
static hierarchical: boolean;
/**
* Does this field type contain other fields in a recursive structure?
* Examples of recursive fields are SchemaField, ArrayField, or TypeDataField
* Examples of non-recursive fields are StringField, NumberField, or ObjectField
*/
static recursive: boolean;
/** Default parameters for this field type */
protected static get _defaults(): DataFieldOptions<unknown, boolean, boolean, boolean>;
/** A dot-separated string representation of the field path within the parent schema. */
get fieldPath(): string;
/**
* Apply a function to this DataField which propagates through recursively to any contained data schema.
* @param fn The function to apply
* @param value The current value of this field
* @param [options={}] Additional options passed to the applied function
* @returns The results object
*/
apply(
fn: string | ((field: this, value?: unknown, options?: Record<string, unknown>) => unknown),
value?: unknown,
options?: Record<string, unknown>,
): unknown;
/* -------------------------------------------- */
/* Field Cleaning */
/* -------------------------------------------- */
/**
* Coerce source data to ensure that it conforms to the correct data type for the field.
* Data coercion operations should be simple and synchronous as these are applied whenever a DataModel is constructed.
* For one-off cleaning of user-provided input the sanitize method should be used.
* @param value The initial value
* @param [options] Additional options for how the field is cleaned
* @param [options.partial] Whether to perform partial cleaning?
* @param [options.source] The root data model being cleaned
* @returns The cast value
*/
clean(value: unknown, options?: CleanFieldOptions): MaybeSchemaProp<TSourceProp, TRequired, TNullable, THasInitial>;
/**
* Apply any cleaning logic specific to this DataField type.
* @param value The appropriately coerced value.
* @param [options] Additional options for how the field is cleaned.
* @returns The cleaned value.
*/
protected _cleanType(value: unknown, options?: CleanFieldOptions): unknown;
/**
* Cast a non-default value to ensure it is the correct type for the field
* @param value The provided non-default value
* @returns The standardized value
*/
protected abstract _cast(value?: unknown): unknown;
/**
* Attempt to retrieve a valid initial value for the DataField.
* @param data The source data object for which an initial value is required
* @returns A valid initial value
* @throws An error if there is no valid initial value defined
*/
getInitialValue(data?: object): MaybeSchemaProp<TSourceProp, TRequired, TNullable, THasInitial>;
/* -------------------------------------------- */
/* Field Validation */
/* -------------------------------------------- */
/**
* Validate a candidate input for this field, ensuring it meets the field requirements.
* A validation failure can be provided as a raised Error (with a string message), by returning false, or by returning
* a DataModelValidationFailure instance.
* A validator which returns true denotes that the result is certainly valid and further validations are unnecessary.
* @param value The initial value
* @param [options={}] Options which affect validation behavior
* @returns Returns a DataModelValidationFailure if a validation failure
* occurred.
*/
validate(value: unknown, options?: DataFieldValidationOptions): DataModelValidationFailure | void;
/**
* Special validation rules which supersede regular field validation.
* This validator screens for certain values which are otherwise incompatible with this field like null or undefined.
* @param value The candidate value
* @returns A boolean to indicate with certainty whether the value is valid.
* Otherwise, return void.
* @throws May throw a specific error if the value is not valid
*/
protected _validateSpecial(value: unknown): boolean | void;
/**
* A default type-specific validator that can be overridden by child classes
* @param value The candidate value
* @param [options={}] Options which affect validation behavior
* @returns A boolean to indicate with certainty whether the value is valid, or specific DataModelValidationFailure
* information, otherwise void.
* @throws May throw a specific error if the value is not valid
*/
protected _validateType(
value: unknown,
options?: DataFieldValidationOptions,
): boolean | DataModelValidationFailure | void;
/**
* Certain fields may declare joint data validation criteria.
* This method will only be called if the field is designated as recursive.
* @param data Candidate data for joint model validation
* @param options Options which modify joint model validation
* @throws An error if joint model validation fails
* @internal
*/
_validateModel(data: TSourceProp, options?: Record<string, unknown>): void;
/* -------------------------------------------- */
/* Initialization and Serialization */
/* -------------------------------------------- */
/**
* Initialize the original source data into a mutable copy for the DataModel instance.
* @param value The source value of the field
* @param model The DataModel instance that this field belongs to
* @param [options] Initialization options
*/
initialize(
value: unknown,
model?: ConstructorOf<abstract.DataModel>,
options?: object,
): MaybeSchemaProp<TModelProp, TRequired, TNullable, THasInitial>;
/**
* Export the current value of the field into a serializable object.
* @param value The initialized value of the field
* @returns An exported representation of the field
*/
toObject(value: TModelProp): MaybeSchemaProp<TSourceProp, TRequired, TNullable, THasInitial>;
/**
* Recursively traverse a schema and retrieve a field specification by a given path
* @param path The field path as an array of strings
*/
protected _getField(path: string[]): this | undefined;
}
/* -------------------------------------------- */
/* Data Schema Field */
/* -------------------------------------------- */
export type DataSchema = Record<string, DataField<JSONValue, unknown, boolean>>;
/** A special class of {@link DataField} which defines a data schema. */
export class SchemaField<
TDataSchema extends DataSchema,
TSourceProp extends SourceFromSchema<TDataSchema> = SourceFromSchema<TDataSchema>,
TModelProp extends NonNullable<JSONValue> = ModelPropsFromSchema<TDataSchema>,
TRequired extends boolean = true,
TNullable extends boolean = false,
THasInitial extends boolean = true,
> extends DataField<TSourceProp, TModelProp, TRequired, TNullable, THasInitial> {
/**
* @param fields The contained field definitions
* @param options Options which configure the behavior of the field
*/
constructor(fields: TDataSchema, options?: DataFieldOptions<TSourceProp, TRequired, TNullable, THasInitial>);
protected static override get _defaults(): DataFieldOptions<object, boolean, boolean, boolean>;
/** The contained field definitions. */
fields: TDataSchema;
/**
* Initialize and validate the structure of the provided field definitions.
* @param fields The provided field definitions
* @returns The validated schema
*/
protected _initialize(fields: DataSchema): DataSchema;
/* -------------------------------------------- */
/* Schema Iteration */
/* -------------------------------------------- */
/** Iterate over a SchemaField by iterating over its fields. */
[Symbol.iterator](): Generator<DataField<TSourceProp, TRequired, TNullable, THasInitial>>;
/** An array of field names which are present in the schema. */
keys(): string[];
/** An array of DataField instances which are present in the schema. */
values(): DataField[];
/** An array of [name, DataField] tuples which define the schema. */
entries(): [string, DataField][];
/**
* Test whether a certain field name belongs to this schema definition.
* @param fieldName The field name
* @returns Does the named field exist in this schema?
*/
has(fieldName: string): boolean;
/**
* Get a DataField instance from the schema by name
* @param fieldName The field name
* @returns The DataField instance or undefined
*/
get(fieldName: string): DataField | undefined;
/* -------------------------------------------- */
/* Data Field Methods */
/* -------------------------------------------- */
protected override _cast(value: unknown): MaybeSchemaProp<TSourceProp, TRequired, TNullable, THasInitial>;
protected override _cleanType(
data: object,
options?: CleanFieldOptions,
): MaybeSchemaProp<TSourceProp, TRequired, TNullable, THasInitial>;
override initialize(
value: unknown,
model?: ConstructorOf<abstract.DataModel>,
options?: Record<string, unknown>,
): MaybeSchemaProp<TModelProp, TRequired, TNullable, THasInitial>;
protected override _validateType(
data: object,
options?: Record<string, unknown>,
): boolean | DataModelValidationFailure | void;
override toObject(value: TModelProp): MaybeSchemaProp<TSourceProp, TRequired, TNullable, THasInitial>;
override apply(
fn: string | ((field: this, value?: unknown, options?: Record<string, unknown>) => unknown),
data?: object,
options?: Record<string, unknown>,
): unknown;
}
export interface CleanFieldOptions {
partial?: boolean;
source?: object;
}
/* -------------------------------------------- */
/* Basic Field Types */
/* -------------------------------------------- */
type BooleanFieldOptions<
TSourceProp extends boolean,
TRequired extends boolean = true,
TNullable extends boolean = false,
THasInitial extends boolean = true,
> = Omit<DataFieldOptions<TSourceProp, TRequired, TNullable, THasInitial>, "choices">;
/** A subclass of [DataField]{@link DataField} which deals with boolean-typed data. */
export class BooleanField<
TSourceProp extends boolean = boolean,
TModelProp extends NonNullable<JSONValue> = TSourceProp,
TRequired extends boolean = true,
TNullable extends boolean = false,
THasInitial extends boolean = true,
> extends DataField<TSourceProp, TModelProp, TRequired, TNullable, THasInitial> {
protected static override get _defaults(): BooleanFieldOptions<boolean, boolean, boolean, boolean>;
protected override _cast(value: unknown): unknown;
protected override _validateType(value: unknown): value is boolean;
}
interface NumberFieldOptions<
TSourceProp extends number,
TRequired extends boolean,
TNullable extends boolean,
THasInitial extends boolean,
> extends DataFieldOptions<TSourceProp, TRequired, TNullable, THasInitial> {
/** A minimum allowed value */
min?: number;
/** A maximum allowed value */
max?: number;
/** A permitted step size */
step?: number;
/** Must the number be an integer? */
integer?: boolean;
/** Must the number be positive? */
positive?: boolean;
}
/** A subclass of [DataField]{@link DataField} which deals with number-typed data. */
export class NumberField<
TSourceProp extends number = number,
TModelProp extends NonNullable<JSONValue> = TSourceProp,
TRequired extends boolean = false,
TNullable extends boolean = true,
THasInitial extends boolean = true,
>
extends DataField<TSourceProp, TModelProp, TRequired, TNullable, THasInitial>
implements NumberFieldOptions<TSourceProp, TRequired, TNullable, THasInitial>
{
/** @param options Options which configure the behavior of the field */
constructor(options?: NumberFieldOptions<TSourceProp, TRequired, TNullable, THasInitial>);
protected static override get _defaults(): NumberFieldOptions<number, boolean, boolean, boolean>;
protected override _cast(value: unknown): unknown;
protected override _cleanType(
value: unknown,
options?: CleanFieldOptions,
): MaybeSchemaProp<TSourceProp, TRequired, TNullable, THasInitial>;
protected override _validateType(value: unknown): void;
}
interface StringFieldOptions<
TSourceProp extends string,
TRequired extends boolean,
TNullable extends boolean,
THasInitial extends boolean,
> extends DataFieldOptions<TSourceProp, TRequired, TNullable, THasInitial> {
choices?:
| readonly TSourceProp[]
| Record<TSourceProp, string>
| (() => readonly TSourceProp[] | Record<TSourceProp, string>);
/** [blank=true] Is the string allowed to be blank (empty)? */
blank?: boolean;
/** [trim=true] Should any provided string be trimmed as part of cleaning? */
trim?: boolean;
}
/** A subclass of `DataField` which deals with string-typed data. */
export class StringField<
TSourceProp extends string = string,
TModelProp extends NonNullable<JSONValue> = TSourceProp,
TRequired extends boolean = false,
TNullable extends boolean = false,
THasInitial extends boolean = true,
>
extends DataField<TSourceProp, TModelProp, TRequired, TNullable, THasInitial>
implements StringFieldOptions<TSourceProp, TRequired, TNullable, THasInitial>
{
/** @param options Options which configure the behavior of the field */
constructor(options?: StringFieldOptions<TSourceProp, TRequired, TNullable, THasInitial>);
protected static override get _defaults(): StringFieldOptions<string, boolean, boolean, boolean>;
override clean(
value: unknown,
options: CleanFieldOptions,
): MaybeSchemaProp<TSourceProp, TRequired, TNullable, THasInitial>;
protected override _cast(value: unknown): unknown;
protected override _validateSpecial(value: unknown): boolean | void;
protected _validateType(value: unknown): boolean | void;
}
type ObjectFieldOptions<
TSourceProp extends object,
TRequired extends boolean,
TNullable extends boolean,
THasInitial extends boolean,
> = DataFieldOptions<TSourceProp, TRequired, TNullable, THasInitial>;
/** A subclass of `DataField` which deals with object-typed data. */
export class ObjectField<
TSourceProp extends object,
TModelProp extends object = TSourceProp,
TRequired extends boolean = true,
TNullable extends boolean = false,
THasInitial extends boolean = true,
>
extends DataField<TSourceProp, TModelProp, TRequired, TNullable, THasInitial>
implements Omit<ObjectFieldOptions<TSourceProp, TRequired, TNullable, THasInitial>, "initial">
{
protected static override get _defaults(): ObjectFieldOptions<object, boolean, boolean, boolean>;
protected override _cast(value: unknown): unknown;
override initialize(
value: unknown,
model?: ConstructorOf<abstract.DataModel>,
options?: ObjectFieldOptions<TSourceProp, TRequired, TNullable, THasInitial>,
): MaybeSchemaProp<TModelProp, TRequired, TNullable, THasInitial>;
override toObject(value: TModelProp): MaybeSchemaProp<TSourceProp, TRequired, TNullable, THasInitial>;
protected override _validateType(
value: unknown,
options?: DataFieldValidationOptions,
): DataModelValidationFailure | boolean | void;
}
type ArrayFieldOptions<
TSourceProp extends unknown[],
TRequired extends boolean,
TNullable extends boolean,
THasInitial extends boolean,
> = DataFieldOptions<TSourceProp, TRequired, TNullable, THasInitial>;
/** A subclass of `DataField` which deals with array-typed data. */
export class ArrayField<
TElementField extends DataField,
TSourceProp extends Partial<
SourcePropFromDataField<TElementField>
>[] = SourcePropFromDataField<TElementField>[],
TModelProp extends object = ModelPropFromDataField<TElementField>[],
TRequired extends boolean = false,
TNullable extends boolean = false,
THasInitial extends boolean = true,
>
extends DataField<TSourceProp, TModelProp, TRequired, TNullable, THasInitial>
implements ArrayFieldOptions<TSourceProp, TRequired, TNullable, THasInitial>
{
/**
* @param element A DataField instance which defines the type of element contained in the Array.
* @param options Options which configure the behavior of the field
*/
constructor(element: TElementField, options?: ArrayFieldOptions<TSourceProp, TRequired, TNullable, THasInitial>);
/** The data type of each element in this array */
element: TElementField;
/**
* Validate the contained element type of the ArrayField
* @param element The type of Array element
* @returns The validated element type
* @throws An error if the element is not a valid type
*/
protected static _validateElementType(element: unknown): unknown;
override _validateModel(changes: TSourceProp, options?: Record<string, unknown>): void;
protected static override get _defaults(): ArrayFieldOptions<unknown[], boolean, boolean, boolean>;
protected override _cast(value: unknown): unknown;
protected _cleanType(value: unknown, options?: CleanFieldOptions): unknown;
protected override _validateType(value: unknown, options?: Record<string, unknown>): void;
/**
* Validate every element of the ArrayField
* @param value The array to validate
* @param options Validation options
* @returns An array of element-specific errors
*/
protected _validateElements(value: unknown[], options?: Record<string, unknown>): DataModelValidationFailure | void;
override initialize(
value: JSONValue,
model: ConstructorOf<abstract.DataModel>,
options: ArrayFieldOptions<TSourceProp, TRequired, TNullable, THasInitial>,
): MaybeSchemaProp<TModelProp, TRequired, TNullable, THasInitial>;
override toObject(value: TModelProp): MaybeSchemaProp<TSourceProp, TRequired, TNullable, THasInitial>;
override apply(
fn: string | ((field: this, value?: unknown, options?: Record<string, unknown>) => unknown),
data?: object,
options?: Record<string, unknown>,
): unknown;
}
export interface ArrayField<
TElementField extends DataField,
TSourceProp extends Partial<SourcePropFromDataField<TElementField>>[] = SourcePropFromDataField<TElementField>[],
TModelProp extends object = ModelPropFromDataField<TElementField>[],
TRequired extends boolean = false,
TNullable extends boolean = false,
THasInitial extends boolean = true,
> extends DataField<TSourceProp, TModelProp, TRequired, TNullable, THasInitial> {
clean(value: unknown, options?: CleanFieldOptions): MaybeSchemaProp<TSourceProp, TRequired, TNullable, THasInitial>;
}
/**
* A subclass of `ArrayField` which supports a set of contained elements.
* Elements in this set are treated as fungible and may be represented in any order or discarded if invalid.
*/
export class SetField<
TElementField extends DataField,
TSourceProp extends SourcePropFromDataField<TElementField>[] = SourcePropFromDataField<TElementField>[],
TModelProp extends Set<ModelPropFromDataField<TElementField>> = Set<ModelPropFromDataField<TElementField>>,
TRequired extends boolean = false,
TNullable extends boolean = false,
THasInitial extends boolean = true,
> extends ArrayField<TElementField, TSourceProp, TModelProp, TRequired, TNullable, THasInitial> {
protected override _validateElements(
value: unknown[],
options?: Record<string, unknown>,
): DataModelValidationFailure | void;
override initialize(
value: TSourceProp,
model: ConstructorOf<abstract.DataModel>,
): MaybeSchemaProp<TModelProp, TRequired, TNullable, THasInitial>;
override toObject(value: TModelProp): TSourceProp;
}
/** A subclass of `SchemaField` which embeds some other DataModel definition as an inner object. */
export class EmbeddedDataField<
TModelProp extends abstract.DataModel = abstract.DataModel,
TRequired extends boolean = true,
TNullable extends boolean = false,
THasInitial extends boolean = true,
> extends SchemaField<
TModelProp["schema"]["fields"],
SourceFromSchema<TModelProp["schema"]["fields"]>,
TModelProp,
TRequired,
TNullable,
THasInitial
> {
/**
* @param model The class of DataModel which should be embedded in this field
* @param options Options which configure the behavior of the field
*/
constructor(
model: ConstructorOf<TModelProp>,
options?: ObjectFieldOptions<
SourceFromSchema<TModelProp["schema"]["fields"]>,
TRequired,
TNullable,
THasInitial
>,
);
/** The embedded DataModel definition which is contained in this field. */
model: ConstructorOf<TModelProp>;
protected override _initialize(fields: DataSchema): DataSchema;
override initialize(
value: MaybeSchemaProp<TModelProp["_source"], TRequired, TNullable, THasInitial>,
model: ConstructorOf<abstract.DataModel>,
options?: object,
): MaybeSchemaProp<TModelProp, TRequired, TNullable, THasInitial>;
override toObject(
value: TModelProp,
): MaybeSchemaProp<SourceFromSchema<TModelProp["schema"]["fields"]>, TRequired, TNullable, THasInitial>;
}
/** A subclass of {@link EmbeddedDataField} which supports a single embedded Document. */
export class EmbeddedDocumentField<
TModelProp extends abstract.Document,
TRequired extends boolean = true,
TNullable extends boolean = true,
THasInitial extends boolean = true,
> extends EmbeddedDataField<TModelProp, TRequired, TNullable, THasInitial> {
/**
* @param model The type of Document which is embedded.
* @param options Options which configure the behavior of the field.
*/
constructor(
model: ConstructorOf<TModelProp>,
options?: DataFieldOptions<TModelProp["_source"], TRequired, TNullable, THasInitial>,
);
static override get _defaults(): DataFieldOptions<object, boolean, true, boolean>;
static override hierarchical: boolean;
override initialize(
value: MaybeSchemaProp<TModelProp["_source"], TRequired, TNullable, THasInitial>,
model: ConstructorOf<TModelProp>,
options?: Record<string, unknown>,
): MaybeSchemaProp<TModelProp, TRequired, TNullable, THasInitial>;
/* -------------------------------------------- */
/* Embedded Document Operations */
/* -------------------------------------------- */
/**
* Return the embedded document(s) as a Collection.
* @param parent The parent document.
*/
getCollection(parent: abstract.Document): Collection<TModelProp>;
}
/**
* A subclass of `ArrayField` which supports an embedded Document collection.
* Invalid elements will be dropped from the collection during validation rather than failing for the field entirely.
*/
export class EmbeddedCollectionField<
TDocument extends abstract.Document<abstract.Document>,
TSourceProp extends object[] = SourceFromDocument<TDocument>[],
TRequired extends boolean = true,
TNullable extends boolean = false,
THasInitial extends boolean = true,
> extends ArrayField<
TDocument["schema"],
TSourceProp,
abstract.EmbeddedCollection<TDocument>,
TRequired,
TNullable,
THasInitial
> {
/**
* @param element The type of Document which belongs to this embedded collection
* @param options Options which configure the behavior of the field
*/
constructor(
element: ConstructorOf<Document>,
options?: ArrayFieldOptions<TSourceProp, TRequired, TNullable, THasInitial>,
);
static override _validateElementType(element: unknown): Document;
/** A reference to the DataModel subclass of the embedded document element */
get model(): ConstructorOf<Document>;
/** The DataSchema of the contained Document model. */
get schema(): TDocument["schema"];
protected override _cleanType(
value: unknown,
options?: CleanFieldOptions,
): MaybeSchemaProp<TSourceProp, TRequired, TNullable, THasInitial>;
protected override _validateElements(
value: unknown[],
options?: Record<string, unknown>,
): DataModelValidationFailure | void;
override initialize(
_value: unknown,
model: ConstructorOf<abstract.DataModel>,
): MaybeSchemaProp<abstract.EmbeddedCollection<TDocument>, TRequired, TNullable, THasInitial>;
override toObject(
value: abstract.EmbeddedCollection<TDocument>,
): MaybeSchemaProp<TSourceProp, TRequired, TNullable, THasInitial>;
override apply(
fn: string | ((field: this, value?: unknown, options?: Record<string, unknown>) => unknown),
data?: object,
options?: Record<string, unknown>,
): unknown;
}
/**
* A subclass of {@link EmbeddedCollectionField} which manages a collection of delta objects relative to another
* collection.
* @todo: fill in
*/
export class EmbeddedCollectionDeltaField<
TDocument extends abstract.Document<abstract.Document>,
TSource extends (SourceFromDocument<TDocument> | SourceFromSchema<TombstoneDataSchema>)[] = (
| SourceFromDocument<TDocument>
| SourceFromSchema<TombstoneDataSchema>
)[],
TRequired extends boolean = true,
TNullable extends boolean = false,
THasInitial extends boolean = true,
> extends EmbeddedCollectionField<TDocument, TSource, TRequired, TNullable, THasInitial> {}
/* -------------------------------------------- */
/* Special Field Types */
/* -------------------------------------------- */
/**
* A subclass of [StringField]{@link StringField} which provides the primary _id for a Document.
* The field may be initially null, but it must be non-null when it is saved to the database.
*/
export class DocumentIdField<
TModelProp extends string | abstract.Document = string,
TRequired extends boolean = true,
TNullable extends boolean = true,
THasInitial extends boolean = true,
> extends StringField<string, TModelProp, TRequired, TNullable, THasInitial> {
protected static override get _defaults(): StringFieldOptions<string, boolean, boolean, boolean>;
protected override _cast(value: unknown): string;
protected override _validateType(value: unknown): boolean;
}
/**
* A special class of [StringField]{@link StringField} field which references another DataModel by its id.
* This field may also be null to indicate that no foreign model is linked.
*/
export class ForeignDocumentField<
TModelProp extends string | abstract.Document = abstract.Document,
TRequired extends boolean = true,
TNullable extends boolean = true,
THasInitial extends boolean = true,
> extends DocumentIdField<TModelProp, TRequired, TNullable, THasInitial> {
/**
* @param model The foreign DataModel class definition which this field should link to.
* @param options Options which configure the behavior of the field
*/
constructor(
model: ConstructorOf<abstract.DataModel>,
options?: StringFieldOptions<string, TRequired, TNullable, THasInitial>,
);
/** A reference to the model class which is stored in this field */
model: abstract.DataModel;
protected static override get _defaults(): StringFieldOptions<string, boolean, boolean, boolean>;
_cast(value: unknown): string;
override initialize(
value: string,
model: ConstructorOf<abstract.DataModel>,
): MaybeSchemaProp<TModelProp, TRequired, TNullable, THasInitial>;
toObject(value: TModelProp): MaybeSchemaProp<string, TRequired, TNullable, THasInitial>;
}
/** A special `StringField` which records a standardized CSS color string. */
export class ColorField<
TRequired extends boolean = false,
TNullable extends boolean = true,
THasInitial extends boolean = true,
> extends StringField<HexColorString, Color, TRequired, TNullable, THasInitial> {
protected static override get _defaults(): StringFieldOptions<HexColorString, boolean, boolean, boolean>;
protected override _validateType(value: unknown): boolean;
}
/**
* @typedef FilePathFieldOptions
* @property [categories] A set of categories in CONST.FILE_CATEGORIES which this field supports
* @property [base64=false] Is embedded base64 data supported in lieu of a file path?
*/
interface FilePathFieldOptions<
TSourceProp extends FilePath,
TRequired extends boolean,
TNullable extends boolean,
THasInitial extends boolean,
> extends StringFieldOptions<TSourceProp, TRequired, TNullable, THasInitial> {
categories?: FileCategory[];
base64?: boolean;
}
/** A special `StringField` which records a file path or inline base64 data. */
export class FilePathField<
TSourceProp extends FilePath = FilePath,
TModelProp extends NonNullable<JSONValue> = TSourceProp,
TRequired extends boolean = false,
TNullable extends boolean = true,
THasInitial extends boolean = true,
> extends StringField<TSourceProp, TModelProp, TRequired, TNullable, THasInitial> {
/** @param options Options which configure the behavior of the field */
constructor(options?: FilePathFieldOptions<TSourceProp, TRequired, TNullable, THasInitial>);
protected static override get _defaults(): FilePathFieldOptions<FilePath, boolean, boolean, boolean>;
protected override _validateType(value: unknown): void;
}
interface AngleFieldOptions<
TSourceProp extends number,
TRequired extends boolean,
TNullable extends boolean,
THasInitial extends boolean,
> extends NumberFieldOptions<TSourceProp, TRequired, TNullable, THasInitial> {
min?: number;
max?: number;
step?: number;
integer?: boolean;
base?: 360 | 0;
}
/**
* A special `NumberField` which represents an angle of rotation in degrees between 0 and 360.
* @property base Whether the base angle should be treated as 360 or as 0
*/
export class AngleField<
TRequired extends boolean = true,
TNullable extends boolean = false,
THasInitial extends boolean = true,
> extends NumberField<number, number, TRequired, TNullable, THasInitial> {
protected static override get _defaults(): AngleFieldOptions<number, boolean, boolean, boolean>;
protected override _cast(value: unknown): number;
}
/** A special `NumberField` represents a number between 0 and 1. */
export class AlphaField<
TRequired extends boolean = true,
TNullable extends boolean = false,
THasInitial extends boolean = true,
> extends NumberField<number, number, TRequired, TNullable, THasInitial> {
protected static get _defaults(): NumberFieldOptions<number, boolean, boolean, boolean>;
}
/** A special `ObjectField` which captures a mapping of User IDs to Document permission levels. */
export class DocumentOwnershipField extends ObjectField<{ [K in string]?: DocumentOwnershipLevel }> {
protected static override get _defaults(): ObjectFieldOptions<
Record<string, DocumentOwnershipLevel | undefined>,
true,
false,
true
>;
protected override _validateType(value: object): boolean | void;
}
/** A special [StringField]{@link StringField} which contains serialized JSON data. */
export class JSONField<
TModelProp extends NonNullable<JSONValue> = object,
TRequired extends boolean = false,
TNullable extends boolean = false,
THasInitial extends boolean = false,
> extends StringField<string, TModelProp, TRequired, TNullable, THasInitial> {
protected static override get _defaults(): StringFieldOptions<string, boolean, boolean, boolean>;
override clean(
value: unknown,
options?: CleanFieldOptions,
): MaybeSchemaProp<string, TRequired, TNullable, THasInitial>;
protected override _validateType(value: unknown): boolean;
override initialize(value: string): MaybeSchemaProp<TModelProp, TRequired, TNullable, THasInitial>;
toObject(value: TModelProp): MaybeSchemaProp<string, TRequired, TNullable, THasInitial>;
}
/**
* A subclass of [StringField]{@link StringField} which contains a sanitized HTML string.
* This class does not override any StringField behaviors, but is used by the server-side to identify fields which
* require sanitization of user input.
*/
export class HTMLField<
TSourceProp extends string = string,
TModelProp extends NonNullable<JSONValue> = TSourceProp,
TRequired extends boolean = true,
TNullable extends boolean = false,
THasInitial extends boolean = true,
> extends StringField<TSourceProp, TModelProp, TRequired, TNullable, THasInitial> {
protected static override get _defaults(): StringFieldOptions<string, boolean, boolean, boolean>;
}
/** A subclass of `NumberField` which is used for storing integer sort keys. */
export class IntegerSortField<
TRequired extends boolean = true,
TNullable extends boolean = false,
THasInitial extends boolean = true,
> extends NumberField<number, number, TRequired, TNullable, THasInitial> {
protected static override get _defaults(): NumberFieldOptions<number, boolean, boolean, boolean>;
}
/** @typedef DocumentStats
* @property systemId The package name of the system the Document was created in.
* @property systemVersion The version of the system the Document was created in.
* @property coreVersion The core version the Document was created in.
* @property createdTime A timestamp of when the Document was created.
* @property modifiedTime A timestamp of when the Document was last modified.
* @property lastModifiedBy The ID of the user who last modified the Document.
*/
/**
* A subclass of {@link SchemaField} which stores document metadata in the _stats field.
* @mixes DocumentStats
*/
export class DocumentStatsField extends SchemaField<DocumentStatsSchema> {
constructor(options?: ObjectFieldOptions<DocumentStatsSchema, true, false, true>);
}
type DocumentStatsSchema = {
systemId: StringField<string, string, true, false, true>;
systemVersion: StringField<string, string, true, false, true>;
coreVersion: StringField<string, string, true, false, true>;
createdTime: NumberField;
modifiedTime: NumberField;
lastModifiedBy: ForeignDocumentField<string>;
};
/** A subclass of `ObjectField` which supports a system-level data object. */
export class TypeDataField<
TSourceProp extends object = object,
TModelProp extends object = TSourceProp,
TDocument extends abstract.Document = abstract.Document,
> extends ObjectField<TSourceProp, TModelProp> {
/**
* @param document The base document class which belongs in this field
* @param options Options which configure the behavior of the field