Skip to content

Commit 5600c73

Browse files
Add the 'DO_NOT_FAIL_IF_NOT_VALID' property validation option (#24)
1 parent 3245974 commit 5600c73

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

core/src/main/java/com/cosium/hal_mock_mvc/Form.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@ private ValidatedFormProperty<?> validate(
197197
FormProperty<?> property, PropertyValidationOption... validationOptions) throws Exception {
198198
Set<PropertyValidationOption> validationOptionSet =
199199
Optional.ofNullable(validationOptions).map(Set::of).orElse(Set.of());
200+
if (validationOptionSet.contains(PropertyValidationOption.Immediate.DO_NOT_FAIL_IF_NOT_VALID)) {
201+
return ValidatedFormProperty.markAsValid(property);
202+
}
200203
TemplatePropertyRepresentation representation =
201204
template.representation().propertyByName().get(property.name());
202205
if (representation == null) {

core/src/main/java/com/cosium/hal_mock_mvc/PropertyValidationOption.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ enum Immediate implements PropertyValidationOption {
99
/** Do not fail if writing to a property not declared by the template */
1010
DO_NOT_FAIL_IF_NOT_DECLARED,
1111
/** Do not fail if writing to a property declared as read-only by the template */
12-
DO_NOT_FAIL_IF_DECLARED_READ_ONLY
12+
DO_NOT_FAIL_IF_DECLARED_READ_ONLY,
13+
/** Do not fail if writing invalid value to a property */
14+
DO_NOT_FAIL_IF_NOT_VALID,
1315
}
1416
}

core/src/test/java/com/cosium/hal_mock_mvc/FormTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;
88
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
99

10+
import com.cosium.hal_mock_mvc.PropertyValidationOption.Immediate;
1011
import com.fasterxml.jackson.jr.ob.JSON;
1112
import com.jayway.jsonpath.DocumentContext;
1213
import com.jayway.jsonpath.JsonPath;
@@ -1843,6 +1844,40 @@ void test39() throws Exception {
18431844
.doesNotThrowAnyException();
18441845
}
18451846

1847+
@Test
1848+
@DisplayName("User can force an invalid property")
1849+
void test40() throws Exception {
1850+
myController.getResponseToSend =
1851+
JSON.std
1852+
.composeString()
1853+
.startObject()
1854+
.startObjectField("_links")
1855+
.startObjectField("self")
1856+
.put("href", "http://localhost/form-test:put")
1857+
.end()
1858+
.end()
1859+
.startObjectField("_templates")
1860+
.startObjectField("default")
1861+
.put("method", "PUT")
1862+
.startArrayField("properties")
1863+
.end()
1864+
.end()
1865+
.end()
1866+
.end()
1867+
.finish();
1868+
1869+
Form form =
1870+
HalMockMvc.builder(mockMvc)
1871+
.baseUri(linkTo(methodOn(MyController.class).get()).toUri())
1872+
.build()
1873+
.follow()
1874+
.templates()
1875+
.byKey("default")
1876+
.createForm();
1877+
assertThatCode(() -> form.withString("foo", "foo", Immediate.DO_NOT_FAIL_IF_NOT_VALID))
1878+
.doesNotThrowAnyException();
1879+
}
1880+
18461881
@Controller
18471882
public static class MyController {
18481883

0 commit comments

Comments
 (0)