Git Product home page Git Product logo

abap-data-validator's Introduction

abap-data-validator

A data validation tool.

ABAP Data Validator is a tool to simplify data validation process for SAP ABAP development.

Goals:

  • General validation interface, which allows complete validation work by one method call.
  • Centralize some validation logic, avoid duplicate & inconsistent, decrease maintenance cost.
  • Avoid potential dump.

To achieve the goal, ABAP Data Validator has some features

  • Built-in validation logic.
  • Customizable validation rules.
  • Extendable validation program.
  • Internal exception handling.

Type list

ABAP Data Validator supports validations for types below (updating),

  • Date.
  • Time.
  • Timestamp.
  • Email.
  • INT4.
  • REGEX string.
  • URL.
  • JSON.
  • HEX.
  • IMEI.
  • GUID.
  • BASE64.
  • HTML (experimental). And validation of mandatory, validate by reference data elements are also supported.

Usage

Single field validation

For every type, ABAP Data Validator has a special check class. You can use static method is_valid of check class to validate data just like a built-in function. Example:

IF zcl_adv_email_check=>is_valid( '[email protected]' ).
 "do something
ENDIF.

Every check class implements the interface zif_adv_check, which contains methodis_valid. check classere

The naming convention of check classes is ZCL_ADV_typename_CHECK.

All classes have unit tests.

Validation with reference data element

For easy to use, is_valid is a quite simple method, that means it can't provide function on validation of quantity and amount type. In order to solve this issue, ABAP Data Validator provide another way to validate data.

DATA(result) = NEW zcl_adata_validator( )->->validate_by_element(
   data    = '11.0'
   element = 'MENGE_D' "quantity data element name
).

Now data elements with below type are supported,

  • Date.
  • Time.
  • Timestamp.
  • INT4.
  • GUID.
  • HEX.
  • Packed(including most of the quantity type and amount type).

To keep the check method simple, there is also no exception definition of the method validate_by_element. The method handles exceptions internally, and return a result with the result-valid = abap_false, result-type = 'Invalid Type' (fixed value).

Internal table validation

Class zcl_adata_validator provides a general validation method validate.

TRY.
    DATA(result) = NEW zcl_adata_validator( )->validate(
         rules   = my_rules
         data    = uploaded_data
     ).
  CATCH zcx_adv_exception INTO DATA(ex).
    DATA(msg) = ex->get_text( ).
ENDTRY.    

zcl_adata_validator calls check methods internally according to the exporting rules and returns the result.

Rules customization

By the parameter RULES, you can customize the validation.

DATA: rules TYPE zcl_adata_validator=>ty_rules_t.

rules = VALUE #(
  ( fname = 'FIELD1' required = abap_true  initial_or_empty = abap_false  user_type = zcl_adata_validator=>c_type_date )
  ( fname = 'FIELD2' required = abap_false initial_or_empty = abap_true   user_type = zcl_adata_validator=>c_type_date )
  ( fname = 'FIELD3' required = abap_true  initial_or_empty = abap_false  user_type = zcl_adata_validator=>c_type_email )
).

Extend validation for special type

There are two ways to extend the validation:

  • Pass regular expression by RULES-REGEX.
  • Create a new class which implements zif_adv_check, and pass the type name & class name to zcl_adata_validator->constructor.

Regex example. If you want to check whether the input email is a gmail address, you can assign gmail\.com$ to rule-regex:

DATA: rules TYPE zcl_adata_validator=>ty_rules_t.

DATA: cases TYPE ty_case_t.

cases = VALUE #(
    ( field3 = '[email protected]') "correct, it is a gmail address
    ( field3 = '[email protected]') "incorrect, it is not a gmail address
).

rules = VALUE #(
  ( fname = 'FIELD3' user_type = zcl_adata_validator=>c_type_email regex = 'gmail\.com$' regex_msg = 'Only gmail supported')
).

Or add a new class, and add it to check config on demand:

DATA: check_class_config TYPE zcl_adata_validator=>ty_check_config_t.

check_class_config = VALUE #( ( type = zcl_adata_validator=>c_type_new  class = 'ZCL_NEW_VALIDATOR' ) ).

TRY.
     DATA(result) = NEW zcl_adata_validator( check_class_conifg = check_class_config )->validate(
         rules   = rules
         data    = cases
     ).
CATCH zcx_adv_exception INTO DATA(ex).
    DATA(msg) = ex->get_text( ).
ENDTRY.

Configuration

The configuration is hard coded in zcl_adata_validator's constructor. You can also pass you own configuration into the constructor. It allows you to change the function without modifying the existed program.

DATA: check_class_config TYPE zcl_adata_validator=>ty_check_config_t.

SELECT * FROM my_config_table INTO TABLE @check_class_config.

TRY.
    DATA(result) = NEW zcl_adata_validator( check_class_conifg = check_class_config )->validate(
        rules   = rules
        data    = cases
    ).
CATCH zcx_adv_exception INTO DATA(ex).
    DATA(msg) = ex->get_text( ).
ENDTRY.

Note: Custom config will cover default config.

Requirment

ABAP Version: 740 sp08 or higher

Exception

The exception class zcx_adv_exception is copied from zcx_abapgit_exception for it is easy to use:)

TODO

Now only date, time and email type are supported. Need to add more check classes.

  • More types.
  • Auto Type detecting.

abap-data-validator's People

Contributors

hhelibeb avatar frehu avatar larshp avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.