Search in sources :

Example 1 with DataValidationError

use of com.enonic.xp.content.DataValidationError in project xp by enonic.

the class ContentDataSerializer method addValidationErrors.

private void addValidationErrors(final ValidationErrors validationErrors, final PropertySet contentAsData) {
    if (validationErrors != null && validationErrors.hasErrors()) {
        contentAsData.addSets(VALIDATION_ERRORS, validationErrors.stream().map(validationError -> {
            final PropertySet propertySet = new PropertySet();
            propertySet.addString("errorCode", validationError.getErrorCode().toString());
            propertySet.addString("message", validationError.getMessage());
            propertySet.addString("i18n", validationError.getI18n());
            if (!validationError.getArgs().isEmpty()) {
                try {
                    propertySet.addString("args", OBJECT_MAPPER.writeValueAsString(validationError.getArgs()));
                } catch (JsonProcessingException e) {
                    throw new UncheckedIOException(e);
                }
            }
            if (validationError instanceof DataValidationError) {
                propertySet.addString("propertyPath", ((DataValidationError) validationError).getPropertyPath().toString());
            } else if (validationError instanceof AttachmentValidationError) {
                propertySet.addString("attachment", ((AttachmentValidationError) validationError).getAttachment().toString());
            }
            return propertySet;
        }).toArray(PropertySet[]::new));
    }
}
Also used : PropertySet(com.enonic.xp.data.PropertySet) UncheckedIOException(java.io.UncheckedIOException) AttachmentValidationError(com.enonic.xp.content.AttachmentValidationError) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) DataValidationError(com.enonic.xp.content.DataValidationError)

Example 2 with DataValidationError

use of com.enonic.xp.content.DataValidationError in project xp by enonic.

the class OccurrenceValidatorTest method given_input_with_minOccur1_with_one_null_value_when_validate_then_hasErrors_returns_MinimumOccurrencesValidationError.

@Test
public void given_input_with_minOccur1_with_one_null_value_when_validate_then_hasErrors_returns_MinimumOccurrencesValidationError() {
    contentType.getForm().getFormItems().add(Input.create().name("myInput").label("Input").inputType(InputTypeName.TEXT_LINE).minimumOccurrences(1).build());
    Content content = Content.create().path(MY_CONTENT_PATH).type(contentType.getName()).build();
    content.getData().setString("myInput[0]", null);
    // exercise
    final ValidationErrors validationResults = validate(content);
    assertTrue(validationResults.hasErrors());
    assertThat(validationResults.stream()).allMatch(ve -> ve instanceof DataValidationError);
}
Also used : ValidationErrors(com.enonic.xp.content.ValidationErrors) Content(com.enonic.xp.content.Content) DataValidationError(com.enonic.xp.content.DataValidationError) Test(org.junit.jupiter.api.Test)

Example 3 with DataValidationError

use of com.enonic.xp.content.DataValidationError in project xp by enonic.

the class OccurrenceValidatorTest method given_input_with_maxOccur1_with_two_nonnull_data_when_validate_then_MaximumOccurrencesValidationError.

@Test
public void given_input_with_maxOccur1_with_two_nonnull_data_when_validate_then_MaximumOccurrencesValidationError() {
    contentType.getForm().getFormItems().add(Input.create().name("myInput").label("Input").inputType(InputTypeName.TEXT_LINE).maximumOccurrences(1).build());
    Content content = Content.create().path(MY_CONTENT_PATH).type(contentType.getName()).build();
    content.getData().setString("myInput[0]", "1");
    content.getData().setString("myInput[1]", "2");
    // exercise
    final ValidationErrors validationResults = validate(content);
    assertThat(validationResults.stream()).allMatch(ve -> ve instanceof DataValidationError);
}
Also used : ValidationErrors(com.enonic.xp.content.ValidationErrors) Content(com.enonic.xp.content.Content) DataValidationError(com.enonic.xp.content.DataValidationError) Test(org.junit.jupiter.api.Test)

Example 4 with DataValidationError

use of com.enonic.xp.content.DataValidationError in project xp by enonic.

the class OccurrenceValidatorTest method given_input_with_minOccur1_with_two_null_values_when_validate_then_hasErrors_returns_MinimumOccurrencesValidationError.

@Test
public void given_input_with_minOccur1_with_two_null_values_when_validate_then_hasErrors_returns_MinimumOccurrencesValidationError() {
    contentType.getForm().getFormItems().add(Input.create().name("myInput").label("Input").inputType(InputTypeName.TEXT_LINE).minimumOccurrences(1).build());
    Content content = Content.create().path(MY_CONTENT_PATH).type(contentType.getName()).build();
    content.getData().setString("myInput[0]", null);
    content.getData().setString("myInput[1]", null);
    // exercise
    final ValidationErrors validationResults = validate(content);
    assertTrue(validationResults.hasErrors());
    assertThat(validationResults.stream()).allMatch(ve -> ve instanceof DataValidationError);
}
Also used : ValidationErrors(com.enonic.xp.content.ValidationErrors) Content(com.enonic.xp.content.Content) DataValidationError(com.enonic.xp.content.DataValidationError) Test(org.junit.jupiter.api.Test)

Example 5 with DataValidationError

use of com.enonic.xp.content.DataValidationError in project xp by enonic.

the class OccurrenceValidatorTest method given_input_with_minOccur1_and_maxOccur2_with_one_null_value_when_validate_then_hasErrors_returns_MinimumOccurrencesValidationError.

@Test
public void given_input_with_minOccur1_and_maxOccur2_with_one_null_value_when_validate_then_hasErrors_returns_MinimumOccurrencesValidationError() {
    contentType.getForm().getFormItems().add(Input.create().name("myInput").label("Input").inputType(InputTypeName.TEXT_LINE).minimumOccurrences(1).build());
    Content content = Content.create().path(MY_CONTENT_PATH).type(contentType.getName()).build();
    content.getData().setString("myInput[0]", null);
    // exercise
    final ValidationErrors validationResults = validate(content);
    assertTrue(validationResults.hasErrors());
    assertThat(validationResults.stream()).allMatch(ve -> ve instanceof DataValidationError);
}
Also used : ValidationErrors(com.enonic.xp.content.ValidationErrors) Content(com.enonic.xp.content.Content) DataValidationError(com.enonic.xp.content.DataValidationError) Test(org.junit.jupiter.api.Test)

Aggregations

DataValidationError (com.enonic.xp.content.DataValidationError)9 Content (com.enonic.xp.content.Content)8 ValidationErrors (com.enonic.xp.content.ValidationErrors)8 Test (org.junit.jupiter.api.Test)8 FormItemSet (com.enonic.xp.form.FormItemSet)2 AttachmentValidationError (com.enonic.xp.content.AttachmentValidationError)1 PropertySet (com.enonic.xp.data.PropertySet)1 FormOptionSet (com.enonic.xp.form.FormOptionSet)1 FormOptionSetOption (com.enonic.xp.form.FormOptionSetOption)1 Input (com.enonic.xp.form.Input)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 UncheckedIOException (java.io.UncheckedIOException)1