|
| 1 | +import {Model} from "@tsed/mongoose"; |
| 2 | +import {CollectionOf, Default, Description, Enum, enums, getJsonSchema, MaxLength, MinLength, Required} from "@tsed/schema"; |
| 3 | +import {TestMongooseContext} from "@tsed/testing-mongoose"; |
| 4 | + |
| 5 | +export enum ComponentStatuses { |
| 6 | + UNDER_MAINTENANCE = "UNDER_MAINTENANCE", |
| 7 | + DEGRADED_PERFORMANCE = "DEGRADED_PERFORMANCE", |
| 8 | + PARTIAL_OUTAGE = "PARTIAL_OUTAGE", |
| 9 | + MAJOR_OUTAGE = "MAJOR_OUTAGE" |
| 10 | +} |
| 11 | + |
| 12 | +enums(ComponentStatuses).label("ComponentStatuses"); |
| 13 | + |
| 14 | +export class ResponseTimeThreshold { |
| 15 | + @Enum(ComponentStatuses) |
| 16 | + @Required() |
| 17 | + @Default(ComponentStatuses.MAJOR_OUTAGE) |
| 18 | + status: ComponentStatuses = ComponentStatuses.MAJOR_OUTAGE; |
| 19 | +} |
| 20 | + |
| 21 | +@Model({ |
| 22 | + name: "components-statuses-settings" |
| 23 | +}) |
| 24 | +export class ComponentStatusSettings { |
| 25 | + @Required() |
| 26 | + @Description("The settings name") |
| 27 | + @MinLength(3) |
| 28 | + @MaxLength(100) |
| 29 | + name: string; |
| 30 | + |
| 31 | + @CollectionOf(ResponseTimeThreshold) |
| 32 | + responseTimes: ResponseTimeThreshold[]; |
| 33 | +} |
| 34 | + |
| 35 | +describe("Enums integration", () => { |
| 36 | + beforeEach(TestMongooseContext.create); |
| 37 | + afterEach(TestMongooseContext.clearDatabase); |
| 38 | + afterEach(TestMongooseContext.reset); |
| 39 | + |
| 40 | + it("should not fail when the enum is used with enums() utils", () => { |
| 41 | + expect(getJsonSchema(ComponentStatusSettings)).toEqual({ |
| 42 | + definitions: { |
| 43 | + ComponentStatuses: { |
| 44 | + enum: ["UNDER_MAINTENANCE", "DEGRADED_PERFORMANCE", "PARTIAL_OUTAGE", "MAJOR_OUTAGE"], |
| 45 | + type: "string" |
| 46 | + }, |
| 47 | + ResponseTimeThreshold: { |
| 48 | + properties: { |
| 49 | + status: { |
| 50 | + $ref: "#/definitions/ComponentStatuses", |
| 51 | + minLength: 1 |
| 52 | + } |
| 53 | + }, |
| 54 | + required: ["status"], |
| 55 | + type: "object" |
| 56 | + } |
| 57 | + }, |
| 58 | + properties: { |
| 59 | + name: { |
| 60 | + description: "The settings name", |
| 61 | + maxLength: 100, |
| 62 | + minLength: 3, |
| 63 | + type: "string" |
| 64 | + }, |
| 65 | + responseTimes: { |
| 66 | + items: { |
| 67 | + $ref: "#/definitions/ResponseTimeThreshold" |
| 68 | + }, |
| 69 | + type: "array" |
| 70 | + } |
| 71 | + }, |
| 72 | + required: ["name"], |
| 73 | + type: "object" |
| 74 | + }); |
| 75 | + }); |
| 76 | +}); |
0 commit comments