protected void processConstraintViolation(final Set<ConstraintViolation<Object>> violations,
            final SheetBindingErrors<?> errors) {
        
        for(ConstraintViolation<Object> violation : violations) {
            
            final String fieldName = violation.getPropertyPath().toString();
            final Optional<FieldError> fieldError = errors.getFirstFieldError(fieldName);
            
            if(fieldError.isPresent() && fieldError.get().isConversionFailure()) {
                // 型変換エラーが既存のエラーにある場合は、処理をスキップする。
                continue;
            }
            
            final ConstraintDescriptor<?> cd = violation.getConstraintDescriptor();
            
            /*
             * エラーメッセージのコードは、後から再変換できるよう、BeanValidationの形式のエラーコードも付けておく。
             */
            final String[] errorCodes = new String[]{
                    cd.getAnnotation().annotationType().getSimpleName(),
                    cd.getAnnotation().annotationType().getCanonicalName(),
                    cd.getAnnotation().annotationType().getCanonicalName() + ".message"
                    };
            
            final Map<String, Object> errorVars = createVariableForConstraint(cd);
            
            final String nestedPath = errors.buildFieldPath(fieldName);
            if(Utils.isEmpty(nestedPath)) {
                // オブジェクトエラーの場合
                errors.createGlobalError(errorCodes)
                    .variables(errorVars)
                    .defaultMessage(violation.getMessage())
                    .buildAndAddError();
                
            } else {
                // フィールドエラーの場合
                
                // 親のオブジェクトから、セルの座標を取得する
                final Object parentObj = violation.getLeafBean();
                final Path path = violation.getPropertyPath();
                Optional<CellPosition> cellAddress = Optional.empty();
                Optional<String> label = Optional.empty();
                if(path instanceof PathImpl) {
                    final PathImpl pathImpl = (PathImpl) path;
                    cellAddress = new PositionGetterFactory().create(parentObj.getClass(), pathImpl.getLeafNode().getName())
                            .map(getter -> getter.get(parentObj)).orElse(Optional.empty());
                    
                    label = new LabelGetterFactory().create(parentObj.getClass(), pathImpl.getLeafNode().getName())
                            .map(getter -> getter.get(parentObj)).orElse(Optional.empty());
                    
                }
                
                // フィールドフォーマッタ
                Class<?> fieldType = errors.getFieldType(nestedPath);
                if(fieldType != null) {
                    FieldFormatter<?> fieldFormatter = errors.findFieldFormatter(nestedPath, fieldType);
                    if(fieldFormatter != null) {
                        errorVars.putIfAbsent("fieldFormatter", fieldFormatter);
                    }
                }
                
                // 実際の値を取得する
                errorVars.putIfAbsent("validatedValue", violation.getInvalidValue());
                
                errors.createFieldError(fieldName, errorCodes)
                    .variables(errorVars)
                    .address(cellAddress)
                    .label(label)
                    .defaultMessage(violation.getMessage())
                    .buildAndAddError();
                
            }
            
        }
        
    }
protected void processConstraintViolation ( final Set < ConstraintViolation < Object > > violations , final SheetBindingErrors < ? > errors ) { for ( ConstraintViolation < Object > violation : violations ) { final String fieldName = violation . getPropertyPath ( ) . toString ( ) ; final Optional < FieldError > fieldError = errors . getFirstFieldError ( fieldName ) ; if ( fieldError . isPresent ( ) && fieldError . get ( ) . isConversionFailure ( ) ) { continue ; } final ConstraintDescriptor < ? > cd = violation . getConstraintDescriptor ( ) ; final String [ ] errorCodes = new String [ ] { cd . getAnnotation ( ) . annotationType ( ) . getSimpleName ( ) , cd . getAnnotation ( ) . annotationType ( ) . getCanonicalName ( ) , cd . getAnnotation ( ) . annotationType ( ) . getCanonicalName ( ) + ".message" } ; final Map < String , Object > errorVars = createVariableForConstraint ( cd ) ; final String nestedPath = errors . buildFieldPath ( fieldName ) ; if ( Utils . isEmpty ( nestedPath ) ) { errors . createGlobalError ( errorCodes ) . variables ( errorVars ) . defaultMessage ( violation . getMessage ( ) ) . buildAndAddError ( ) ; } else { final Object parentObj = violation . getLeafBean ( ) ; final Path path = violation . getPropertyPath ( ) ; Optional < CellPosition > cellAddress = Optional . empty ( ) ; Optional < String > label = Optional . empty ( ) ; if ( path instanceof PathImpl ) { final PathImpl pathImpl = ( PathImpl ) path ; cellAddress = new PositionGetterFactory ( ) . create ( parentObj . getClass ( ) , pathImpl . getLeafNode ( ) . getName ( ) ) . map ( getter -> getter . get ( parentObj ) ) . orElse ( Optional . empty ( ) ) ; label = new LabelGetterFactory ( ) . create ( parentObj . getClass ( ) , pathImpl . getLeafNode ( ) . getName ( ) ) . map ( getter -> getter . get ( parentObj ) ) . orElse ( Optional . empty ( ) ) ; } Class < ? > fieldType = errors . getFieldType ( nestedPath ) ; if ( fieldType != null ) { FieldFormatter < ? > fieldFormatter = errors . findFieldFormatter ( nestedPath , fieldType ) ; if ( fieldFormatter != null ) { errorVars . putIfAbsent ( "fieldFormatter" , fieldFormatter ) ; } } errorVars . putIfAbsent ( "validatedValue" , violation . getInvalidValue ( ) ) ; errors . createFieldError ( fieldName , errorCodes ) . variables ( errorVars ) . address ( cellAddress ) . label ( label ) . defaultMessage ( violation . getMessage ( ) ) . buildAndAddError ( ) ; } } }
BeanValidationの検証結果をSheet用のエラーに変換する
@param violations BeanValidationの検証結果
@param errors シートのエラー