Log in to GraphQL Editor
Levensthein algorithm for better faker experience
Artur

Artur Czemiel

10/21/2018

Levensthein algorithm for better faker experience

Today I am writing about faking data again using ts-api-faker . Usually we pass this kind of data to faker

[
  {
    "name": "name.firstName",
    "surname": "name.lastName",
    "mail": "internet.email",
    "profilePhoto": "internet.avatar",
    "animalPhoto": "image.cat"
  },
  {
    "name": "name.firstName",
    "surname": "name.lastName",
    "mail": "internet.email",
    "profilePhoto": "internet.avatar",
    "animalPhoto": "image.dog"
  }
]

and then we receive faked data

[
  {
    "name": "Van",
    "surname": "Veum",
    "mail": "[email protected]",
    "profilePhoto": "https://s3.amazonaws.com/uifaces/faces/twitter/pixage/128.jpg",
    "animalPhoto": "https://source.unsplash.com/200x200/?cat"
  },
  {
    "name": "Serena",
    "surname": "Wilderman",
    "mail": "[email protected]",
    "profilePhoto": "https://s3.amazonaws.com/uifaces/faces/twitter/imcoding/128.jpg",
    "animalPhoto": "https://source.unsplash.com/200x200/?dog"
  }
]

It works perfect but... Usually a schema looks like this.

[
  {
    "name": "String",
    "surname": "String",
    "mail": "String",
    "profilePhoto": "String",
    "animalPhoto": "String"
  }
]

The answer from faker is inappropriate then

[
  {
    "name": "atque",
    "surname": "eos",
    "mail": "voluptas",
    "profilePhoto": "similique",
    "animalPhoto": "earum"
  }
]

What if you use levensthein algorithm on keys of values and transform values to the closest to key name option?

Again we pass

[
  {
    "name": "String",
    "surname": "String",
    "mail": "String",
    "profilePhoto": "String",
    "animalPhoto": "String"
  }
]

Voila, we get the correct faker structure.

[
  {
    "name": "Edgar",
    "surname": "Alyce64",
    "mail": "[email protected]",
    "profilePhoto": "https://source.unsplash.com/200x200/?profile",
    "animalPhoto": "https://source.unsplash.com/200x200/?animal"
  }
]

I've just added this feature to my repo ts-api-faker

Feel free to contribute and extend this library functionality. If you support me and this idea please star this repo to help me get more contributors and extend ts-api-faker library.

In future we can add machine learning to this and get ultimate intelligent faker for our apis and databases :)

Check out our other blogposts

COVID-19 - what does it mean for the dev world
Tomek Poniatowicz
Tomek Poniatowicz
COVID-19 - what does it mean for the dev world
2 min read
about 4 years ago
GraphQL Schema - The Beginner's Guide to GraphQL Editor p.1
Michał Tyszkiewicz
Michał Tyszkiewicz
GraphQL Schema - The Beginner's Guide to GraphQL Editor p.1
2 min read
about 3 years ago
Top 3 python libraries for graphql
Robert Matyszewski
Robert Matyszewski
Top 3 python libraries for graphql
5 min read
about 5 years ago

Ready for take-off?

Elevate your work with our editor that combines world-class visual graph, documentation and API console

Get Started with GraphQL Editor