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 17 package com.networknt.schema; 18 19 import java.util.Locale; 20 import java.util.Objects; 21 import java.util.function.Predicate; 22 23 /** 24 * Configuration per execution. 25 */ 26 public class ExecutionConfig { 27 /** 28 * The locale to use for formatting messages. 29 */ 30 private Locale locale = Locale.ROOT; 31 32 /** 33 * Determines if annotation collection is enabled. 34 * <p> 35 * This does not affect annotation collection required for evaluating keywords 36 * such as unevaluatedItems or unevaluatedProperties and only affects reporting. 37 */ 38 private boolean annotationCollectionEnabled = false; 39 40 /** 41 * If annotation collection is enabled, determine which annotations to collect. 42 * <p> 43 * This does not affect annotation collection required for evaluating keywords 44 * such as unevaluatedItems or unevaluatedProperties and only affects reporting. 45 */ 46 private Predicate<String> annotationCollectionFilter = keyword -> false; 47 48 /** 49 * Since Draft 2019-09 format assertions are not enabled by default. 50 */ 51 private Boolean formatAssertionsEnabled = null; 52 53 /** 54 * Determine if the validation execution can fail fast. 55 */ 56 private boolean failFast = false; 57 58 /** 59 * Gets the locale to use for formatting messages. 60 * 61 * @return the locale 62 */ getLocale()63 public Locale getLocale() { 64 return locale; 65 } 66 67 /** 68 * Sets the locale to use for formatting messages. 69 * 70 * @param locale the locale 71 */ setLocale(Locale locale)72 public void setLocale(Locale locale) { 73 this.locale = Objects.requireNonNull(locale, "Locale must not be null"); 74 } 75 76 /** 77 * Gets the format assertion enabled flag. 78 * <p> 79 * This defaults to null meaning that it will follow the defaults of the 80 * specification. 81 * <p> 82 * Since draft 2019-09 this will default to false unless enabled by using the 83 * $vocabulary keyword. 84 * 85 * @return the format assertions enabled flag 86 */ getFormatAssertionsEnabled()87 public Boolean getFormatAssertionsEnabled() { 88 return formatAssertionsEnabled; 89 } 90 91 /** 92 * Sets the format assertion enabled flag. 93 * 94 * @param formatAssertionsEnabled the format assertions enabled flag 95 */ setFormatAssertionsEnabled(Boolean formatAssertionsEnabled)96 public void setFormatAssertionsEnabled(Boolean formatAssertionsEnabled) { 97 this.formatAssertionsEnabled = formatAssertionsEnabled; 98 } 99 100 /** 101 * Return if fast fail is enabled. 102 * 103 * @return if fast fail is enabled 104 */ isFailFast()105 public boolean isFailFast() { 106 return failFast; 107 } 108 109 /** 110 * Sets whether fast fail is enabled. 111 * 112 * @param failFast true to fast fail 113 */ setFailFast(boolean failFast)114 public void setFailFast(boolean failFast) { 115 this.failFast = failFast; 116 } 117 118 /** 119 * Return if annotation collection is enabled. 120 * <p> 121 * This does not affect annotation collection required for evaluating keywords 122 * such as unevaluatedItems or unevaluatedProperties and only affects reporting. 123 * <p> 124 * The annotations to collect can be customized using the annotation collection 125 * predicate. 126 * 127 * @return if annotation collection is enabled 128 */ isAnnotationCollectionEnabled()129 public boolean isAnnotationCollectionEnabled() { 130 return annotationCollectionEnabled; 131 } 132 133 /** 134 * Sets whether the annotation collection is enabled. 135 * <p> 136 * This does not affect annotation collection required for evaluating keywords 137 * such as unevaluatedItems or unevaluatedProperties and only affects reporting. 138 * <p> 139 * The annotations to collect can be customized using the annotation collection 140 * predicate. 141 * 142 * @param annotationCollectionEnabled true to enable annotation collection 143 */ setAnnotationCollectionEnabled(boolean annotationCollectionEnabled)144 public void setAnnotationCollectionEnabled(boolean annotationCollectionEnabled) { 145 this.annotationCollectionEnabled = annotationCollectionEnabled; 146 } 147 148 /** 149 * Gets the predicate to determine if annotation collection is allowed for a 150 * particular keyword. This only has an effect if annotation collection is 151 * enabled. 152 * <p> 153 * The default value is to not collect any annotation keywords if annotation 154 * collection is enabled. 155 * <p> 156 * This does not affect annotation collection required for evaluating keywords 157 * such as unevaluatedItems or unevaluatedProperties and only affects reporting. 158 * 159 * @return the predicate to determine if annotation collection is allowed for 160 * the keyword 161 */ getAnnotationCollectionFilter()162 public Predicate<String> getAnnotationCollectionFilter() { 163 return annotationCollectionFilter; 164 } 165 166 /** 167 * Predicate to determine if annotation collection is allowed for a particular 168 * keyword. This only has an effect if annotation collection is enabled. 169 * <p> 170 * The default value is to not collect any annotation keywords if annotation 171 * collection is enabled. 172 * <p> 173 * This does not affect annotation collection required for evaluating keywords 174 * such as unevaluatedItems or unevaluatedProperties and only affects reporting. 175 * 176 * @param annotationCollectionFilter the predicate accepting the keyword 177 */ setAnnotationCollectionFilter(Predicate<String> annotationCollectionFilter)178 public void setAnnotationCollectionFilter(Predicate<String> annotationCollectionFilter) { 179 this.annotationCollectionFilter = Objects.requireNonNull(annotationCollectionFilter, 180 "annotationCollectionFilter must not be null"); 181 } 182 183 } 184