Skip to content

Commit

Permalink
#26 Added studio validators for forms
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas Fechner authored and TallenceJanHendrikPopp committed Jun 20, 2019
1 parent 6fb2cb6 commit a4fef95
Show file tree
Hide file tree
Showing 16 changed files with 988 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,27 @@
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.tallence.formeditor.cae;

import com.coremedia.blueprint.testing.ContentTestHelper;
import com.tallence.formeditor.cae.elements.*;
import com.tallence.formeditor.cae.elements.CheckBoxesGroup;
import com.tallence.formeditor.cae.elements.ConsentFormCheckBox;
import com.tallence.formeditor.cae.elements.FormElement;
import com.tallence.formeditor.cae.elements.NumberField;
import com.tallence.formeditor.cae.elements.RadioButtonGroup;
import com.tallence.formeditor.cae.elements.SelectBox;
import com.tallence.formeditor.cae.elements.TextArea;
import com.tallence.formeditor.cae.elements.TextField;
import com.tallence.formeditor.cae.elements.TextOnly;
import com.tallence.formeditor.cae.elements.ZipField;
import com.tallence.formeditor.cae.validator.InvalidGroupElementException;
import com.tallence.formeditor.contentbeans.FormEditor;
import org.junit.Test;
Expand All @@ -30,7 +38,9 @@
import java.util.Collections;

import static com.tallence.formeditor.cae.parser.ConsentFormCheckBoxParser.CONSENT_FORM_CHECK_BOX_TYPE;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.junit.Assert.*;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
<!--
~ Copyright 2018 Tallence AG
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<content>
<folder id="1" name="" placeApproved="true" published="true" modificationDate="2009-06-01T20:59:42.000+01:00">
<folder id="3" name="Content Test">
Expand All @@ -7,7 +23,7 @@
<markupProperty name="formData" url="2formElements.xml" grammar="coremedia-struct-2008"/>
</version>
</document>
<document id="4" type="FormEditor" name="TestForm" modificationDate="2009-06-01T20:59:42.000+01:00">
<document id="4" type="FormEditor" name="TestForm2" modificationDate="2009-06-01T20:59:42.000+01:00">
<version number="1">
<stringProperty name="formAction" value="mailAction"/>
<stringProperty name="adminMails" value="admin@example.com"/>
Expand Down
48 changes: 48 additions & 0 deletions form-editor-studio-lib/pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2018 Tallence AG
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

Expand Down Expand Up @@ -45,6 +61,38 @@
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>

<!-- Testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.coremedia.cms</groupId>
<artifactId>cap-client-xml</artifactId>
<scope>test</scope>
</dependency>


</dependencies>

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright 2018 Tallence AG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.tallence.formeditor.studio.validator;

import com.coremedia.cap.content.Content;
import com.coremedia.cap.struct.Struct;
import com.coremedia.rest.cap.validation.ContentTypeValidatorBase;
import com.coremedia.rest.validation.Issues;
import com.coremedia.rest.validation.Severity;
import com.tallence.formeditor.contentbeans.FormEditor;
import com.tallence.formeditor.studio.validator.field.FieldValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
* Validates, that a form with form action "mailAction" does not have a fileUpload-field and has a mail-address entered.
*/
public class FormEditorValidator extends ContentTypeValidatorBase {

@Autowired
private List<FieldValidator> fieldValidators;

private Map<String, List<FieldValidator>> fieldValidatorsByType = new HashMap<>();

@PostConstruct
public void init() {
fieldValidators.forEach(validator -> {
validator.resonsibleFor().forEach(type -> {
if (!fieldValidatorsByType.containsKey(type)) {
fieldValidatorsByType.put(type, new ArrayList<>());
}
fieldValidatorsByType.get(type).add(validator);
});
});
}

@Override
public void validate(Content content, Issues issues) {

Struct formData = content.getStruct(FormEditor.FORM_DATA);
String action = content.getString(FormEditor.FORM_ACTION);

// Validate form fields
if (formData.get(FormEditor.FORM_ELEMENTS) != null) {
Struct formElements = formData.getStruct(FormEditor.FORM_ELEMENTS);

formElements.getProperties().values()
.stream()
.filter(Struct.class::isInstance)
.map(Struct.class::cast)
.forEach(fieldStruct -> {
String type = (String) fieldStruct.get("type");
Optional.ofNullable(fieldValidatorsByType.get(type)).ifPresent(fieldValidatorsForType -> {
for (FieldValidator fieldValidator : fieldValidatorsForType) {
fieldValidator.validateField(fieldStruct, action, issues);
}
});
});
}

// Further validations
if (FormEditor.MAIL_ACTION.equals(action) && !StringUtils.hasText(content.getString(FormEditor.ADMIN_MAILS))) {
issues.addIssue(Severity.ERROR, FormEditor.FORM_ACTION, "form_action_mail");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2018 Tallence AG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.tallence.formeditor.studio.validator.field;

import com.coremedia.cap.struct.Struct;
import com.coremedia.rest.validation.Issues;

import java.util.List;

/**
* Defines a common interface for form editor fild validators.
*/
public interface FieldValidator {

/**
* Returns the field type(s) this validator acts on.
*/
List<String> resonsibleFor();

/**
* Validate a single field configuration.
*
* @param fieldData the sub struct of formElements for a field.
* @param action the current action of the form (some validators need this).
* @param issues the Issues object for the current validation request.
*/
void validateField(Struct fieldData, String action, Issues issues);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2018 Tallence AG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.tallence.formeditor.studio.validator.field;

import com.coremedia.cap.struct.Struct;
import com.coremedia.rest.validation.Issues;
import com.coremedia.rest.validation.Severity;
import com.tallence.formeditor.contentbeans.FormEditor;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;

/**
* Validates, that all form elements have a name.
*/
@Component
public class NameNotEmptyValidator implements FieldValidator {
@Override
public List<String> resonsibleFor() {
return Arrays.asList("TextField", "NumberField", "RadioButtons", "CheckBoxes", "SelectBox", "TextArea", "UsersMail",
"FileUpload", "ConsentFormCheckBox");
}

@Override
public void validateField(Struct fieldData, String action, Issues issues) {
if (!StringUtils.hasText(fieldData.getString("name"))) {
issues.addIssue(Severity.ERROR, FormEditor.FORM_ELEMENTS, "missing_name");
}
}
}
Loading

0 comments on commit a4fef95

Please sign in to comment.