Validate Record Fields

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

public class ValidateRecordFields {

    public static void main(String[] args) {
        EntityDef entityDef = new EntityDef();
        entityDef.addField(new TextFieldDef("name", FieldType.STRING)
                .setRequired(true)
                .setAllowBlank(false)
                .setMaximumLength(100));
        entityDef.addField(new NumericFieldDef("age", FieldType.INT)
                .setRequired(true)
                .setMinimum(25)
                .setMaximum(75));
        
        Record record = new Record();
        record.addField("name", "");
        record.addField("age", 76);
        
        System.out.println(entityDef.validateRecord(record));
    }

}

 

Code walkthrough

  1. A new EntityDef instance is created with the following two fields: "name", and "age". Validation rules such as string length, minimum and maximum values are applied for this field.
  2. A Record object is instantiated, holding the age field of 76 and the name field with a value of "".
  3. Then validateRecord method of EntityDef object is called and the output is printed in the console.

 

Console Output

{
  "errors" : [ {
    "entity" : {
      "addMissingOptionalFields" : false,
      "allowExtraFieldsInMapping" : true,
      "allowExtraFieldsInValidation" : true,
      "attributes" : { },
      "fields" : [ {
        "allowBlank" : false,
        "array" : false,
        "attributes" : { },
        "entityQualifiedName" : "name",
        "limitToAllowedValues" : false,
        "maximumLength" : 100,
        "name" : "name",
        "primaryKey" : false,
        "qualifiedName" : "name",
        "required" : true,
        "strictArrays" : true,
        "tags" : { },
        "type" : "STRING"
      }, {
        "array" : false,
        "attributes" : { },
        "entityQualifiedName" : "age",
        "limitToAllowedValues" : false,
        "maximum" : 75,
        "minimum" : 25,
        "name" : "age",
        "primaryKey" : false,
        "qualifiedName" : "age",
        "required" : true,
        "strictArrays" : true,
        "tags" : { },
        "type" : "INT"
      } ],
      "tags" : { }
    },
    "field" : {
      "allowBlank" : false,
      "array" : false,
      "attributes" : { },
      "entityQualifiedName" : "name",
      "limitToAllowedValues" : false,
      "maximumLength" : 100,
      "name" : "name",
      "primaryKey" : false,
      "qualifiedName" : "name",
      "required" : true,
      "strictArrays" : true,
      "tags" : { },
      "type" : "STRING"
    },
    "message" : "name is blank; expected at least 1 character(s)"
  }, {
    "entity" : {
      "addMissingOptionalFields" : false,
      "allowExtraFieldsInMapping" : true,
      "allowExtraFieldsInValidation" : true,
      "attributes" : { },
      "fields" : [ {
        "allowBlank" : false,
        "array" : false,
        "attributes" : { },
        "entityQualifiedName" : "name",
        "limitToAllowedValues" : false,
        "maximumLength" : 100,
        "name" : "name",
        "primaryKey" : false,
        "qualifiedName" : "name",
        "required" : true,
        "strictArrays" : true,
        "tags" : { },
        "type" : "STRING"
      }, {
        "array" : false,
        "attributes" : { },
        "entityQualifiedName" : "age",
        "limitToAllowedValues" : false,
        "maximum" : 75,
        "minimum" : 25,
        "name" : "age",
        "primaryKey" : false,
        "qualifiedName" : "age",
        "required" : true,
        "strictArrays" : true,
        "tags" : { },
        "type" : "INT"
      } ],
      "tags" : { }
    },
    "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" : [ "name", "age" ],
  "fields" : [ {
    "allowBlank" : false,
    "array" : false,
    "attributes" : { },
    "entityQualifiedName" : "name",
    "limitToAllowedValues" : false,
    "maximumLength" : 100,
    "name" : "name",
    "primaryKey" : false,
    "qualifiedName" : "name",
    "required" : true,
    "strictArrays" : true,
    "tags" : { },
    "type" : "STRING"
  }, {
    "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" : [ "name is blank; expected at least 1 character(s)", "age is too large; expected maximum 75, found 76" ],
  "valid" : false
}

 

Mobile Analytics