You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@unomi.apache.org by js...@apache.org on 2023/05/02 15:07:20 UTC

[unomi] branch UNOMI-775-add-validation-endpoint updated: remove eventType from ValidationException

This is an automated email from the ASF dual-hosted git repository.

jsinovassinnaik pushed a commit to branch UNOMI-775-add-validation-endpoint
in repository https://gitbox.apache.org/repos/asf/unomi.git


The following commit(s) were added to refs/heads/UNOMI-775-add-validation-endpoint by this push:
     new 67f24d660 remove eventType from ValidationException
67f24d660 is described below

commit 67f24d660ae3fa06a01e6ddb9b1990158ba69d15
Author: jsinovassin <js...@jahia.com>
AuthorDate: Tue May 2 17:07:14 2023 +0200

    remove eventType from ValidationException
---
 .../unomi/schema/api/ValidationException.java      | 15 ------------
 .../unomi/schema/impl/SchemaServiceImpl.java       | 28 ++++++++++------------
 2 files changed, 12 insertions(+), 31 deletions(-)

diff --git a/extensions/json-schema/services/src/main/java/org/apache/unomi/schema/api/ValidationException.java b/extensions/json-schema/services/src/main/java/org/apache/unomi/schema/api/ValidationException.java
index c5481da83..58610b285 100644
--- a/extensions/json-schema/services/src/main/java/org/apache/unomi/schema/api/ValidationException.java
+++ b/extensions/json-schema/services/src/main/java/org/apache/unomi/schema/api/ValidationException.java
@@ -23,17 +23,10 @@ package org.apache.unomi.schema.api;
  */
 public class ValidationException extends Exception {
 
-    private String eventType;
-
     public ValidationException(String message) {
         super(message);
     }
 
-    public ValidationException(String message, String eventType) {
-        super(message);
-        this.eventType = eventType;
-    }
-
     public ValidationException(Throwable throwable) {
         super(throwable);
     }
@@ -41,12 +34,4 @@ public class ValidationException extends Exception {
     public ValidationException(String message, Throwable throwable) {
         super(message, throwable);
     }
-
-    public void setEventType(String eventType) {
-        this.eventType = eventType;
-    }
-
-    public String getEventType() {
-        return eventType;
-    }
 }
diff --git a/extensions/json-schema/services/src/main/java/org/apache/unomi/schema/impl/SchemaServiceImpl.java b/extensions/json-schema/services/src/main/java/org/apache/unomi/schema/impl/SchemaServiceImpl.java
index 5adcbc010..ca54fd243 100644
--- a/extensions/json-schema/services/src/main/java/org/apache/unomi/schema/impl/SchemaServiceImpl.java
+++ b/extensions/json-schema/services/src/main/java/org/apache/unomi/schema/impl/SchemaServiceImpl.java
@@ -124,10 +124,14 @@ public class SchemaServiceImpl implements SchemaService {
         Map<String, Set<ValidationError>> errorsPerEventType = new HashMap<>();
         JsonNode eventsNodes = parseData(events);
         eventsNodes.forEach(event -> {
+            String eventType = null;
             try {
-                Set<ValidationError> errors = validateNodeEvent(event);
+                eventType = extractEventType(event);
+                JsonSchemaWrapper eventSchema = getSchemaForEventType(eventType);
+                JsonSchema jsonSchema = getJsonSchema(eventSchema.getItemId());
+
+                Set<ValidationError> errors = validate(event, jsonSchema);
                 if (!errors.isEmpty()) {
-                    String eventType = event.get("eventType").asText();
                     if (errorsPerEventType.containsKey(eventType)) {
                         errorsPerEventType.get(eventType).addAll(errors);
                     } else {
@@ -136,11 +140,11 @@ public class SchemaServiceImpl implements SchemaService {
                 }
             } catch (ValidationException e) {
                 Set<ValidationError> errors = buildCustomErrorMessage(e.getMessage());
-                String eventType = e.getEventType() != null ? e.getEventType() : GENERIC_ERROR_KEY;
-                if (errorsPerEventType.containsKey(eventType)) {
-                    errorsPerEventType.get(eventType).addAll(errors);
+                String eventTypeOrErrorKey = eventType != null ? eventType : GENERIC_ERROR_KEY;
+                if (errorsPerEventType.containsKey(eventTypeOrErrorKey)) {
+                    errorsPerEventType.get(eventTypeOrErrorKey).addAll(errors);
                 } else {
-                    errorsPerEventType.put(eventType, errors);
+                    errorsPerEventType.put(eventTypeOrErrorKey, errors);
                 }
             }
         });
@@ -154,14 +158,6 @@ public class SchemaServiceImpl implements SchemaService {
         return errors;
     }
 
-    private Set<ValidationError> validateNodeEvent(JsonNode event) throws ValidationException {
-        String eventType = extractEventType(event);
-        JsonSchemaWrapper eventSchema = getSchemaForEventType(eventType);
-        JsonSchema jsonSchema = getJsonSchema(eventSchema.getItemId());
-
-        return validate(event, jsonSchema);
-    }
-
     @Override
     public JsonSchemaWrapper getSchema(String schemaId) {
         return schemasById.get(schemaId);
@@ -182,7 +178,7 @@ public class SchemaServiceImpl implements SchemaService {
     @Override
     public JsonSchemaWrapper getSchemaForEventType(String eventType) throws ValidationException {
         if (StringUtils.isEmpty(eventType)) {
-            throw new ValidationException("eventType missing", eventType);
+            throw new ValidationException("eventType missing");
         }
 
         return schemasById.values().stream()
@@ -192,7 +188,7 @@ public class SchemaServiceImpl implements SchemaService {
                                 jsonSchemaWrapper.getName() != null &&
                                 jsonSchemaWrapper.getName().equals(eventType))
                 .findFirst()
-                .orElseThrow(() -> new ValidationException("Schema not found for event type: " + eventType, eventType));
+                .orElseThrow(() -> new ValidationException("Schema not found for event type: " + eventType));
     }
 
     @Override