Validate a Field

In this example, you will learn how you can use DataPipeline to validate data fields 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.Field;
import com.northconcepts.datapipeline.core.FieldType;
import com.northconcepts.datapipeline.core.Record;
import com.northconcepts.datapipeline.foundations.schema.FieldDef;
import com.northconcepts.datapipeline.foundations.schema.NumericFieldDef;

public class ValidateAField {

    public static void main(String[] args) {
        FieldDef fieldDef = new NumericFieldDef("age", FieldType.INT)
                .setRequired(true)
                .setMinimum(25)
                .setMaximum(75);
        
        Record record = new Record();
        Field field = record.addField("age", 76);
        
        System.out.println(fieldDef.validateField(field));
        System.out.println("------------------------------------");
        System.out.println(fieldDef.validateField(record));
        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. A Record object is instantiated, holding the age field to a value of 76.
  3. Then validateField method of FieldDef object is called twice: first time with field variable, the second time with record instance. As can be seen from the output, the results are the same for both operations.

Console Output

{
  "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 large; expected maximum 75, found 76"
  } ],
  "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 large; expected maximum 75, found 76" ],
  "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" : "age is too large; expected maximum 75, found 76"
  } ],
  "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 large; expected maximum 75, found 76" ],
  "valid" : false
}
------------------------------------
Mobile Analytics