|
| 1 | +import { |
| 2 | + ApiResponse, |
| 3 | + ApiResponseHttpsexamplecomschemasuser, |
| 4 | + ApiResponseHttpsexamplecomschemasmetadata, |
| 5 | + ApiResponseHttpsexamplecomschemasemail |
| 6 | +} from "./expected"; |
| 7 | + |
| 8 | + |
| 9 | +// Valid: full API response with all fields |
| 10 | +const fullResponse: ApiResponse = { |
| 11 | + data: { |
| 12 | + id: 123, |
| 13 | + name: "John Doe", |
| 14 | + email: "john@example.com" |
| 15 | + }, |
| 16 | + meta: { |
| 17 | + timestamp: "2024-01-15T10:30:00Z", |
| 18 | + version: 1 |
| 19 | + } |
| 20 | +}; |
| 21 | + |
| 22 | +// Valid: minimal response (required fields only) |
| 23 | +const minimalResponse: ApiResponse = { |
| 24 | + data: { |
| 25 | + id: 1, |
| 26 | + name: "Jane" |
| 27 | + }, |
| 28 | + meta: {} |
| 29 | +}; |
| 30 | + |
| 31 | +// Valid: user object directly |
| 32 | +const user: ApiResponseHttpsexamplecomschemasuser = { |
| 33 | + id: 42, |
| 34 | + name: "Test User" |
| 35 | +}; |
| 36 | + |
| 37 | +// Valid: user with email |
| 38 | +const userWithEmail: ApiResponseHttpsexamplecomschemasuser = { |
| 39 | + id: 42, |
| 40 | + name: "Test User", |
| 41 | + email: "test@example.com" |
| 42 | +}; |
| 43 | + |
| 44 | +// Valid: metadata object |
| 45 | +const metadata: ApiResponseHttpsexamplecomschemasmetadata = { |
| 46 | + timestamp: "2024-01-15", |
| 47 | + version: 2 |
| 48 | +}; |
| 49 | + |
| 50 | +// Valid: email is just a string |
| 51 | +const email: ApiResponseHttpsexamplecomschemasemail = "user@domain.com"; |
| 52 | + |
| 53 | +// Invalid: missing required field 'data' |
| 54 | +// @ts-expect-error - data is required |
| 55 | +const missingData: ApiResponse = { |
| 56 | + meta: {} |
| 57 | +}; |
| 58 | + |
| 59 | +// Invalid: missing required field 'meta' |
| 60 | +// @ts-expect-error - meta is required |
| 61 | +const missingMeta: ApiResponse = { |
| 62 | + data: { id: 1, name: "Test" } |
| 63 | +}; |
| 64 | + |
| 65 | +// Invalid: user missing required 'id' |
| 66 | +const userMissingId: ApiResponse = { |
| 67 | + // @ts-expect-error - id is required on user |
| 68 | + data: { |
| 69 | + name: "Test" |
| 70 | + }, |
| 71 | + meta: {} |
| 72 | +}; |
| 73 | + |
| 74 | +// Invalid: user missing required 'name' |
| 75 | +const userMissingName: ApiResponse = { |
| 76 | + // @ts-expect-error - name is required on user |
| 77 | + data: { |
| 78 | + id: 1 |
| 79 | + }, |
| 80 | + meta: {} |
| 81 | +}; |
| 82 | + |
| 83 | +// Invalid: wrong type for user id |
| 84 | +const wrongIdType: ApiResponse = { |
| 85 | + data: { |
| 86 | + // @ts-expect-error - id must be number |
| 87 | + id: "not-a-number", |
| 88 | + name: "Test" |
| 89 | + }, |
| 90 | + meta: {} |
| 91 | +}; |
| 92 | + |
| 93 | +// Invalid: extra property on user (additionalProperties: false) |
| 94 | +const extraUserProp: ApiResponse = { |
| 95 | + data: { |
| 96 | + id: 1, |
| 97 | + name: "Test", |
| 98 | + // @ts-expect-error - extra property not allowed |
| 99 | + extra: "not allowed" |
| 100 | + }, |
| 101 | + meta: {} |
| 102 | +}; |
0 commit comments