model/src/main/java/eu/internetofus/common/model/ValidateContext.java
/**
   * Verify that a field with a list of models is valid.
   *
   * @param fieldName name of the field to validate.
   * @param value     to verify.
   * @param predicate used to compare if two models are equals, to check if they
   *                  are duplicated.
   *
   * @param <T>       type of models to verify.
   *
   * @return the validation result of the field.
   */
Verify that a field with a list of models is valid.
@param fieldName name of the field to validate.
@param value     to verify.
@param predicate used to compare if two models are equals, to check if they
are duplicated.

@param        type of models to verify.

@return the validation result of the field.
Verify that a field with a list of models is valid . @param fieldName name of the field to validate . @param value to verify . @param predicate used to compare if two models are equals to check if they are duplicated . @param type of models to verify . @return the validation result of the field .
default <T extends Validable<SELF>> Function<Void, Future<Void>> validateListField(final String fieldName,
      final List<T> value, final BiPredicate<T, T> predicate) {
    return empty -> {
      if (value == null) {
        return this.failField(fieldName, "The '" + fieldName + "' can not be 'null'.");
      } else {
        @SuppressWarnings("rawtypes")
        final List<Future> futures = new ArrayList<>();
        final var iterator = value.listIterator();
        while (iterator.hasNext()) {
          final var model = iterator.next();
          if (model == null) {
            iterator.remove();
          } else {
            final var index = iterator.previousIndex();
            final var elementContext = this.createFieldElementContext(fieldName, index);
            for (var firstIndex = 0; firstIndex < index; firstIndex++) {
              final var element = value.get(firstIndex);
              if (predicate.test(model, element)) {
                return elementContext.fail("This model is already defined at '" + firstIndex + "'.");
              }
            }
            futures.add(model.validate(elementContext));
          }
        }
        return CompositeFuture.all(futures).map(any -> null);
      }
    };
  }
default < T extends Validable < SELF > > Function < Void , Future < Void > > validateListField ( final String fieldName , final List < T > value , final BiPredicate < T , T > predicate ) { return empty -> { if ( value == null ) { return this . failField ( fieldName , "The '" + fieldName + "' can not be 'null'." ) ; } else { @ SuppressWarnings ( "rawtypes" ) final List < Future > futures = new ArrayList < > ( ) ; final var iterator = value . listIterator ( ) ; while ( iterator . hasNext ( ) ) { final var model = iterator . next ( ) ; if ( model == null ) { iterator . remove ( ) ; } else { final var index = iterator . previousIndex ( ) ; final var elementContext = this . createFieldElementContext ( fieldName , index ) ; for ( var firstIndex = 0 ; firstIndex < index ; firstIndex ++ ) { final var element = value . get ( firstIndex ) ; if ( predicate . test ( model , element ) ) { return elementContext . fail ( "This model is already defined at '" + firstIndex + "'." ) ; } } futures . add ( model . validate ( elementContext ) ) ; } } return CompositeFuture . all ( futures ) . map ( any -> null ) ; } } ; }
Verify that a field with a list of models is valid.
Verify that a field with a list of models is valid .