-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdataSchemaField.ts
More file actions
78 lines (72 loc) · 2.25 KB
/
dataSchemaField.ts
File metadata and controls
78 lines (72 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { StringDict } from "@/parsing/index.js";
export class DataSchemaField {
/**
* Display name for the field, also impacts inference results.
*/
public title: string;
/**
* Name of the field in the data schema.
*/
public name: string;
/**
* Whether this field can contain multiple values.
*/
public isArray: boolean;
/**
* Data type of the field.
*/
public type: string;
/**
* Allowed values when type is `classification`. Leave empty for other types.
*/
public classificationValues?: Array<string>;
/**
* Whether to remove duplicate values in the array.
* Only applicable if `is_array` is True.
*/
public uniqueValues?: boolean;
/**
* Detailed description of what this field represents.
*/
public description?: string;
/**
* Optional extraction guidelines.
*/
public guidelines?: string;
/**
* Subfields when type is `nested_object`. Leave empty for other types.
*/
public nestedFields?: StringDict;
constructor(fields: StringDict) {
this.name = fields["name"];
this.title = fields["title"];
this.isArray = fields["is_array"];
this.type = fields["type"];
this.classificationValues = fields["classification_values"];
this.uniqueValues = fields["unique_values"];
this.description = fields["description"];
this.guidelines = fields["guidelines"];
this.nestedFields = fields["nested_fields"];
}
toJSON() {
const out: Record<string, unknown> = {
name: this.name,
title: this.title,
// eslint-disable-next-line @typescript-eslint/naming-convention,camelcase
is_array: this.isArray,
type: this.type,
};
// eslint-disable-next-line camelcase
if (this.classificationValues !== undefined) out.classification_values = this.classificationValues;
// eslint-disable-next-line camelcase
if (this.uniqueValues !== undefined) out.unique_values = this.uniqueValues;
if (this.description !== undefined) out.description = this.description;
if (this.guidelines !== undefined) out.guidelines = this.guidelines;
// eslint-disable-next-line camelcase
if (this.nestedFields !== undefined) out.nested_fields = this.nestedFields;
return out;
}
toString() {
return JSON.stringify(this.toJSON());
}
}