Skip to content

Commit

Permalink
Add validator definition builders
Browse files Browse the repository at this point in the history
  • Loading branch information
eschleb committed Feb 21, 2024
1 parent b36730a commit fa3b75b
Show file tree
Hide file tree
Showing 13 changed files with 470 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public abstract class AbstractConfiguredFieldDefinitionBuilder<T, D extends Conf
private String styleName;
@Nullable
private Class<? extends Converter<T, ?>> converterClass;
@Nullable
private List<FieldValidatorDefinition> validators;

protected AbstractConfiguredFieldDefinitionBuilder(final Provider<D> factory) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.merkle.oss.magnolia.definition.builder.validator;

import info.magnolia.ui.field.ConfiguredFieldValidatorDefinition;

import javax.annotation.Nullable;
import javax.inject.Provider;
import java.util.Optional;

public abstract class AbstractConfiguredFieldValidatorDefinitionBuilder<D extends ConfiguredFieldValidatorDefinition, B extends AbstractConfiguredFieldValidatorDefinitionBuilder<D, B>> {
private final Provider<D> factory;

@Nullable
private String errorMessage;

protected AbstractConfiguredFieldValidatorDefinitionBuilder(final Provider<D> factory) {
this.factory = factory;
}

public B errorMessage(final String errorMessage) {
this.errorMessage = errorMessage;
return self();
}

@SuppressWarnings("unchecked")
protected B self() {
return (B) this;
}

protected D build(final String name) {
final D definition = factory.get();
definition.setName(name);
Optional.ofNullable(errorMessage).ifPresent(definition::setErrorMessage);
return definition;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.merkle.oss.magnolia.definition.builder.validator;

import info.magnolia.ui.field.EmailValidatorDefinition;

public class EmailValidatorDefinitionBuilder extends AbstractConfiguredFieldValidatorDefinitionBuilder<EmailValidatorDefinition, EmailValidatorDefinitionBuilder>{

public EmailValidatorDefinitionBuilder() {
super(EmailValidatorDefinition::new);
}

public EmailValidatorDefinition build() {
return build("emailValidator");
}
public EmailValidatorDefinition build(final String name) {
return super.build(name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.merkle.oss.magnolia.definition.builder.validator;

import info.magnolia.ui.editor.validator.NodeNameValidatorDefinition;
import info.magnolia.ui.editor.validator.NodeNameValidatorDefinition.Mode;
import info.magnolia.ui.field.RegexpValidatorDefinition;

import javax.annotation.Nullable;
import java.util.Optional;

public class NodeNameValidatorDefinitionBuilder extends AbstractConfiguredFieldValidatorDefinitionBuilder<NodeNameValidatorDefinition, NodeNameValidatorDefinitionBuilder>{

@Nullable
private String pattern;
@Nullable
private Mode mode;

public NodeNameValidatorDefinitionBuilder() {
super(NodeNameValidatorDefinition::new);
}

public NodeNameValidatorDefinitionBuilder pattern(final String pattern) {
this.pattern = pattern;
return self();
}

public NodeNameValidatorDefinitionBuilder mode(final Mode mode) {
this.mode = mode;
return self();
}

public NodeNameValidatorDefinition build() {
return build("nodeNameValidator");
}
public NodeNameValidatorDefinition build(final String name) {
final NodeNameValidatorDefinition definition = super.build(name);
Optional.ofNullable(mode).ifPresent(definition::setMode);
Optional.ofNullable(pattern).ifPresent(definition::setPattern);
return definition;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.merkle.oss.magnolia.definition.builder.validator;

import info.magnolia.ui.field.RegexpValidatorDefinition;

import javax.annotation.Nullable;
import java.util.Optional;

public class RegexpValidatorDefinitionBuilder extends AbstractConfiguredFieldValidatorDefinitionBuilder<RegexpValidatorDefinition, RegexpValidatorDefinitionBuilder>{

@Nullable
private String pattern;

public RegexpValidatorDefinitionBuilder() {
super(RegexpValidatorDefinition::new);
}

public RegexpValidatorDefinitionBuilder pattern(final String pattern) {
this.pattern = pattern;
return self();
}

public RegexpValidatorDefinition build() {
return build("regexpValidator");
}
public RegexpValidatorDefinition build(final String name) {
final RegexpValidatorDefinition definition = super.build(name);
Optional.ofNullable(pattern).ifPresent(definition::setPattern);
return definition;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package com.merkle.oss.magnolia.definition.builder.validator;

import info.magnolia.ui.field.SafeHtmlValidatorDefinition;
import info.magnolia.ui.field.SafeHtmlValidatorDefinition.Attribute;
import info.magnolia.ui.field.SafeHtmlValidatorDefinition.Protocol;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class SafeHtmlValidatorDefinitionBuilder extends AbstractConfiguredFieldValidatorDefinitionBuilder<SafeHtmlValidatorDefinition, SafeHtmlValidatorDefinitionBuilder>{

@Nullable
private List<String> allowedTags;
@Nullable
private List<String> globallyAllowedAttributes;
@Nullable
private List<Attribute> allowedAttributes;
@Nullable
private List<Protocol> allowedProtocols;

public SafeHtmlValidatorDefinitionBuilder() {
super(SafeHtmlValidatorDefinition::new);
}

public SafeHtmlValidatorDefinitionBuilder allowedTag(final String allowedTag) {
return item(this::allowedTags, allowedTags, allowedTag);
}

public SafeHtmlValidatorDefinitionBuilder allowedTags(final List<String> allowedTags) {
this.allowedTags = allowedTags;
return self();
}

public SafeHtmlValidatorDefinitionBuilder globallyAllowedAttribute(final String globallyAllowedAttribute) {
return item(this::globallyAllowedAttributes, globallyAllowedAttributes, globallyAllowedAttribute);
}

public SafeHtmlValidatorDefinitionBuilder globallyAllowedAttributes(final List<String> globallyAllowedAttributes) {
this.globallyAllowedAttributes = globallyAllowedAttributes;
return self();
}

public SafeHtmlValidatorDefinitionBuilder allowedAttribute(final String tag, final String... attributes) {
return allowedAttribute(new AttributeBuilder().attributes(List.of(attributes)).build(tag));
}

public SafeHtmlValidatorDefinitionBuilder allowedAttribute(final Attribute allowedAttribute) {
return item(this::allowedAttributes, allowedAttributes, allowedAttribute);
}

public SafeHtmlValidatorDefinitionBuilder allowedAttributes(final List<Attribute> allowedAttributes) {
this.allowedAttributes = allowedAttributes;
return self();
}

public SafeHtmlValidatorDefinitionBuilder allowedProtocol(final String tag, final String attribute, final String... protocols) {
return allowedProtocol(new ProtocolBuilder().protocols(List.of(protocols)).build(tag, attribute));
}

public SafeHtmlValidatorDefinitionBuilder allowedProtocol(final Protocol allowedProtocol) {
return item(this::allowedProtocols, allowedProtocols, allowedProtocol);
}

public SafeHtmlValidatorDefinitionBuilder allowedProtocols(final List<Protocol> allowedProtocols) {
this.allowedProtocols = allowedProtocols;
return self();
}

private <T> SafeHtmlValidatorDefinitionBuilder item(final Consumer<List<T>> consumer, final List<T> items, final T item) {
consumer.accept(Stream.concat(
Stream.ofNullable(items).flatMap(Collection::stream),
Stream.of(item)
).collect(Collectors.toList()));
return self();
}

public SafeHtmlValidatorDefinition build() {
return build("safeHtmlValidator");
}
public SafeHtmlValidatorDefinition build(final String name) {
final SafeHtmlValidatorDefinition definition = super.build(name);
Stream.ofNullable(allowedTags).flatMap(Collection::stream).forEach(definition.getAllowedTags()::add);
Stream.ofNullable(globallyAllowedAttributes).flatMap(Collection::stream).forEach(definition.getGloballyAllowedAttributes()::add);
Stream.ofNullable(allowedAttributes).flatMap(Collection::stream).forEach(definition.getAllowedAttributes()::add);
Stream.ofNullable(allowedProtocols).flatMap(Collection::stream).forEach(definition.getAllowedProtocols()::add);
return definition;
}

public static class AttributeBuilder {
private List<String> attributes = new ArrayList<>();

public AttributeBuilder attribute(final String attribute) {
this.attributes.add(attribute);
return this;
}

public AttributeBuilder attributes(final List<String> attributes) {
this.attributes = attributes;
return this;
}

public Attribute build(final String tag) {
final Attribute attribute = new Attribute();
attribute.setTag(tag);
attribute.setAttributes(attributes.toArray(String[]::new));
return attribute;
}
}

public static class ProtocolBuilder {
private List<String> protocols = new ArrayList<>();

public ProtocolBuilder protocol(final String protocol) {
this.protocols.add(protocol);
return this;
}

public ProtocolBuilder protocols(final List<String> protocols) {
this.protocols = protocols;
return this;
}

public Protocol build(final String tag, final String attribute) {
final Protocol protocol = new Protocol();
protocol.setTag(tag);
protocol.setAttribute(attribute);
protocol.setProtocols(protocols.toArray(String[]::new));
return protocol;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.merkle.oss.magnolia.definition.builder.validator;

import info.magnolia.ui.field.SvgUploadValidatorDefinition;

public class SvgUploadValidatorDefinitionBuilder extends AbstractConfiguredFieldValidatorDefinitionBuilder<SvgUploadValidatorDefinition, SvgUploadValidatorDefinitionBuilder>{

public SvgUploadValidatorDefinitionBuilder() {
super(SvgUploadValidatorDefinition::new);
}

public SvgUploadValidatorDefinition build() {
return build("svgUploadValidator");
}
public SvgUploadValidatorDefinition build(final String name) {
return super.build(name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.merkle.oss.magnolia.definition.builder.validator;

import info.magnolia.ui.field.ConfiguredFieldValidatorDefinition;

import java.util.function.BiFunction;

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

public abstract class AbstractConfiguredFieldValidatorDefinitionBuilderTestCase {

public <D extends ConfiguredFieldValidatorDefinition, B extends AbstractConfiguredFieldValidatorDefinitionBuilder<D, B>> B assertValidator(final B builder, final BiFunction<String, B, D> buildFunction) {
final D definition = buildFunction.apply(
"someName",
builder
.errorMessage("someErrorMessage")
);
assertEquals("someErrorMessage", definition.getErrorMessage());
return builder;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.merkle.oss.magnolia.definition.builder.validator;

import info.magnolia.ui.field.EmailValidatorDefinition;
import org.junit.jupiter.api.Test;

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

class EmailValidatorDefinitionBuilderTest extends AbstractConfiguredFieldValidatorDefinitionBuilderTestCase {
@Test
void testBuilder() {
final EmailValidatorDefinition definition = super.assertValidator(new EmailValidatorDefinitionBuilder(), (name, builder) -> builder.build(name))
.build("somEmailValidator");
assertEquals("somEmailValidator", definition.getName());

final EmailValidatorDefinition emptyDefinition = new EmailValidatorDefinitionBuilder().build();
assertEquals("emailValidator", emptyDefinition.getName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.merkle.oss.magnolia.definition.builder.validator;

import info.magnolia.ui.editor.validator.NodeNameValidatorDefinition;
import org.junit.jupiter.api.Test;

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

class NodeNameValidatorDefinitionBuilderTest extends AbstractConfiguredFieldValidatorDefinitionBuilderTestCase {
@Test
void testBuilder() {
final NodeNameValidatorDefinition definition = super.assertValidator(new NodeNameValidatorDefinitionBuilder(), (name, builder) -> builder.build(name))
.mode(NodeNameValidatorDefinition.Mode.ADD)
.pattern("somePattern")
.build("someNodeNameValidator");
assertEquals("someNodeNameValidator", definition.getName());
assertEquals("somePattern", definition.getPattern());
assertEquals(NodeNameValidatorDefinition.Mode.ADD, definition.getMode());

final NodeNameValidatorDefinition emptyDefinition = new NodeNameValidatorDefinitionBuilder().build();
assertEquals("nodeNameValidator", emptyDefinition.getName());
assertEquals(".+", emptyDefinition.getPattern());
assertEquals(NodeNameValidatorDefinition.Mode.EDIT, emptyDefinition.getMode());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.merkle.oss.magnolia.definition.builder.validator;

import info.magnolia.ui.field.RegexpValidatorDefinition;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

class RegexpValidatorDefinitionBuilderTest extends AbstractConfiguredFieldValidatorDefinitionBuilderTestCase {
@Test
void testBuilder() {
final RegexpValidatorDefinition definition = super.assertValidator(new RegexpValidatorDefinitionBuilder(), (name, builder) -> builder.build(name))
.pattern("somePattern")
.build("someRegexpValidator");
assertEquals("someRegexpValidator", definition.getName());
assertEquals("somePattern", definition.getPattern());

final RegexpValidatorDefinition emptyDefinition = new RegexpValidatorDefinitionBuilder().build();
assertEquals("regexpValidator", emptyDefinition.getName());
assertNull(emptyDefinition.getPattern());
}
}
Loading

0 comments on commit fa3b75b

Please sign in to comment.