Skip to content

Add support for @Pattern annotations in String schemas #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ private JsonSchema addValidationConstraints(JsonSchema schema, BeanProperty prop
StringSchema stringSchema = schema.asStringSchema();
stringSchema.setMaxLength(constraintResolver.getStringMaxLength(prop));
stringSchema.setMinLength(constraintResolver.getStringMinLength(prop));
stringSchema.setPattern(constraintResolver.getStringPattern(prop));
}
return schema;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,13 @@ public Integer getStringMaxLength(BeanProperty prop) {
public Integer getStringMinLength(BeanProperty prop) {
return getMinSize(prop);
}

@Override
public String getStringPattern(final BeanProperty prop) {
Pattern patternAnnotation = prop.getAnnotation(Pattern.class);
if (patternAnnotation != null) {
return patternAnnotation.regexp();
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ public interface ValidationConstraintResolver {

Integer getStringMinLength(BeanProperty prop);

String getStringPattern(BeanProperty prop);
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ public static class ValidationBean {
@Size(min = 15, max = 16)
private String stringWithMinAndMaxSize;

@Pattern(regexp = "[a-z]+")
private String stringWithPattern;

public List<String> getListWithoutConstraints() {
return listWithoutConstraints;
}
Expand Down Expand Up @@ -222,6 +225,14 @@ public String getStringWithMinAndMaxSize() {
public void setStringWithMinAndMaxSize(String stringWithMinAndMaxSize) {
this.stringWithMinAndMaxSize = stringWithMinAndMaxSize;
}

public String getStringWithPattern() {
return stringWithPattern;
}

public void setStringWithPattern(final String stringWithPattern) {
this.stringWithPattern = stringWithPattern;
}
}

/*
Expand Down Expand Up @@ -256,6 +267,11 @@ private Object[][] stringTestData() {
{"stringWithMinAndMaxSize", 15, 16}};
}

private Object[][] stringPatternTestData() {
return new Object[][] {{"stringWithPattern", "[a-z]+"},
{"stringWithoutConstraints", null}};
}

/**
* Test set validation constraints
*/
Expand Down Expand Up @@ -294,6 +310,13 @@ public void testAddingValidationConstraints() throws Exception {
assertEquals(testCase[1], stringSchema.getMinLength());
assertEquals(testCase[2], stringSchema.getMaxLength());
}
for (Object[] testCase : stringPatternTestData()) {
JsonSchema propertySchema = properties.get(testCase[0]);
assertNotNull(propertySchema);
assertTrue(propertySchema.isStringSchema());
StringSchema stringSchema = propertySchema.asStringSchema();
assertEquals(testCase[1], stringSchema.getPattern());
}
}

}