Aaron Benton is an experienced architect who specializes in creative solutions to develop innovative mobile applications. He has over 10 years experience in full stack development, including ColdFusion, SQL, NoSQL, JavaScript, HTML, and CSS. Aaron is currently an Applications Architect for Shop.com in Greensboro, North Carolina and is a Couchbase Community Champion.

Aaron Benton

FakeIt Series 3 of 5: Lean Models through Definitions

In our previous post FakeIt Series 2 of 5: Shared Data and Dependencies we saw how to create multi-model dependencies with FakeIt. Today we are going to look at how we can create the same, but smaller models by leveraging definitions.

Definitions are a way of creating reusable set(s) within your model. It allows you to define a set of properties / values one time, referencing them multiple times throughout your model. Definitions in a FakeIt model are very similar to how definitions are used in the Swagger / Open API Specification.

Users Model

We will start with our users.yaml model that we defined in our first post.

Lets say that we have a new requirement where we have to support a home and work address for each user. Based on this requirement, we have decided to create a top-level property named addresses that will contain nested properties of home and work.

Our users.yaml model will need to be updated to support these new address properties.

(For brevity the other properties have been left off of the model definition)

As you can see our home and work address properties contain the exact same nested properties. This duplication makes our model bigger and more verbose. Additionally, what happens if our address requirements need to change? We’d have to make two updates to keep the structure the same. This is where we can take advantage of definitions, by defining a single way to create an address and reference it.

(For brevity the other properties have been left off of the model definition)

Definitions are defined at the root of the model by specifying a definitions: property, then the name of the definition. Definitions are referenced by using $ref with the value being the path to the definition, for example #/definitions/Address. By using definitions we have saved almost 30 lines of code in our model and created a single place for how an Address is to be defined and generated within our Users model.

We can test the output of our updated Users model addresses using the following command:

Now, lets say we have a requirement to store a Users main phone number, and then store optional additional phone numbers i.e. Home, Work, Mobile, Fax, etc. For this change we will use two new top-level properties: main_phone and additional_phones that is an array.

While this may not be the most practical data model, or how I personally would have modeled this data we can use this example again to illustrate how we can take advantage of using definitions within our FakeIt model.

(For brevity the other properties have been left off of the model definition)

For this example we have created a Phone definition that contains 3 properties: type, phone_number and extension. You will notice that we have defined a post_build function on the main_phone property that removes the type attribute. This is illustrating how definitions can be used in conjunction with a build function to manipulate what is returned by the definition. The additional_phones property is an array of Phone definitions that will generate between 1 – 4 phones. We can test the output of our updated Users model phone numbers using the following command:


Definitions allow you to streamline your FakeIt models by creating reusable sets of code for smaller more efficient models.

Up Next


Couchbase Champion This post is part of the Couchbase Community Writing Program


Posted by Laura Czajkowski, Developer Community Manager, Couchbase

Laura Czajkowski is the Snr. Developer Community Manager at Couchbase overseeing the community. She’s responsible for our monthly developer newsletter.

Leave a reply