How to handle the many validation checks needed before creating an object?

I have a class that models FK relationships. It has 2 lists. These lists contain the column names of the parent table and the child table, respectively. These lists are transmitted to me by the client. Now, before creating my FK object, I think it is necessary to perform the following checks (in order):

  • Check if lists are zeros.
  • Check if lists contain null.
  • If the list contains duplicate columns?
  • The size of both lists is equal.

So, you can see that there will be 7 checks. Can there be so many checks?

If you have all of these checks, is there any template for handling such cases (with a high number of check checks)?

If this is not so, then what should I do? Should I just document these conditions under the contract and mention that the API will give meaningless results if this contract is violated?

Edit: Basically, I am trying to take these 2 lists and create a database specific query. Thus, it is important to build this object correctly.

+4
source share
4 answers

As everyone says, it is up to you. There is no such fixed / standard guide for this. But to make it simple, you need to put all your validation logic in one place so that it remains readable and easily changing.

, , . , db. , FKEntity. , , FKEntity.validate() ( Validatable), ... , FKEntity . - , / FKEntity (, FKEntity "x", "x" , , ), .

Inteface Validatable { void validate() throws InvalidEntityException; }   

Class FKEntity implements Validatable {
  //..
  public void validate() throws InvalidEntityException {
     //your entity specific logic
  }
}

Class FKDigestService {

    public digestEntities() {

      try {
      for(FKEntity e : entityList)
         e.validate();

      //your collective validation logic goes here
      } catch (EntityValidationException e) {//do whatever you want}
    }

}

:

  • ( )

  • , , , FKEntity, ... ,

+3

, , .

:

wrapping private method doAllNeededListsValidationInFixedOrder(), - .

, , doAllNeededListsValidationInFixedOrder, - javadoc, .

- . , , , - , , .

, State - .

- Builder , . , () ( ) , .

- , , , , .

+2

. . API, . .

, , . API. . , .

, ? , - ? - , . ( ), . , .

+2

, - ( )?

, .

  • List from a client -

  • List from a client list of not duplicating not null elements

  • This transformation can be decomposed into a few simple changes ToNonNull, ToNonNullList,ToNonDuplicatingList

  • The last requirement is the conversion from two lists to one list of pairs ToPairs(ListA, ListB)

Combine this:

ParentTableColumns = List1FromClient.
    ToNonNull.
    ToNonNullList.
    ToNonDuplicatingList

ChildTableColumns = List2FromClient.
    ToNonNull. 
    ToNonNullList.
    ToNonDuplicatingList

ParentChildColumnPairs = List.
    ToPairs(ParentTableColumns, ChildTableColumns)

If the data from the client is valid, all conversions are successful and a valid result.

If the data from the client is invalid, then one of the conversions fails and gives an error message.

0
source

All Articles