Closed ghost closed 5 years ago
Hi @adityamandhare. Thank you for contacting me. Unfortunately, there is no standard way for schema authors to assert a string value must not be blank. One method I come up with is using custom format. An example of the format attribute representing "non-blank string" is shown below.
public class NonBlankFormatAttribute implements FormatAttribute {
private static final Pattern BLANK_PATTERN = Pattern.compile("\\s*");
@Override
public String name() {
return "non-blank";
}
@Override
public Localizable localizedName() {
return locale -> "non-blank string";
}
@Override
public InstanceType valueType() {
return InstanceType.STRING;
}
@Override
public boolean test(JsonValue value) {
String string = ((JsonString) value).getString();
return !BLANK_PATTERN.matcher(string).matches();
}
}
When the validator finds a blank string against the schema, an error message like below will be presented.
[2,19][/greeting] The value must be a valid non-blank string.
For writing custom formats, the code sample Custom Format may come in useful for you.
Thanks @leadpony this is helpful. Always appreciate your quick response 🙂!
Hi, currently if a String field is marked as required and if its minLength value is set to 1, it still accepts empty Strings with more than one blank space (" "). Here is an example:
In the above example the
firstName
can still accepts empty strings with more than one blank space ex: " ", would be a valid value. Now we can write a regex for it to make sure empty strings are not passed but the validation error message isn't good enough in that case as it informs the input provider to follow the regex rules. There needs to be a mechanism that if a required string field with minLength of 1, then it should not allow empty strings. Something like a non-empty attribute.