oeCloud - Conditional Validation Using Expression Language

This guide helps you to add conditional validation with the concept of ‘validateWhen’ using ‘Expression Language’.

What you’ll build

  • You’ll build a model ‘Product’ with attached declarative validations.
  • You’ll add validateWhen to some of the properties to apply validations based on a certain condition.

What You’ll need:

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 scratch download and install sample application from here.

Create a model Product with some validations attached to the property of the model. To achieve conditional validation add validateWhen object which contains the applied validation as the key and an expression as value. The concept is to consult and evaluate the values of the validateWhen object to check for the applicability condition for the validation which evaluates to a boolean value. If the condition evaluates to true, that particular validation is applied on the property or else it is ignored.

{
	"name": "Product",
	"base": "BaseEntity",
	"strict": false,
	"plural": "Products",
	"idInjection": true,
	"options": {
		"validateUpsert": true
	},
	"properties": {
		"code": {
			"type": "string",
			"required": true,
			"unique": true
		},
		"name": {
			"type": "string",
			"min": 5,
			"max": 10,
			"validateWhen": {
				"max": "Date(@i.offeredSince).year > 2000"
			}
		},
		"category": {
			"type": "string",
			"in": ["Premium", "Luxury"],
			"validateWhen": {
				"in": "@i.price > 100000"
			}
		},
		"price": {
			"type": "number",
			"numericality": "integer",
			"max": 10000,
			"validateWhen": {
				"max": "@i.category === \"Regular\""
			}
		},
		"offeredSince": {
			"type": "date"
		},
		"active": {
			"type": "string",
			"in": ["Active", "Inactive"]
		},
		"description": {
			"type": "string",
			"required":"true",
			"validateWhen": {
				"required": "(Date.now.year - Date(@i.offeredSince).year) > 5"
			}
		}
	}
}

Now that you’ve created the Product model, time to check the conditional application of validations. Let’s use the below mentioned api and post some data.

http://localhost:3000/explorer/Product

The success/error response can be checked in the response body after posting the data.

  • Post some valid json to check the api.
{
	"code": "EX1001",
	"name": "Kryptonium",
	"description": "Fataly injure Superman",
	"category": "Premium",
	"price": 10000000,
	"offeredSince": "01-01-2002"
}

Results in a valid response with status code 200. The api http://localhost:3000/explorer/Product works!

  • Post the json below to observe the validation error as the price of the item is too high to have the category Regular
{
	"code": "EX1002",
	"name": "Bat-Mobile",
	"description": "Wheeled, self-powered, armoured fighting motor vehicle, frequently used by Batman - manufactured by Wayne Industries",
	"category": "Business",
	"price": 100001,
	"offeredSince": "01-01-2012"
}

Results in a response with status code 422.

  • Post the json below to observe the validation error as category should be “Premium” or “Luxury” when price is > 100000.
{
	"code": "EX1003",
	"name": "Batarang",
	"description": "roughly bat-shaped throwing weapon used by the vigilante Batman",
	"category": "Regular",
	"price": 10000,
	"offeredSince": "01-01-1998"
}

Results in a response with status code 422.

  • Post the below json to observe the validation error as the name is too long for a product offered after year 2000
{
	"code": "EX1003",
	"name": "Clown heist mask",
	"description": "Cool, Awesome,Strong And Durable mask used by Joker's aides",
	"category": "Regular",
	"price": 1000,
	"offeredSince": "01-01-2015"
}

Results in a response with status code 422.

  • Post the below json to observe the validation error as the description is required for products that have been offered for more than 5 years.
{
	"code": "EX1004",
	"name": "Wonder Woman's Shield",
	"category": "Premium",
	"price": 10000000,
	"offeredSince": "02-03-1998"
}

Results in a response with status code 422.

Summary

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