Skip to content

Commit

Permalink
Add option to validate for any of multiple templates in TemplateValid…
Browse files Browse the repository at this point in the history
…ator
  • Loading branch information
eschleb committed Jul 23, 2024
1 parent 7f1b1f5 commit 00038e5
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package com.merkle.oss.magnolia.definition.custom.validator.template;

import com.vaadin.data.ValidationResult;
import com.vaadin.data.ValueContext;
import com.vaadin.data.validator.AbstractValidator;
import info.magnolia.jcr.util.NodeTypes;
import info.magnolia.jcr.util.PropertyUtil;

import java.util.Set;

import javax.jcr.Node;
import java.util.Objects;

import com.vaadin.data.ValidationResult;
import com.vaadin.data.ValueContext;
import com.vaadin.data.validator.AbstractValidator;

public class TemplateValidator extends AbstractValidator<Node> {
private final String templateId;
private final Set<String> templateIds;

public TemplateValidator(
final String templateId,
final Set<String> templateIds,
final String errorMessage) {
super(errorMessage);
this.templateId = templateId;
this.templateIds = templateIds;
}

@Override
Expand All @@ -25,6 +27,6 @@ public ValidationResult apply(final Node value, final ValueContext context) {
}

private boolean isValid(final Node value) {
return value == null || Objects.equals(templateId, PropertyUtil.getString(value, NodeTypes.Renderable.TEMPLATE));
return value == null || templateIds.contains(PropertyUtil.getString(value, NodeTypes.Renderable.TEMPLATE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@
import info.magnolia.ui.field.FieldValidatorFactory;
import info.magnolia.ui.field.ValidatorType;

import java.util.Set;

@I18nable(keyGenerator = FieldValidatorDefinitionKeyGenerator.class)
@ValidatorType("templateValidator")
public class TemplateValidatorDefinition extends ConfiguredFieldValidatorDefinition {
private final String templateId;
private final Set<String> templateIds;

public TemplateValidatorDefinition(final String templateId) {
this.templateId = templateId;
public TemplateValidatorDefinition(final Set<String> templateIds) {
this.templateIds = templateIds;
}

@Override
public Class<? extends FieldValidatorFactory> getFactoryClass() {
return TemplateValidatorFactory.class;
}

public String getTemplateId() {
return templateId;
public Set<String> getTemplateIds() {
return templateIds;
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package com.merkle.oss.magnolia.definition.custom.validator.template;

import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.merkle.oss.magnolia.definition.builder.validator.AbstractConfiguredFieldValidatorDefinitionBuilder;

public class TemplateValidatorDefinitionBuilder extends AbstractConfiguredFieldValidatorDefinitionBuilder<TemplateValidatorDefinition, TemplateValidatorDefinitionBuilder> {

public TemplateValidatorDefinition build(final String templateId) {
return build("templateValidator", templateId);
}
public TemplateValidatorDefinition build(final String name, final String templateId) {
final TemplateValidatorDefinition definition = new TemplateValidatorDefinition(templateId);
super.populate(definition, name);
return definition;
}
public TemplateValidatorDefinition build(final String templateId, final String... additionalTemplateIds) {
return build("templateValidator", templateId, additionalTemplateIds);
}

public TemplateValidatorDefinition build(final String name, final String templateId, final String... additionalTemplateIds) {
final TemplateValidatorDefinition definition = new TemplateValidatorDefinition(Stream.concat(
Stream.of(templateId),
Arrays.stream(additionalTemplateIds)
).collect(Collectors.toSet()));
super.populate(definition, name);
return definition;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import javax.inject.Inject;
import javax.jcr.Node;
import java.text.MessageFormat;
import java.util.stream.Collectors;

public class TemplateValidatorFactory extends AbstractFieldValidatorFactory<TemplateValidatorDefinition, Node> {

Expand All @@ -16,14 +17,14 @@ public TemplateValidatorFactory(final TemplateValidatorDefinition definition) {

@Override
public Validator<Node> createValidator() {
return new TemplateValidator(definition.getTemplateId(), getI18nErrorMessage());
return new TemplateValidator(definition.getTemplateIds(), getI18nErrorMessage());
}

@Override
public String getI18nErrorMessage() {
final MessageFormat fmt = new MessageFormat(super.getI18nErrorMessage());
final String[] args = new String[]{
definition.getTemplateId()
String.join("|", definition.getTemplateIds())
};
return fmt.format(args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import javax.jcr.Node;
import java.util.Collections;
import java.util.Map;
import java.util.Set;

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

Expand All @@ -18,7 +19,7 @@ class TemplateValidatorTest {

@BeforeEach
public void setUp() {
validator = new TemplateValidator("someTemplate", "error");
validator = new TemplateValidator(Set.of("someTemplate"), "error");
}

@Test
Expand Down

0 comments on commit 00038e5

Please sign in to comment.