Skip to content

Commit

Permalink
guardrails npe (opensearch-project#2304)
Browse files Browse the repository at this point in the history
Signed-off-by: Jing Zhang <[email protected]>
  • Loading branch information
jngz-es authored Apr 9, 2024
1 parent e3e72f7 commit abba0ec
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ public Boolean validate(String input, Type type) {
}

public Boolean validateRegexList(String input, List<Pattern> regexPatterns) {
if (regexPatterns == null || regexPatterns.isEmpty()) {
return true;
}
for (Pattern pattern : regexPatterns) {
if (!validateRegex(input, pattern)) {
return false;
Expand All @@ -107,6 +110,9 @@ public Boolean validateRegex(String input, Pattern pattern) {
}

public Boolean validateStopWords(String input, Map<String, List<String>> stopWordsIndices) {
if (stopWordsIndices == null || stopWordsIndices.isEmpty()) {
return true;
}
for (Map.Entry entry : stopWordsIndices.entrySet()) {
if (!validateStopWordsSingleIndex(input, (String) entry.getKey(), (List<String>) entry.getValue())) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,22 @@ public void validateRegexListFailed() {
Assert.assertFalse(res);
}

@Test
public void validateRegexListNull() {
String input = "\n\nHuman:hello good words.\n\nAssistant:";
Boolean res = mlGuard.validateRegexList(input, null);

Assert.assertTrue(res);
}

@Test
public void validateRegexListEmpty() {
String input = "\n\nHuman:hello good words.\n\nAssistant:";
Boolean res = mlGuard.validateRegexList(input, List.of());

Assert.assertTrue(res);
}

@Test
public void validateRegexSuccess() {
String input = "\n\nHuman:hello good words.\n\nAssistant:";
Expand Down Expand Up @@ -138,6 +154,18 @@ public void validateStopWords() throws IOException {
Assert.assertTrue(res);
}

@Test
public void validateStopWordsNull() {
Boolean res = mlGuard.validateStopWords("hello world", null);
Assert.assertTrue(res);
}

@Test
public void validateStopWordsEmpty() {
Boolean res = mlGuard.validateStopWords("hello world", Map.of());
Assert.assertTrue(res);
}

@Test
public void validateStopWordsSingleIndex() throws IOException {
SearchResponse searchResponse = createSearchResponse(1);
Expand Down

0 comments on commit abba0ec

Please sign in to comment.