-
-
Notifications
You must be signed in to change notification settings - Fork 437
Expand file tree
/
Copy pathprimitive.ts
More file actions
84 lines (74 loc) · 2.65 KB
/
Copy pathprimitive.ts
File metadata and controls
84 lines (74 loc) · 2.65 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
79
80
81
82
83
84
import { SCHEMA_TYPES } from "../../constants.js";
import { MonoSchemaParser } from "../mono-schema-parser.js";
export class PrimitiveSchemaParser extends MonoSchemaParser {
override parse() {
let contentType = null;
const { additionalProperties, type, description, items, readOnly } =
this.schema || {};
const readonly =
(readOnly && this.config.addReadonly) || this.config.immutable;
if (type === this.config.Ts.Keyword.Object && additionalProperties) {
const propertyNamesSchema = this.schemaUtils.getSchemaPropertyNamesSchema(
this.schema,
);
let recordKeysContent: any;
let recordValuesContent: any;
if (propertyNamesSchema) {
recordKeysContent = this.schemaParserFabric
.createSchemaParser({
schema: propertyNamesSchema,
schemaPath: this.schemaPath,
})
.getInlineParseContent();
} else {
recordKeysContent = this.config.Ts.Keyword.String;
}
if (typeof additionalProperties === "object") {
recordValuesContent = this.schemaParserFabric
.createSchemaParser({
schema: additionalProperties,
schemaPath: this.schemaPath,
})
.getInlineParseContent();
} else {
recordValuesContent = this.config.Ts.Keyword.Any;
}
contentType = this.config.Ts.RecordType({
readonly,
key: recordKeysContent,
value: recordValuesContent,
});
}
if (Array.isArray(type) && type.length) {
contentType = this.schemaParser._complexSchemaParsers.oneOf({
...(typeof this.schema === "object" ? this.schema : {}),
oneOf: type.map((type) => ({ type })),
});
}
if (Array.isArray(items) && type === SCHEMA_TYPES.ARRAY) {
contentType = this.config.Ts.Tuple({
readonly,
values: items.map((item) =>
this.schemaParserFabric
.createSchemaParser({ schema: item, schemaPath: this.schemaPath })
.getInlineParseContent(),
),
});
}
return {
...(typeof this.schema === "object" ? this.schema : {}),
$schemaPath: this.schemaPath.slice(),
$parsedSchema: true,
schemaType: SCHEMA_TYPES.PRIMITIVE,
type: SCHEMA_TYPES.PRIMITIVE,
typeIdentifier: this.config.Ts.Keyword.Type,
name: this.typeName,
description: this.schemaFormatters.formatDescription(description),
// TODO: probably it should be refactored. `type === 'null'` is not flexible
content:
type === this.config.Ts.Keyword.Null
? type
: contentType || this.schemaUtils.getSchemaType(this.schema),
};
}
}