Class Assert

java.lang.Object
tech.jhipster.lite.error.domain.Assert

public class Assert extends Object
This class provides utilities for input assertions.

It is designed to validate domain input, if you want to validate application input it's better to stick to BeanValidation to get all errors at once and internationalized error messages.

The main goal of this class is to ensure some basic type validation in your classes. If you have to do business related validation you should create your own exception and code dedicated to that check

  • Method Details

    • notNull

      public static void notNull(String field, Object input)
      Ensure that the input is not null
      Parameters:
      field - name of the field to check (will be displayed in exception message)
      input - input to check
      Throws:
      MissingMandatoryValueException - if the input is null
    • notBlank

      public static void notBlank(String field, String input)
      Ensure that the value is not blank (null, empty or only whitespace)
      Parameters:
      field - name of the field to check (will be displayed in exception message)
      input - input to check
      Throws:
      MissingMandatoryValueException - if the input is blank
    • notEmpty

      public static void notEmpty(String field, Collection<?> collection)
      Ensure that the given collection is not empty
      Parameters:
      field - name of the field to check (will be displayed in exception message)
      collection - collection to check
      Throws:
      MissingMandatoryValueException - if the collection is null or empty
    • noWhitespace

      public static void noWhitespace(String field, String input)
      Ensure that the value contains no whitespace
      Parameters:
      field - name of the field to check (will be displayed in exception message)
      input - input to check
      Throws:
      MissingMandatoryValueException - if the input contains whitespace
    • field

      public static Assert.StringAsserter field(String field, String input)
      Create a fluent asserter for String

      Usage:

       
       Assert.field("name", name)
         .notBlank()
         .maxLength(150);
       
       
      Parameters:
      field - name of the field to check (will be displayed in exception message)
      input - string to check
      Returns:
      A Assert.StringAsserter for this field and value
    • field

      public static Assert.IntegerAsserter field(String field, Integer input)
      Create a fluent asserter for Integer values (int and Integer)

      Usage:

       
       Assert.field("age", age)
         .min(0)
         .max(150);
       
       
      Parameters:
      field - name of the field to check (will be displayed in exception message)
      input - value to check
      Returns:
      An Assert.IntegerAsserter for this field and value
    • field

      public static Assert.LongAsserter field(String field, Long input)
      Create a fluent asserter for Long values (long and Long)

      Usage:

       
       Assert.field("duration", duration)
         .min(100)
         .max(500_000);
       
       
      Parameters:
      field - name of the field to check (will be displayed in exception message)
      input - value to check
      Returns:
      An Assert.LongAsserter for this field and value
    • field

      public static Assert.FloatAsserter field(String field, Float input)
      Create a fluent asserter for Float values (float and Float)

      Usage:

       
       Assert.field("rate", rate)
         .min(0)
         .max(1);
       
       
      Parameters:
      field - name of the field to check (will be displayed in exception message)
      input - value to check
      Returns:
      An Assert.DoubleAsserter for this field and value
    • field

      public static Assert.DoubleAsserter field(String field, Double input)
      Create a fluent asserter for Double values (double and Double)

      Usage:

       
       Assert.field("rate", rate)
         .min(0)
         .max(1);
       
       
      Parameters:
      field - name of the field to check (will be displayed in exception message)
      input - value to check
      Returns:
      An Assert.DoubleAsserter for this field and value
    • field

      public static Assert.BigDecimalAsserter field(String field, BigDecimal input)
      Create a fluent asserter for BigDecimal values

      Usage:

       
       Assert.field("rate", rate)
         .min(0)
         .max(1);
       
       
      Parameters:
      field - name of the field to check (will be displayed in exception message)
      input - value to check
      Returns:
      An Assert.BigDecimalAsserter for this field and value
    • field

      public static <T> Assert.CollectionAsserter<T> field(String field, Collection<T> input)
      Create a fluent asserter for Collection

      Usage:

       
       Assert.field("name", name)
        .notEmpty()
        .maxSize(150);
       
       
      Parameters:
      field - name of the field to check (will be displayed in exception message)
      input - collection to check
      Returns:
      A Assert.CollectionAsserter for this field and value
    • field

      public static Assert.InstantAsserter field(String field, Instant input)
      Create a fluent asserter for an Instant

      Usage:

       
       Assert.field("date", date)
         .inPast()
         .after(otherDate);
       
       
      Parameters:
      field - name of the field to check (will be displayed in exception message)
      input - value to check
      Returns:
      An Assert.InstantAsserter for this field and value