Skip to content

Commit 880a90e

Browse files
authored
refactor!: put getDefaultValidator implementation behind a feature flag (#3597)
1 parent 22209fd commit 880a90e

22 files changed

Lines changed: 342 additions & 18 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.vaadin.experimental.enforceFieldValidation=true

‎vaadin-date-picker-flow-parent/vaadin-date-picker-flow/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<groupId>javax.servlet</groupId>
3030
<artifactId>javax.servlet-api</artifactId>
3131
<version>3.1.0</version>
32-
<scope>test</scope>
32+
<scope>provided</scope>
3333
</dependency>
3434
<dependency>
3535
<groupId>com.vaadin</groupId>

‎vaadin-date-picker-flow-parent/vaadin-date-picker-flow/src/main/java/com/vaadin/flow/component/datepicker/DatePicker.java‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
import java.util.stream.Collectors;
2525
import java.util.stream.Stream;
2626

27+
import com.vaadin.experimental.Feature;
28+
import com.vaadin.experimental.FeatureFlags;
29+
import com.vaadin.flow.server.VaadinService;
2730
import com.vaadin.flow.component.AttachEvent;
2831
import com.vaadin.flow.component.ComponentEventListener;
2932
import com.vaadin.flow.component.shared.HasAllowedCharPattern;
@@ -461,7 +464,11 @@ public String getErrorMessage() {
461464

462465
@Override
463466
public Validator<LocalDate> getDefaultValidator() {
464-
return (value, context) -> checkValidity(value);
467+
if (isFeatureFlagEnabled(FeatureFlags.ENFORCE_FIELD_VALIDATION)) {
468+
return (value, context) -> checkValidity(value);
469+
}
470+
471+
return Validator.alwaysPass();
465472
}
466473

467474
@Override
@@ -744,6 +751,27 @@ public void removeThemeVariants(DatePickerVariant... variants) {
744751
.collect(Collectors.toList()));
745752
}
746753

754+
/**
755+
* Returns true if the given feature flag is enabled, false otherwise.
756+
* <p>
757+
* Exposed with protected visibility to support mocking
758+
* <p>
759+
* The method requires the {@code VaadinService} instance to obtain the
760+
* available feature flags, otherwise, the feature is considered disabled.
761+
*
762+
* @param feature
763+
* the feature flag.
764+
* @return whether the feature flag is enabled.
765+
*/
766+
protected boolean isFeatureFlagEnabled(Feature feature) {
767+
VaadinService service = VaadinService.getCurrent();
768+
if (service == null) {
769+
return false;
770+
}
771+
772+
return FeatureFlags.get(service.getContext()).isEnabled(feature);
773+
}
774+
747775
/**
748776
* The internationalization properties for {@link DatePicker}.
749777
*/

‎vaadin-date-picker-flow-parent/vaadin-date-picker-flow/src/test/java/com/vaadin/flow/component/datepicker/DatePickerBinderValidationTest.java‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.vaadin.flow.component.datepicker;
22

3+
import com.vaadin.experimental.Feature;
4+
import com.vaadin.experimental.FeatureFlags;
5+
import com.vaadin.flow.component.Tag;
36
import com.vaadin.flow.data.binder.Binder;
47
import com.vaadin.flow.data.binder.BindingValidationStatus;
58
import com.vaadin.flow.data.binder.BindingValidationStatusHandler;
@@ -27,6 +30,18 @@ public class DatePickerBinderValidationTest {
2730
@Mock
2831
private BindingValidationStatusHandler statusHandlerMock;
2932

33+
@Tag("test-date-picker")
34+
private class TestDatePicker extends DatePicker {
35+
protected boolean isFeatureFlagEnabled(Feature feature) {
36+
if (feature.getId() == FeatureFlags.ENFORCE_FIELD_VALIDATION
37+
.getId()) {
38+
return true;
39+
}
40+
41+
return super.isFeatureFlagEnabled(feature);
42+
}
43+
}
44+
3045
public static class Bean {
3146
private LocalDate date;
3247

@@ -42,7 +57,7 @@ public void setDate(LocalDate date) {
4257
@Before
4358
public void init() {
4459
MockitoAnnotations.openMocks(this);
45-
field = new DatePicker();
60+
field = new TestDatePicker();
4661
field.setMax(LocalDate.now().plusDays(1));
4762
}
4863

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.vaadin.experimental.enforceFieldValidation=true

‎vaadin-date-time-picker-flow-parent/vaadin-date-time-picker-flow/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
<groupId>javax.servlet</groupId>
3535
<artifactId>javax.servlet-api</artifactId>
3636
<version>3.1.0</version>
37-
<scope>test</scope>
37+
<scope>provided</scope>
3838
</dependency>
3939
<dependency>
4040
<groupId>com.vaadin</groupId>

‎vaadin-date-time-picker-flow-parent/vaadin-date-time-picker-flow/src/main/java/com/vaadin/flow/component/datetimepicker/DateTimePicker.java‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import java.util.stream.Collectors;
2626
import java.util.stream.Stream;
2727

28+
import com.vaadin.experimental.Feature;
29+
import com.vaadin.experimental.FeatureFlags;
2830
import com.vaadin.flow.component.*;
2931
import com.vaadin.flow.component.datepicker.DatePicker.DatePickerI18n;
3032
import com.vaadin.flow.component.dependency.JsModule;
@@ -35,6 +37,7 @@
3537
import com.vaadin.flow.data.binder.ValidationResult;
3638
import com.vaadin.flow.data.binder.Validator;
3739
import com.vaadin.flow.function.SerializableFunction;
40+
import com.vaadin.flow.server.VaadinService;
3841

3942
@Tag("vaadin-date-time-picker-date-picker")
4043
@NpmPackage(value = "@vaadin/polymer-legacy-adapter", version = "23.2.0-beta2")
@@ -624,7 +627,11 @@ public boolean isInvalid() {
624627

625628
@Override
626629
public Validator<LocalDateTime> getDefaultValidator() {
627-
return (value, context) -> checkValidity(value);
630+
if (isFeatureFlagEnabled(FeatureFlags.ENFORCE_FIELD_VALIDATION)) {
631+
return (value, context) -> checkValidity(value);
632+
}
633+
634+
return Validator.alwaysPass();
628635
}
629636

630637
private ValidationResult checkValidity(LocalDateTime value) {
@@ -804,4 +811,25 @@ public void removeThemeVariants(DateTimePickerVariant... variants) {
804811
Stream.of(variants).map(DateTimePickerVariant::getVariantName)
805812
.collect(Collectors.toList()));
806813
}
814+
815+
/**
816+
* Returns true if the given feature flag is enabled, false otherwise.
817+
* <p>
818+
* Exposed with protected visibility to support mocking
819+
* <p>
820+
* The method requires the {@code VaadinService} instance to obtain the
821+
* available feature flags, otherwise, the feature is considered disabled.
822+
*
823+
* @param feature
824+
* the feature flag.
825+
* @return whether the feature flag is enabled.
826+
*/
827+
protected boolean isFeatureFlagEnabled(Feature feature) {
828+
VaadinService service = VaadinService.getCurrent();
829+
if (service == null) {
830+
return false;
831+
}
832+
833+
return FeatureFlags.get(service.getContext()).isEnabled(feature);
834+
}
807835
}

‎vaadin-date-time-picker-flow-parent/vaadin-date-time-picker-flow/src/test/java/com/vaadin/flow/component/datetimepicker/DateTimePickerBinderValidationTest.java‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.vaadin.flow.component.datetimepicker;
22

3+
import com.vaadin.experimental.Feature;
4+
import com.vaadin.experimental.FeatureFlags;
5+
import com.vaadin.flow.component.Tag;
36
import com.vaadin.flow.data.binder.Binder;
47
import com.vaadin.flow.data.binder.BindingValidationStatus;
58
import com.vaadin.flow.data.binder.BindingValidationStatusHandler;
@@ -27,6 +30,18 @@ public class DateTimePickerBinderValidationTest {
2730
@Mock
2831
private BindingValidationStatusHandler statusHandlerMock;
2932

33+
@Tag("test-date-time-picker")
34+
private class TestDateTimePicker extends DateTimePicker {
35+
protected boolean isFeatureFlagEnabled(Feature feature) {
36+
if (feature.getId() == FeatureFlags.ENFORCE_FIELD_VALIDATION
37+
.getId()) {
38+
return true;
39+
}
40+
41+
return super.isFeatureFlagEnabled(feature);
42+
}
43+
}
44+
3045
public static class Bean {
3146
private LocalDateTime date;
3247

@@ -42,7 +57,7 @@ public void setDate(LocalDateTime date) {
4257
@Before
4358
public void init() {
4459
MockitoAnnotations.openMocks(this);
45-
field = new DateTimePicker();
60+
field = new TestDateTimePicker();
4661
field.setMax(LocalDateTime.now().plusDays(1));
4762
}
4863

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.vaadin.experimental.enforceFieldValidation=true

‎vaadin-text-field-flow-parent/vaadin-text-field-flow/src/main/java/com/vaadin/flow/component/textfield/AbstractNumberField.java‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import java.math.BigDecimal;
2020

21+
import com.vaadin.experimental.Feature;
22+
import com.vaadin.experimental.FeatureFlags;
2123
import com.vaadin.flow.component.AttachEvent;
2224
import com.vaadin.flow.component.CompositionNotifier;
2325
import com.vaadin.flow.component.HasHelper;
@@ -36,6 +38,7 @@
3638
import com.vaadin.flow.data.value.HasValueChangeMode;
3739
import com.vaadin.flow.data.value.ValueChangeMode;
3840
import com.vaadin.flow.function.SerializableFunction;
41+
import com.vaadin.flow.server.VaadinService;
3942

4043
/**
4144
* Abstract base class for components based on {@code vaadin-number-field}
@@ -370,7 +373,11 @@ public boolean isInvalid() {
370373

371374
@Override
372375
public Validator<T> getDefaultValidator() {
373-
return (value, context) -> checkValidity(value);
376+
if (isFeatureFlagEnabled(FeatureFlags.ENFORCE_FIELD_VALIDATION)) {
377+
return (value, context) -> checkValidity(value);
378+
}
379+
380+
return Validator.alwaysPass();
374381
}
375382

376383
private ValidationResult checkValidity(T value) {
@@ -454,4 +461,25 @@ public void addThemeVariants(TextFieldVariant... variants) {
454461
public void removeThemeVariants(TextFieldVariant... variants) {
455462
HasThemeVariant.super.removeThemeVariants(variants);
456463
}
464+
465+
/**
466+
* Returns true if the given feature flag is enabled, false otherwise.
467+
* <p>
468+
* Exposed with protected visibility to support mocking
469+
* <p>
470+
* The method requires the {@code VaadinService} instance to obtain the
471+
* available feature flags, otherwise, the feature is considered disabled.
472+
*
473+
* @param feature
474+
* the feature flag.
475+
* @return whether the feature flag is enabled.
476+
*/
477+
protected boolean isFeatureFlagEnabled(Feature feature) {
478+
VaadinService service = VaadinService.getCurrent();
479+
if (service == null) {
480+
return false;
481+
}
482+
483+
return FeatureFlags.get(service.getContext()).isEnabled(feature);
484+
}
457485
}

0 commit comments

Comments
 (0)