Skip to content

Commit

Permalink
Add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagohora committed Oct 4, 2024
1 parent fd5164b commit e778b4f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@

public class ValidationUtils {

/**
* Regular expression to validate if a string is null or not blank.
*
* <p>It matches any string that is not null and contains at least one non-whitespace character.</p>
* For example:
* <ul>
* <li>"" -> false</li>
* <li>" " -> false</li>
* <li>"\n" -> false</li>
* <li>null -> true</li>
* <li>"a" -> true</li>
* <li>" a " -> true</li>
* <li>"\n a \n" -> true</li>
* </ul>
*
* @see <a href="https://regexper.com/">Visual Explainer</a>
* @see <a href="https://zzzcode.ai/regex/explain">Ai Explainer</a>
*/
public static final String NULL_OR_NOT_BLANK = "(?s)^\\s*(\\S.*\\S|\\S)\\s*$";

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.comet.opik.utils;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;

class ValidationUtilsTest {

public static Stream<Arguments> testNullOrNotBlank() {
return Stream.of(
Arguments.of("", false),
Arguments.of(" ", false),
Arguments.of("\n", false),
Arguments.of("a", true),
Arguments.of(" a ", true),
Arguments.of("\n a \n", true)
);
}

@ParameterizedTest
@MethodSource
void testNullOrNotBlank(String input, boolean expected) {
assertEquals(expected, input.matches(ValidationUtils.NULL_OR_NOT_BLANK));
}

}

0 comments on commit e778b4f

Please sign in to comment.