Skip to content

Commit 8c2f60b

Browse files
authored
Make ethlo excludable (#974)
1 parent bc93b44 commit 8c2f60b

File tree

3 files changed

+98
-12
lines changed

3 files changed

+98
-12
lines changed

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,21 @@ The YAML dependency can be excluded if this is not required. Attempting to proce
179179
</dependency>
180180
```
181181

182+
The Ethlo Time dependency can be excluded if accurate validation of the `date-time` format is not required. The `date-time` format will then use `java.time.OffsetDateTime` to determine if the `date-time` is valid .
183+
184+
```xml
185+
<dependency>
186+
<groupId>com.networknt</groupId>
187+
<artifactId>json-schema-validator</artifactId>
188+
<exclusions>
189+
<exclusion>
190+
<groupId>com.ethlo.time</groupId>
191+
<artifactId>itu</artifactId>
192+
</exclusion>
193+
</exclusions>
194+
</dependency>
195+
```
196+
182197
#### Community
183198

184199
This library is very active with a lot of contributors. New features and bug fixes are handled quickly by the team members. Because it is an essential dependency of the [light-4j](https://github.com/networknt/light-4j) framework in the same GitHub organization, it will be evolved and maintained along with the framework.
@@ -199,7 +214,7 @@ This package is available on Maven central.
199214
<dependency>
200215
<groupId>com.networknt</groupId>
201216
<artifactId>json-schema-validator</artifactId>
202-
<version>1.3.1</version>
217+
<version>1.3.3</version>
203218
</dependency>
204219
```
205220

src/main/java/com/networknt/schema/format/DateTimeFormat.java

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,61 @@
11
package com.networknt.schema.format;
22

3+
import java.time.OffsetDateTime;
4+
import java.time.format.DateTimeParseException;
5+
import java.util.function.Predicate;
6+
37
import org.slf4j.Logger;
48
import org.slf4j.LoggerFactory;
59

610
import com.ethlo.time.ITU;
711
import com.ethlo.time.LeapSecondException;
812
import com.networknt.schema.ExecutionContext;
913
import com.networknt.schema.Format;
14+
import com.networknt.schema.utils.Classes;
1015

1116
/**
1217
* Format for date-time.
1318
*/
1419
public class DateTimeFormat implements Format {
1520
private static final Logger logger = LoggerFactory.getLogger(DateTimeFormat.class);
1621

22+
private static final boolean ETHLO_PRESENT = Classes.isPresent("com.ethlo.time.ITU", DateTimeFormat.class.getClassLoader());
23+
24+
/**
25+
* Uses etho.
26+
* <p>
27+
* This needs to be in a holder class otherwise a ClassNotFoundException will be
28+
* thrown when the DateTimeFormat is instantiated.
29+
*/
30+
public static class Ethlo {
31+
public static boolean isValid(String value) {
32+
try {
33+
ITU.parseDateTime(value);
34+
} catch (LeapSecondException ex) {
35+
if (!ex.isVerifiedValidLeapYearMonth()) {
36+
return false;
37+
}
38+
}
39+
return true;
40+
}
41+
}
42+
43+
/**
44+
* Uses java time.
45+
*/
46+
public static class JavaTimeOffsetDateTime {
47+
public static boolean isValid(String value) {
48+
try {
49+
OffsetDateTime.parse(value);
50+
return true;
51+
} catch (DateTimeParseException e) {
52+
return false;
53+
}
54+
}
55+
}
56+
57+
private static final Predicate<String> VALIDATE = ETHLO_PRESENT ? Ethlo::isValid : JavaTimeOffsetDateTime::isValid;
58+
1759
@Override
1860
public String getName() {
1961
return "date-time";
@@ -26,20 +68,12 @@ public String getMessageKey() {
2668

2769
@Override
2870
public boolean matches(ExecutionContext executionContext, String value) {
29-
return isLegalDateTime(value);
71+
return isValid(value);
3072
}
3173

32-
private static boolean isLegalDateTime(String string) {
74+
private static boolean isValid(String value) {
3375
try {
34-
try {
35-
ITU.parseDateTime(string);
36-
} catch (LeapSecondException ex) {
37-
if (!ex.isVerifiedValidLeapYearMonth()) {
38-
return false;
39-
}
40-
}
41-
42-
return true;
76+
return VALIDATE.test(value);
4377
} catch (Exception ex) {
4478
logger.debug("Invalid {}: {}", "date-time", ex.getMessage());
4579
return false;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2024 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.utils;
17+
18+
/**
19+
* Utility methods for classes.
20+
*/
21+
public class Classes {
22+
/**
23+
* Determines if a class is present.
24+
*
25+
* @param name the name of the class
26+
* @param classLoader the class loader
27+
* @return true if present
28+
*/
29+
public static boolean isPresent(String name, ClassLoader classLoader) {
30+
try {
31+
Class.forName(name, false, classLoader);
32+
return true;
33+
} catch (ClassNotFoundException e) {
34+
return false;
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)