org.lastaflute.web.validation.LaValidatable Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lastaflute Show documentation
Show all versions of lastaflute Show documentation
Typesafe Web Framework for LeAn STArtup with DBFlute and Java8
/*
* Copyright 2015-2016 the original author or authors.
*
* 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 org.lastaflute.web.validation;
import org.lastaflute.web.ruts.message.ActionMessages;
/**
* @param The type of action messages.
* @author jflute
*/
public interface LaValidatable {
// ===================================================================================
// Validate
// ========
/**
* Validate the form values.
*
* // by-annotation only
* validate(form, messages -> {}, () -> {
* return asHtml(path_Sea_SeaListJsp);
* });
*
* // by-annotation and by-program
* validate(form, messages -> {
* if (...) {
* messages.addConstraint...
* }
* }, () -> {
* return asHtml(path_Sea_SeaListJsp);
* });
*
* @param form The form that has request parameters. (NotNull)
* @param moreValidationLambda The callback for more validation, e.g. correlation rule. (NotNull)
* @param validationErrorLambda The callback for response when validation error. (NotNull)
* @return The success information of validation, basically for success attribute. (NotNull)
*/
default ValidationSuccess validate(Object form, VaMore moreValidationLambda, VaErrorHook validationErrorLambda) {
return createValidator().validate(form, moreValidationLambda, validationErrorLambda);
}
/**
* Validate the form values for API, when e.g. JsonResponse.
* The validation error handling is in ApiFailureHook.
*
* // by-annotation only
* validateApi(body, messages -> {});
*
* // by-annotation and by-program
* validateApi(body, messages -> {
* if (...) {
* messages.addConstraint...
* }
* });
*
* @param body The form or body that has request parameters. (NotNull)
* @param moreValidationLambda The callback for more validation, e.g. correlation rule, very complex rule. (NotNull)
* @return The success information of validation, basically for success attribute. (NotNull)
*/
default ValidationSuccess validateApi(Object body, VaMore moreValidationLambda) {
return createValidator().validateApi(body, moreValidationLambda);
}
// ===================================================================================
// Throw Error
// ===========
/**
* Throw validation error exception immediately.
*
* if (...) { // any invalid state
* throwValidationError(messages -> messages.addConstraints..., () -> {
* return asHtml(path_Sea_SeaListJsp);
* });
* }
*
* @param validationMessagesLambda The callback for setting of validation error messages. (NotNull)
* @param validationErrorLambda The callback for response when validation error. (NotNull)
*/
default void throwValidationError(VaMessenger validationMessagesLambda, VaErrorHook validationErrorLambda) {
createValidator().throwValidationError(() -> {
final MESSAGES messages = createMessages();
validationMessagesLambda.message(messages);
return messages;
} , validationErrorLambda);
}
/**
* Throw validation error exception immediately, for API, when e.g. JsonResponse.
*
* if (...) { // any invalid state
* throwValidationErrorApi(messages -> messages.addConstraints...);
* }
*
* @param validationMessagesLambda The callback for setting of validation error messages. (NotNull)
*/
default void throwValidationErrorApi(VaMessenger validationMessagesLambda) {
createValidator().throwValidationErrorApi(() -> {
final MESSAGES messages = createMessages();
validationMessagesLambda.message(messages);
return messages;
});
}
// ===================================================================================
// Validation Parts
// ================
/**
* @return The new-created validator for action. (NotNull)
*/
ActionValidator createValidator();
/**
* @return The new-created messages for action. (NotNull)
*/
MESSAGES createMessages();
}