You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@unomi.apache.org by "dgriffon (via GitHub)" <gi...@apache.org> on 2023/03/23 06:56:05 UTC

[GitHub] [unomi] dgriffon commented on a diff in pull request #597: UNOMI-754: new jsonSchema endpoint to be able to validate events and …

dgriffon commented on code in PR #597:
URL: https://github.com/apache/unomi/pull/597#discussion_r1145756152


##########
extensions/json-schema/rest/src/main/java/org/apache/unomi/schema/rest/JsonSchemaEndPoint.java:
##########
@@ -114,4 +116,22 @@ public Response save(String jsonSchema) {
     public boolean remove(String id) {
         return schemaService.deleteSchema(id);
     }
+
+    /**
+     * Being able to validate a given event is useful when you want to develop custom events and associated schemas
+     * @param event the event to be validated
+     * @return Validation error messages if there is some
+     */
+    @POST
+    @Produces(MediaType.APPLICATION_JSON + ";charset=UTF-8")
+    @Consumes({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON })
+    @Path("/validateEvent")
+    public Collection<ValidationError> validateEvent(String event) {
+        try {
+            return schemaService.validateEvent(event);

Review Comment:
   There is small inconsistency in this method, we should return an error code always when the schema is invalid.
   Ideally, all the errors should be handled by the Exception (if possible and have `schemaService.validateEvent()` to return a boolean. The `Collection<ValidationError>` can be carried by the ValidateException. 
   If we don't want to do such change, at least we should return an error code in case of invalid event. 
   



##########
extensions/json-schema/services/src/main/java/org/apache/unomi/schema/api/SchemaService.java:
##########
@@ -29,21 +29,39 @@ public interface SchemaService {
 
     /**
      * Verify if a jsonNode is valid against a schema
+     * (This method is fail safe, if unexpected errors happens it will returns false)
      *
      * @param data   to validate
      * @param schemaId id of the schema used for the validation
-     * @return true is the object is valid
+     * @return true is the object is valid, false otherwise, false also in case of unexpected errors !
      */
     boolean isValid(String data, String schemaId);
 
+    /**
+     * Deprecate (since 2.2.0).
+     * the eventType is now directly extracted from the event source
+     * You can directly use sibling function: isEventValid(String event)
+     */
+    @Deprecated
+    boolean isEventValid(String event, String eventType);
+
     /**
      * Verify if the event is valid
+     * (This method is fail safe, if unexpected errors happens it will returns false)
      *
-     * @param event   to validate
-     * @param eventType The type of the event
+     * @param event the event to check validity
+     * @return true is the event is valid, false otherwise, false also in case of unexpected errors !
+     */
+    boolean isEventValid(String event);
+
+    /**
+     * perform a validation on the given event
+     *
+     * @param event the event to validate
      * @return true is the event is valid
+     * @throws ValidationException in case something goes wrong and validation could not be performed.
      */
-    boolean isEventValid(String event, String eventType);
+    Set<ValidationError> validateEvent(String event) throws ValidationException;

Review Comment:
   The return type does not match the description. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@unomi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org