Search in sources :

Example 46 with ValidationErrors

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

the class OccurrenceValidatorTest method given_required_input_at_top_and_inside_formItemSet_and_formItemSet_have_other_unrequired_data_when_validate_then_two_errors_are_found.

@Test
public void given_required_input_at_top_and_inside_formItemSet_and_formItemSet_have_other_unrequired_data_when_validate_then_two_errors_are_found() {
    Input myInput = Input.create().name("myRequiredInput").label("Input").inputType(InputTypeName.TEXT_LINE).required(true).build();
    FormItemSet mySet = FormItemSet.create().name("mySet").required(false).addFormItem(myInput).build();
    contentType.getForm().getFormItems().add(mySet);
    contentType.getForm().getFormItems().add(Input.create().name("myOtherRequiredInput").label("Other input").inputType(InputTypeName.TEXT_LINE).required(true).build());
    Content content = Content.create().path(MY_CONTENT_PATH).type(contentType.getName()).build();
    content.getData().setString("mySet.myUnrequiredData", "1");
    assertEquals("mySet.myRequiredInput", mySet.getInput("myRequiredInput").getPath().toString());
    // exercise
    final ValidationErrors validationResults = validate(content);
    assertThat(validationResults.stream()).hasSize(2).allMatch(ve -> ve instanceof DataValidationError).extracting(ValidationError::getArgs).containsExactly(Arrays.array(List.of("mySet.myRequiredInput", 1, 0), List.of("myOtherRequiredInput", 1, 0)));
}
Also used : Input(com.enonic.xp.form.Input) ValidationErrors(com.enonic.xp.content.ValidationErrors) Content(com.enonic.xp.content.Content) FormItemSet(com.enonic.xp.form.FormItemSet) DataValidationError(com.enonic.xp.content.DataValidationError) Test(org.junit.jupiter.api.Test)

Example 47 with ValidationErrors

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

the class OccurrenceValidatorTest method given_input_with_maxOccur2_with_three_data_when_validate_then_MaximumOccurrencesValidationError.

@Test
public void given_input_with_maxOccur2_with_three_data_when_validate_then_MaximumOccurrencesValidationError() {
    contentType.getForm().getFormItems().add(Input.create().name("myInput").label("Input").inputType(InputTypeName.TEXT_LINE).maximumOccurrences(2).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");
    content.getData().setString("myInput[2]", "3");
    // exercise
    final ValidationErrors validationResults = validate(content);
    assertThat(validationResults.stream().findFirst()).containsInstanceOf(DataValidationError.class).get().extracting(ValidationError::getArgs, LIST).containsExactly("myInput", 2, 3);
}
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 48 with ValidationErrors

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

the class OccurrenceValidatorTest method given_required_optionset_with_no_data_within_layout_when_validate_then_MinimumOccurrencesValidationError.

@Test
public void given_required_optionset_with_no_data_within_layout_when_validate_then_MinimumOccurrencesValidationError() {
    FormOptionSet.Builder myOptionSet = FormOptionSet.create().occurrences(Occurrences.create(1, 0)).name("myOptionSet");
    FormOptionSetOption.Builder option1 = FormOptionSetOption.create().name("option1").defaultOption(true);
    option1.addFormItem(Input.create().name("myInput").label("Input").inputType(InputTypeName.TEXT_LINE).build());
    myOptionSet.addOptionSetOption(option1.build());
    contentType.getForm().getFormItems().add(FieldSet.create().label("My layout").name("myLayout").addFormItem(myOptionSet.build()).build());
    Content content = Content.create().path(MY_CONTENT_PATH).type(contentType.getName()).build();
    // exercise
    final ValidationErrors validationResults = validate(content);
    assertTrue(validationResults.hasErrors());
    assertThat(validationResults.stream()).allMatch(ve -> ve instanceof DataValidationError);
}
Also used : FormOptionSet(com.enonic.xp.form.FormOptionSet) ValidationErrors(com.enonic.xp.content.ValidationErrors) Content(com.enonic.xp.content.Content) FormOptionSetOption(com.enonic.xp.form.FormOptionSetOption) DataValidationError(com.enonic.xp.content.DataValidationError) Test(org.junit.jupiter.api.Test)

Example 49 with ValidationErrors

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

the class OccurrenceValidatorTest method given_required_input_with_no_data_when_validate_then_validation_error_propertyPath_correct.

@Test
public void given_required_input_with_no_data_when_validate_then_validation_error_propertyPath_correct() {
    contentType.getForm().getFormItems().add(Input.create().name("myInput").label("Input").inputType(InputTypeName.TEXT_LINE).required(true).build());
    Content content = Content.create().path(MY_CONTENT_PATH).type(contentType.getName()).build();
    // exercise
    final ValidationErrors validationResults = validate(content);
    assertThat(validationResults.stream().findFirst()).get(as(type(DataValidationError.class))).extracting(DataValidationError::getPropertyPath).asString().isEqualTo("myInput");
}
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 50 with ValidationErrors

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

the class CreateContentCommand method populateValid.

private void populateValid(final CreateContentTranslatorParams.Builder builder) {
    final ValidationErrors validationErrors = ValidateContentDataCommand.create().data(builder.getData()).extraDatas(builder.getExtraDatas()).contentTypeName(builder.getType()).contentName(builder.getName()).displayName(builder.getDisplayName()).createAttachments(builder.getCreateAttachments()).contentValidators(this.contentValidators).contentTypeService(this.contentTypeService).build().execute();
    if (params.isRequireValid()) {
        validationErrors.stream().findFirst().ifPresent(validationError -> {
            throw new ContentDataValidationException(validationError.getMessage());
        });
    }
    builder.valid(!validationErrors.hasErrors());
    builder.validationErrors(validationErrors);
}
Also used : ValidationErrors(com.enonic.xp.content.ValidationErrors) ContentDataValidationException(com.enonic.xp.content.ContentDataValidationException)

Aggregations

ValidationErrors (com.enonic.xp.content.ValidationErrors)51 Content (com.enonic.xp.content.Content)48 Test (org.junit.jupiter.api.Test)48 DataValidationError (com.enonic.xp.content.DataValidationError)21 ContentType (com.enonic.xp.schema.content.ContentType)10 FormOptionSet (com.enonic.xp.form.FormOptionSet)8 FormOptionSetOption (com.enonic.xp.form.FormOptionSetOption)8 GetContentTypeParams (com.enonic.xp.schema.content.GetContentTypeParams)7 FormItemSet (com.enonic.xp.form.FormItemSet)5 PropertySet (com.enonic.xp.data.PropertySet)4 ApplicationKey (com.enonic.xp.app.ApplicationKey)3 PropertyTree (com.enonic.xp.data.PropertyTree)3 ContentDataValidationException (com.enonic.xp.content.ContentDataValidationException)2 UpdateContentTranslatorParams (com.enonic.xp.content.UpdateContentTranslatorParams)2 FieldSet (com.enonic.xp.form.FieldSet)2 Input (com.enonic.xp.form.Input)2 PageDescriptorService (com.enonic.xp.page.PageDescriptorService)2 LayoutDescriptorService (com.enonic.xp.region.LayoutDescriptorService)2 PartDescriptorService (com.enonic.xp.region.PartDescriptorService)2 ContentTypeName (com.enonic.xp.schema.content.ContentTypeName)2