• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.networknt.schema;
17 
18 import java.util.Locale;
19 import java.util.Map;
20 import java.util.function.BiConsumer;
21 
22 import com.networknt.schema.i18n.MessageSource;
23 
24 /**
25  * MessageSourceValidationMessage.
26  */
27 public class MessageSourceValidationMessage {
28 
builder(MessageSource messageSource, Map<String, String> errorMessage, BiConsumer<ValidationMessage, Boolean> observer)29     public static Builder builder(MessageSource messageSource, Map<String, String> errorMessage,
30             BiConsumer<ValidationMessage, Boolean> observer) {
31         return new Builder(messageSource, errorMessage, observer);
32     }
33 
34     public static class Builder extends BuilderSupport<Builder> {
Builder(MessageSource messageSource, Map<String, String> errorMessage, BiConsumer<ValidationMessage, Boolean> observer)35         public Builder(MessageSource messageSource, Map<String, String> errorMessage,
36                 BiConsumer<ValidationMessage, Boolean> observer) {
37             super(messageSource, errorMessage, observer);
38         }
39 
40         @Override
self()41         public Builder self() {
42             return this;
43         }
44     }
45 
46     public abstract static class BuilderSupport<S> extends ValidationMessage.BuilderSupport<S> {
47         private final BiConsumer<ValidationMessage, Boolean> observer;
48         private final MessageSource messageSource;
49         private final Map<String, String> errorMessage;
50         private boolean failFast;
51         private Locale locale;
52 
BuilderSupport(MessageSource messageSource, Map<String, String> errorMessage, BiConsumer<ValidationMessage, Boolean> observer)53         public BuilderSupport(MessageSource messageSource, Map<String, String> errorMessage,
54                 BiConsumer<ValidationMessage, Boolean> observer) {
55             this.messageSource = messageSource;
56             this.observer = observer;
57             this.errorMessage = errorMessage;
58         }
59 
60         @Override
build()61         public ValidationMessage build() {
62             // Use custom error message if present
63             String messagePattern = null;
64             if (this.errorMessage != null) {
65                 messagePattern = this.errorMessage.get("");
66                 if (this.property != null) {
67                     String specificMessagePattern = this.errorMessage.get(this.property);
68                     if (specificMessagePattern != null) {
69                         messagePattern = specificMessagePattern;
70                     }
71                 }
72                 if (messagePattern != null && !"".equals(messagePattern)) {
73                     this.message = messagePattern;
74                 }
75             }
76 
77             // Default to message source formatter
78             if (this.message == null && this.messageSupplier == null && this.messageFormatter == null) {
79                 this.messageFormatter = args -> this.messageSource.getMessage(this.messageKey,
80                         this.locale == null ? Locale.ROOT : this.locale, args);
81             }
82             ValidationMessage validationMessage = super.build();
83             if (this.observer != null) {
84                 this.observer.accept(validationMessage, this.failFast);
85             }
86             return validationMessage;
87         }
88 
locale(Locale locale)89         public S locale(Locale locale) {
90             this.locale = locale;
91             return self();
92         }
93 
failFast(boolean failFast)94         public S failFast(boolean failFast) {
95             this.failFast = failFast;
96             return self();
97         }
98     }
99 }
100