Validate Records using Rules

This example specializes in validating records based on predefined rules and provides detailed responses indicating the validation outcomes. It offers a flexible and efficient solution for ensuring the accuracy and compliance of data records.

Users can employ the library to validate records in business applications, such as customer management systems, inventory systems, or financial applications. It can verify the correctness of data entries, ensuring that they meet specific criteria or adhere to business rules.

Java Code Listing

package com.northconcepts.datapipeline.foundations.examples.schema;

import com.northconcepts.datapipeline.core.Record;
import com.northconcepts.datapipeline.filter.FieldCount;
import com.northconcepts.datapipeline.filter.FilterExpression;
import com.northconcepts.datapipeline.foundations.schema.EntityDef;

public class ValidateRecordsUsingRules {

    public static void main(String[] args) {
        EntityDef entityDef = new EntityDef()
                .addValidation(new FieldCount(2))
                .addValidation(new FilterExpression("!contains(email, '@example.com') && year(now()) >= 2019"));
        
        
        System.out.println(entityDef.validateRecord(new Record()
                .setField("age", 76)
                .setField("email", "henry@northpole.com")));

        System.out.println("------------------------------------");
        
        System.out.println(entityDef.validateRecord(new Record()
                .setField("name", "")
                .setField("age", 76)
                .setField("email", "jsmith@example2.com")));
        
    }

}

Code walkthrough

  1. EntityDef instance is created with the following validation rules:
    1. The Record should have only two fields.
    2. A custom logical expression is created in the FilterExpression object. It checks that the email should not contain "@example.com" text and that the current year is later than 2019.
  2. Two records are validated for the above rules via EntityDef.validateRecord().

Output

{
  "valid" : true
}
------------------------------------
{
  "errors" : [ {
    "message" : "Record failed validation rule, expected: record has 2 fields"
  } ],
  "fieldNames" : [ null ],
  "fields" : [ null ],
  "messages" : [ "Record failed validation rule, expected: record has 2 fields" ],
  "valid" : false
}
Mobile Analytics