Skip to content

Commit

Permalink
Simplified EntityName regex which was overly complex
Browse files Browse the repository at this point in the history
Signed-off-by: Yannic Klem <Yannic.Klem@bosch.io>
  • Loading branch information
Yannic92 committed Mar 24, 2022
1 parent 4887cc3 commit 4bda33e
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,33 +82,11 @@ public final class RegexPatterns {
public static final String NAMESPACE_REGEX = "(?<" + NAMESPACE_GROUP_NAME + ">|(?:" +
"(?:" + ALLOWED_NAMESPACE_CHARACTERS_REGEX + ")" +
"(?:" + ALLOWED_NAMESPACE_CHARACTERS_REGEX_INNER + ")*+))";

/**
* Regex pattern that matches URL escapes. E.G. %3A for a colon (':').
*/
public static final String URL_ESCAPES = "%\\p{XDigit}{2}";

/**
* Adds the $ to allowed characters. Its defined as separate constant because names are not allowed to start
* with $.
*/
public static final String ALLOWED_CHARACTERS_IN_NAME_INCLUDING_DOLLAR = ALLOWED_CHARACTERS_IN_NAME + "$";

/**
* First part of an entity name.
*/
public static final String URI_PATH_SEGMENT = "(?:[" + ALLOWED_CHARACTERS_IN_NAME + "]|" + URL_ESCAPES + ")";

/**
* Second part of an entity name: This part allows the $ symbol.
*/
public static final String URI_PATH_SEGMENT_INCLUDING_DOLLAR =
"(?:[" + ALLOWED_CHARACTERS_IN_NAME_INCLUDING_DOLLAR + "]|" + URL_ESCAPES + ")";
/**
* The regex pattern for an Entity Name.
*/
public static final String ENTITY_NAME_REGEX = "(?<" + ENTITY_NAME_GROUP_NAME +
">" + URI_PATH_SEGMENT + URI_PATH_SEGMENT_INCLUDING_DOLLAR + "*+)";
public static final String ENTITY_NAME_REGEX =
"(?<" + ENTITY_NAME_GROUP_NAME + ">" + ALLOWED_CHARACTERS_IN_NAME + "++)";

/**
* The regex pattern for an Entity ID.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,4 @@ protected EntityId getFallbackEntityId(final EntityType entityType, final CharSe

}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ The name must conform to the following notation:
* may not contain control characters

When writing a Java application, you can use the following regex to validate your thing name:
``(?<name>(?:[[^\x00-\x1F\x7F-\xFF/]]|%\p{XDigit}{2})(?:[[^\x00-\x1F\x7F-\xFF/]$]|%\p{XDigit}{2})*+)``
``(?<name>[^\x00-\x1F\x7F-\xFF/]++)``
(see [RegexPatterns#ENTITY_NAME_REGEX](https://github.com/eclipse/ditto/blob/master/base/model/src/main/java/org/eclipse/ditto/base/model/entity/id/RegexPatterns.java)).

Examples for valid names:
Expand All @@ -52,7 +52,7 @@ A namespaced ID must conform to the following expectations:
* have a maximum length of 256 characters

When writing a Java application, you can use the following regex to validate your namespaced IDs:
``(?<ns>|(?:(?:[a-zA-Z]\w*+)(?:([.\-])[a-zA-Z]\w*+)*+)):(?<name>(?:[[^\x00-\x1F\x7F-\xFF/]]|%\p{XDigit}{2})(?:[[^\x00-\x1F\x7F-\xFF/]$]|%\p{XDigit}{2})*+)``
``(?<ns>|(?:(?:[a-zA-Z]\w*+)(?:([.\-])[a-zA-Z]\w*+)*+)):(?<name>[^\x00-\x1F\x7F-\xFF/]++)``
(see [RegexPatterns#ID_REGEX](https://github.com/eclipse/ditto/blob/master/base/model/src/main/java/org/eclipse/ditto/base/model/entity/id/RegexPatterns.java)).

Examples for valid IDs:
Expand Down

0 comments on commit 4bda33e

Please sign in to comment.