vertx/src/main/java/eu/internetofus/common/vertx/OpenAPIValidator.java
/**
   * Validate that the value is a valid object.
   *
   * @param promise     to inform of the validation.
   * @param environment to use for the validation.
   *
   * @return the future that says if the value is valid object or not.
   */
Validate that the value is a valid object.
@param promise     to inform of the validation.
@param environment to use for the validation.

@return the future that says if the value is valid object or not.
Validate that the value is a valid object . @param promise to inform of the validation . @param environment to use for the validation . @return the future that says if the value is valid object or not .
static Future<ValueEnvironment> validateObjectValue(@NotNull final Promise<ValueEnvironment> promise,
      @NotNull final ValueEnvironment environment) {
    var future = promise.future();
    if (environment.value instanceof JsonObject) {
      final var value = (JsonObject) environment.value;
      final var properties = environment.specification.getJsonObject("properties");
      if (properties != null) {
        for (final var field : properties.fieldNames()) {
          final var fieldType = properties.getJsonObject(field);
          if (value.containsKey(field)) {
            environment.match = true;
            final var fieldValue = value.getValue(field);
            future = future.compose(
                chain -> validateValue(chain.prefixFor(field), chain.vertx, fieldType, fieldValue).map(validValue -> {
                  if (((JsonObject) chain.value).getValue(field) != validValue) {
                    ((JsonObject) chain.value).put(field, validValue);
                  }
                  return chain;
                }));
          } else {
            future = future.compose(
                chain -> validateValue(chain.prefixFor(field), chain.vertx, fieldType, null).transform(validated -> {
                  if (validated.succeeded()) {
                    final var result = validated.result();
                    if (result != null) {
                      ((JsonObject) chain.value).put(field, result);
                    }
                    chain.match = true;
                  }
                  return Future.succeededFuture(chain);
                }));
          }
        }
        future = future.compose(chain -> {
          chain.addFieldNames(properties);
          return Future.succeededFuture(chain);
        });
      } else {
        final var additionalProperties = environment.specification.getValue("additionalProperties");
        if (additionalProperties instanceof JsonObject) {
          for (final var field : value.fieldNames()) {
            final var fieldValue = value.getValue(field);
            future = future.compose(chain -> validateValue(chain.prefixFor(field), chain.vertx,
                (JsonObject) additionalProperties, fieldValue).map(validValue -> {
                  if (fieldValue != validValue) {
                    ((JsonObject) chain.value).put(field, validValue);
                  }
                  chain.match = true;
                  return chain;
                }));
          }
        }
      }
      final var min = environment.specification.getNumber("minProperties", 0).intValue();
      if (value.size() < min) {
        promise.fail(environment.notValid("Expecting at least '" + min + "' properties."));
        return future;
      }
      if (environment.specification.containsKey("maxProperties")) {
        final var max = environment.specification.getNumber("maxProperties", 0).intValue();
        if (value.size() > max) {
          promise.fail(environment.notValid("Expecting at most '" + max + "' properties."));
          return future;
        }
      }
      final var required = environment.specification.getJsonArray("required", new JsonArray());
      final var max = required.size();
      for (var i = 0; i < max; i++) {
        final var key = required.getString(i);
        if (!value.containsKey(key)) {
          promise.fail(environment.notValid(key, "Required property not defined."));
          return future;
        }
      }
    } else {
      promise.fail(environment.notValid("Expecting a 'JsonObject' value."));
    }
    return future;
  }
static Future < ValueEnvironment > validateObjectValue ( @ NotNull final Promise < ValueEnvironment > promise , @ NotNull final ValueEnvironment environment ) { var future = promise . future ( ) ; if ( environment . value instanceof JsonObject ) { final var value = ( JsonObject ) environment . value ; final var properties = environment . specification . getJsonObject ( "properties" ) ; if ( properties != null ) { for ( final var field : properties . fieldNames ( ) ) { final var fieldType = properties . getJsonObject ( field ) ; if ( value . containsKey ( field ) ) { environment . match = true ; final var fieldValue = value . getValue ( field ) ; future = future . compose ( chain -> validateValue ( chain . prefixFor ( field ) , chain . vertx , fieldType , fieldValue ) . map ( validValue -> { if ( ( ( JsonObject ) chain . value ) . getValue ( field ) != validValue ) { ( ( JsonObject ) chain . value ) . put ( field , validValue ) ; } return chain ; } ) ) ; } else { future = future . compose ( chain -> validateValue ( chain . prefixFor ( field ) , chain . vertx , fieldType , null ) . transform ( validated -> { if ( validated . succeeded ( ) ) { final var result = validated . result ( ) ; if ( result != null ) { ( ( JsonObject ) chain . value ) . put ( field , result ) ; } chain . match = true ; } return Future . succeededFuture ( chain ) ; } ) ) ; } } future = future . compose ( chain -> { chain . addFieldNames ( properties ) ; return Future . succeededFuture ( chain ) ; } ) ; } else { final var additionalProperties = environment . specification . getValue ( "additionalProperties" ) ; if ( additionalProperties instanceof JsonObject ) { for ( final var field : value . fieldNames ( ) ) { final var fieldValue = value . getValue ( field ) ; future = future . compose ( chain -> validateValue ( chain . prefixFor ( field ) , chain . vertx , ( JsonObject ) additionalProperties , fieldValue ) . map ( validValue -> { if ( fieldValue != validValue ) { ( ( JsonObject ) chain . value ) . put ( field , validValue ) ; } chain . match = true ; return chain ; } ) ) ; } } } final var min = environment . specification . getNumber ( "minProperties" , 0 ) . intValue ( ) ; if ( value . size ( ) < min ) { promise . fail ( environment . notValid ( "Expecting at least '" + min + "' properties." ) ) ; return future ; } if ( environment . specification . containsKey ( "maxProperties" ) ) { final var max = environment . specification . getNumber ( "maxProperties" , 0 ) . intValue ( ) ; if ( value . size ( ) > max ) { promise . fail ( environment . notValid ( "Expecting at most '" + max + "' properties." ) ) ; return future ; } } final var required = environment . specification . getJsonArray ( "required" , new JsonArray ( ) ) ; final var max = required . size ( ) ; for ( var i = 0 ; i < max ; i ++ ) { final var key = required . getString ( i ) ; if ( ! value . containsKey ( key ) ) { promise . fail ( environment . notValid ( key , "Required property not defined." ) ) ; return future ; } } } else { promise . fail ( environment . notValid ( "Expecting a 'JsonObject' value." ) ) ; } return future ; }
Validate that the value is a valid object.
Validate that the value is a valid object .
// the fields are optional by default, but if they are not defined and has a // default value it has to be set