oeCloud - Relation Validation

There are scenarios where a Model is related to another Model and we want to apply validations based on these relationships.This guide demonstrates the process of applying BelongsTo relational validations on a model.

What you’ll learn

By the end of this guide

  • You’ll build a model Category with some properties.
  • You’ll build a model Product model with a belongsTo relationship with the model Category.
  • Verify the belongsTo relational validation by POSTing data to Product and Category Models.

What You’ll need:

To complete this guide, you will need the following -

  • an understanding of what a Model is in the context of Loopback, which is the basis of the oeCloud Framework. You can go through the loopback documentation for the same by clicking here
  • an understanding of Model creation. You can go through the guide for the same.
  • an understanding of what Relations are in the context of Loopback.You can go through the loopback documentation for the same by clicking here
  • a running NodeJS application built using the oeCloud Framework.
  • a working REST client. You can use the Linux cURL command as a REST client if you have access to a Linux machine or have Git Bash installed on your Windows machine. REST addons for browsers, like Postman for Firefox or Google Chrome also can be used to complete this guide. You can use API explorer also to complete this guide.
  • an understanding of basic property validations, you can go through the guides for the same. - optional.

How to complete this guide:

You can start from scratch and complete each step, or you can bypass basic setup steps that are already familiar to you.

To start from the scratch go to Getting Started

Create Category and Product Models

We will first create the Category model with properties mentioned below. To do this, you can follow the Create a Model with Properties Guide.But briefly, you can POST below JSON Data for Category Model to ModelDefinitions. Request API will be http://localhost:3000/api/ModelDefinitions.

{
	"name": "Category",
	"base": "BaseEntity",
	"strict": false,
	"plural": "Categories",
	"idInjection": true,
	"options": {
		"validateUpsert": true
	},
	"properties": {
		"catCode": {
			"type": "string",
			"required": true,
			"unique": true,
			"id":true
		},
		"name": {
			"type": "string"
		},
		"description": {
			"type": "string"
		}
	}
}

If you have already created the Product model following the previous guides then modify the Product Model. To do this you can follow the Manage a Model guide. If not then create the Product model with properties mentioned below. To do this, you can follow the Create a Model with Properties Guide.But briefly, you can POST below JSON Data for Category Model to ModelDefinitions. Request API will be http://localhost:3000/api/ModelDefinitions. Product :

{
	"name": "Product",
	"base": "BaseEntity",
	"strict": false,
	"plural": "Products",
	"idInjection": true,
	"options": {
		"validateUpsert": true
	},
	"properties": {
		"productCode": {
			"type": "string",
			"required": true,
			"unique": true
		},
		"name": {
			"type": "string"
		},
		"category": {
			"type": "string",
			"in": ["Premium", "Luxury", "Regular"]
		},
		"price": {
			"type": "number",
			"numericality": "integer"
		},
		"offeredSince": {
			"type": "date"
		},
		"active": {
			"type": "string",
			"in": ["Active", "Inactive"]
		},
		"description": {
			"type": "string"
		}
	},
	"relations": {
        "productcategory": {
            "type": "belongsTo",
            "model": "Category",
            "foreignKey": "categoryCode"
        }
    }
}

Now that you’ve created the Category and the Product model, time to check the relation validation.

Let’s use the below mentioned api and some data to post to the Category model.

http://localhost:3000/api/Categories
{
	"catCode": "C1001",
	"name": "DC Collectibles",
	"description": "Collectible items used by various DC characters"
}

Now let’s post some data to the Product model using the below api.

http://localhost:3000/api/Products
  • Post the below mentioned product data with a valid categoryCode (categoryCode exists in the Category model).
{
	"productCode": "EX1001",
	"name": "Kryptonium",
	"description": "Fataly injure Superman",
	"category": "Premium",
	"price": 10000000,
	"offeredSince": "01-01-2002",
	"categoryCode" : "C1001"
}

Results in a valid response with status code 200.

  • Let’s post some data with a non-existent categoryCode (categoryCode doesn’t exist in the Category model).
{
	"productCode": "EX1002",
	"name": "Bat-Mobile",
	"description": "Wheeled, self-powered, armoured fighting motor vehicle, frequently used by Batman - manufactured by Wayne Industries",
	"category": "Luxury",
	"price": 1000000,
	"offeredSince": "01-01-2012",
	"categoryCode" : "C1002"
}

Fails with a status code of 422 as the data doesn’t adhere to the applied relationship constraint.

Note

If the data you POST do not have the foreign key(in above example its categoryCode) then this validation will pass.To make the checking of foreign key compulsory you need to put "foreignKeyRequired": true in the relations(in above example inside productcategory).

Summary

Congratulations! You’ve just completed a relation validation example using an application powered by oeCloud.