Validate a Value

In this example, you will learn how you can use DataPipeline to validate field values and provide a response indicating the validation result. It offers a convenient and customizable solution for ensuring data integrity and accuracy.

Users can utilize the example to validate user-submitted data in applications or forms. It can check for data format, completeness, constraints, or any other specific validation rules, providing feedback to users regarding the validity of their input.

Java Code Listing

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

import com.northconcepts.datapipeline.core.FieldType;
import com.northconcepts.datapipeline.foundations.schema.FieldDef;
import com.northconcepts.datapipeline.foundations.schema.NumericFieldDef;

public class ValidateAValue {

    public static void main(String[] args) {
        FieldDef fieldDef = new NumericFieldDef("age", FieldType.INT)
                .setRequired(true)
                .setMinimum(25)
                .setMaximum(75);
        
        System.out.println(fieldDef.validateValue(25));
        System.out.println("------------------------------------");
        System.out.println(fieldDef.validateValue(24));
        System.out.println("------------------------------------");
        System.out.println(fieldDef.validateValue(null));
        System.out.println("------------------------------------");
        
    }

}

Code walkthrough

  1. A new NumericFieldDef instance is created with the name "age" and integer type. Validation rules such as minimum and maximum values are applied for this field.
  2. Then validateField method of FieldDef object is called with different values: 25, 24, and null.

Console Output

{
  "valid" : true
}
------------------------------------
{
  "errors" : [ {
    "field" : {
      "array" : false,
      "attributes" : { },
      "entityQualifiedName" : "age",
      "limitToAllowedValues" : false,
      "maximum" : 75,
      "minimum" : 25,
      "name" : "age",
      "primaryKey" : false,
      "qualifiedName" : "age",
      "required" : true,
      "strictArrays" : true,
      "tags" : { },
      "type" : "INT"
    },
    "message" : "age is too small; expected minimum 25, found 24"
  } ],
  "fieldNames" : [ "age" ],
  "fields" : [ {
    "array" : false,
    "attributes" : { },
    "entityQualifiedName" : "age",
    "limitToAllowedValues" : false,
    "maximum" : 75,
    "minimum" : 25,
    "name" : "age",
    "primaryKey" : false,
    "qualifiedName" : "age",
    "required" : true,
    "strictArrays" : true,
    "tags" : { },
    "type" : "INT"
  } ],
  "messages" : [ "age is too small; expected minimum 25, found 24" ],
  "valid" : false
}
------------------------------------
{
  "errors" : [ {
    "field" : {
      "array" : false,
      "attributes" : { },
      "entityQualifiedName" : "age",
      "limitToAllowedValues" : false,
      "maximum" : 75,
      "minimum" : 25,
      "name" : "age",
      "primaryKey" : false,
      "qualifiedName" : "age",
      "required" : true,
      "strictArrays" : true,
      "tags" : { },
      "type" : "INT"
    },
    "message" : "Required age is null"
  } ],
  "fieldNames" : [ "age" ],
  "fields" : [ {
    "array" : false,
    "attributes" : { },
    "entityQualifiedName" : "age",
    "limitToAllowedValues" : false,
    "maximum" : 75,
    "minimum" : 25,
    "name" : "age",
    "primaryKey" : false,
    "qualifiedName" : "age",
    "required" : true,
    "strictArrays" : true,
    "tags" : { },
    "type" : "INT"
  } ],
  "messages" : [ "Required age is null" ],
  "valid" : false
}
------------------------------------
Mobile Analytics