|
1 | 1 | import { |
2 | | - GbnfJsonSchema, isGbnfJsonArraySchema, isGbnfJsonBasicSchemaIncludesType, isGbnfJsonConstSchema, |
3 | | - isGbnfJsonEnumSchema, isGbnfJsonObjectSchema, isGbnfJsonOneOfSchema |
| 2 | + GbnfJsonSchema, isGbnfJsonArraySchema, isGbnfJsonBasicSchemaIncludesType, isGbnfJsonBasicStringSchema, isGbnfJsonConstSchema, |
| 3 | + isGbnfJsonEnumSchema, isGbnfJsonFormatStringSchema, isGbnfJsonObjectSchema, isGbnfJsonOneOfSchema |
4 | 4 | } from "./gbnfJson/types.js"; |
5 | 5 |
|
6 | 6 | const maxTypeRepetition = 10; |
@@ -31,11 +31,50 @@ export function getTypeScriptTypeStringForGbnfJsonSchema(schema: GbnfJsonSchema) |
31 | 31 |
|
32 | 32 | const mapping = keyText + ": " + valueType; |
33 | 33 |
|
34 | | - if (propSchema.description != null && propSchema.description !== "") { |
| 34 | + const description: string[] = (propSchema.description != null && propSchema.description !== "") |
| 35 | + ? [propSchema.description] |
| 36 | + : []; |
| 37 | + const propInfo: string[] = []; |
| 38 | + |
| 39 | + if (isGbnfJsonBasicStringSchema(propSchema)) { |
| 40 | + if (propSchema.minLength != null && propSchema.minLength > 0) |
| 41 | + propInfo.push("minimum length: " + String(Math.floor(propSchema.minLength))); |
| 42 | + |
| 43 | + if (propSchema.maxLength != null) |
| 44 | + propInfo.push("maximum length: " + String(Math.floor(Math.max(propSchema.maxLength, propSchema.minLength ?? 0, 0)))); |
| 45 | + } else if (isGbnfJsonFormatStringSchema(propSchema)) { |
| 46 | + if (propSchema.format === "date-time") |
| 47 | + propInfo.push("format: ISO 8601 date-time"); |
| 48 | + else |
| 49 | + propInfo.push("format: " + String(propSchema.format)); |
| 50 | + } else if (isGbnfJsonArraySchema(propSchema)) { |
| 51 | + if (propSchema.minItems != null && propSchema.minItems > maxTypeRepetition) |
| 52 | + propInfo.push("minimum items: " + String(Math.floor(propSchema.minItems))); |
| 53 | + |
| 54 | + if (propSchema.maxItems != null) |
| 55 | + propInfo.push("maximum items: " + String(Math.floor(Math.max(propSchema.maxItems, propSchema.minItems ?? 0, 0)))); |
| 56 | + } else if (isGbnfJsonObjectSchema(propSchema)) { |
| 57 | + if (propSchema.minProperties != null && propSchema.minProperties > 0) |
| 58 | + propInfo.push("minimum number of properties: " + String(Math.floor(propSchema.minProperties))); |
| 59 | + |
| 60 | + if (propSchema.maxProperties != null) |
| 61 | + propInfo.push( |
| 62 | + "maximum number of properties: " + |
| 63 | + String(Math.floor(Math.max(propSchema.maxProperties, propSchema.minProperties ?? 0, 0))) |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + if (propInfo.length > 0) |
| 68 | + description.push(propInfo.join(", ")); |
| 69 | + |
| 70 | + if (description.length > 0) { |
35 | 71 | addNewline = true; |
36 | 72 | return [ |
37 | 73 | "\n", |
38 | | - "// ", propSchema.description.split("\n").join("\n// "), |
| 74 | + "// ", description |
| 75 | + .join("\n") |
| 76 | + .split("\n") |
| 77 | + .join("\n// "), |
39 | 78 | "\n", |
40 | 79 | mapping |
41 | 80 | ].join(""); |
|
0 commit comments