0

I am building an API with NestJS and typeorm. I need to test the structure of the data that is sent back to the client who perform requests to it.

For example, I could expect the route /players to send some data with a structure that looks like this (TypeScript example):

[
    {
        id: number,
        name: string,
        hasCats: boolean, // optional
        birthday: date-time,
        team: {
            id: number,
            name: string
        }
    },
    ...
]

I found no tool that enables me to check that the data sent back to the client follow this pattern. Hence I have to write my own function. For the pattern, I use the documentation data provided as a JSON by Swagger on the route /api-json.

Is there a tool for testing the structure of some data?

papillon
  • 1,807
  • 2
  • 19
  • 39
  • Can you elaborate? what do you mean? are you trying to validate the structure on the client side? Server side? I guess (from the example) that you use **typescript** and not **javascript**? – Thatkookooguy May 13 '19 at 12:38
  • If you are using **typescript**, do you have an interface for that type? – Thatkookooguy May 13 '19 at 12:40
  • @Thatkookooguy It's an API test, so its code would likely be located on the API, no? I can use both JavaScript or TypeScript. The example I gave is a TypeScript looking draft. – papillon May 13 '19 at 12:40
  • No I don't have an interface for it. How would that help (I have never used interfaces)? – papillon May 13 '19 at 12:41
  • I'm trying to understand what you have since you didn't include too much code. If you're writing in typescript or javascript makes a difference in the type of answer someone can give you. and about the "API test" comment, it wasn't clear which type of test this is. It's better that people won't need to assume what you actually need. – Thatkookooguy May 13 '19 at 12:44
  • @Thatkookooguy I have an API and I need to test that the responses it sends to a client that makes requests to it contain correctly formatted data. This is what I mean by API test. – papillon May 13 '19 at 12:49
  • If this is only on the server side, using an interface might be enough of a check for the response (since it will give you "compile time errors"). If you want to test it at run-time, just go over the properties and check if they're there. a combined solution might be to use a typescript type guards (which is the same as creating a custom function to check the type). https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards – Thatkookooguy May 13 '19 at 12:52
  • please stop adding back the tag of javascript. Typescript includes javascript so that tag is redundant if you don't mind answers in both flavors – Thatkookooguy May 13 '19 at 13:05
  • @Thatkookooguy it seems to me that this is useful when the data structure is very simple. What if there are lots of nested properties, arrays, objets in the data? I can't just do `if (a.b && a.c && a.c.d && a.e)` etc... It seems to me that I need a way to check the structure of my data in a programmatic way, most probably recursively. – papillon May 13 '19 at 13:05
  • @Thatkookooguy People who follow the tag JavaScript don't necessarily follow the tag TypeScript, do they? My question is not specific to TypeScript. It may have a vanilla JS solution. – papillon May 13 '19 at 13:07
  • ok. but I'm trying to help you make this question more precise and it sounds like you're trying to avoid that. as it currently stands, this sounds like a duplication of this question: https://stackoverflow.com/questions/14425568/interface-type-check-with-typescript – Thatkookooguy May 13 '19 at 13:11
  • Possible duplicate of [Interface type check with Typescript](https://stackoverflow.com/questions/14425568/interface-type-check-with-typescript) – Thatkookooguy May 13 '19 at 13:12
  • @Thatkookooguy the question looks similar, however none of the answers helped me... I actually don't understand why there is an accepted answer. From what I understood, using a discriminator is pretty much the same as checking, one by one, if the data contains the required properties. That's definitely not helpful... – papillon May 13 '19 at 13:35
  • 1
    The "joi" library was specifically built for this use case - https://www.npmjs.com/package/@hapi/joi – AdityaParab May 13 '19 at 13:46
  • @AdityaParab it looks like exactly what I need, thank you – papillon May 13 '19 at 13:57

1 Answers1

1

Alternatively to using another library, you can just use jest's any matcher to assert an object's structure:

results.forEach(result => expect(result).toEqual({
      id: expect.any(Number),
      name: expect.any(String),
      date: expect.any(Date)
    }),
  ));
Kim Kern
  • 54,283
  • 17
  • 197
  • 195